How to do it...

Define a static model method called getByFullName that will allow you to search for a specific user using their full name:

  1. Create a new file named static-methods.js
  2. Include the Mongoose NPM module and create a new connection to your MongoDB:
      const mongoose = require('mongoose') 
      const { connection, Schema } = mongoose 
      mongoose.connect( 
          'mongodb://localhost:27017/test' 
      ).catch(console.error) 
  1. Define a schema:
      const UsrSchm = new Schema({ 
          firstName: String, 
          lastName: String, 
          likes: [String], 
      }) 
  1. Define getByFullName static model method:
      UsrSchm.static('getByFullName', function getByFullName(v) { 
          const fullName = String(v).split(' ') 
          const lastName = fullName[0] || '' 
          const firstName = fullName[1] || '' 
          return this.findOne() 
              .where('firstName').equals(firstName) 
              .where('lastName').equals(lastName) 
      }) 
  1. Compile the schema into a model:
      const User = mongoose.model('User', UsrSchm) 
  1. Once connected, create a new user and save it. Then, use the getByFullName static model method to look for the user in the collection of users using their full name:
      connection.once('connected', async () => { 
          try { 
              // Create 
              const user = new User({ 
                  firstName: 'Jingxuan', 
                  lastName: 'Huang', 
                  likes: ['kitties', 'strawberries'], 
              }) 
              await user.save() 
              // Read 
              const person = await User.getByFullName( 
                  'Huang Jingxuan' 
              ) 
              console.log(JSON.stringify(person, null, 4)) 
              await person.remove() 
              await connection.close() 
          } catch (error) { 
              console.log(error.message) 
          } 
      }) 
  1. Save the file
  2. Open a Terminal and run this code:
    node static-methods.js
  
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.129.39.252