2

mongoose schema:

const userSchema = mongoose.Schema({
  name: {
    type: String,
    required: true
  },
  industry: {
    type: String,
    required: true
  },
  members: [
    {
      name: {
        type: String,
        required: true
      },
      contact: {
        email: {
          type: String,
          required: true
        },
        phone: {
          type: String
        }
      },
      role: {
        type: String,
        required: true
      }
    }
  ]
});

Need to retrieve a member with queried email. The mongo shell command:

db.users.find({"members.contact.email": "johndoe@acmecorp.com"}, {"members.$": 1});

fetching result perfectly:

{
    "_id" : ObjectId("5b17cde35b5da82e2e8bb013"),
    "members" : [ 
        {
            "name" : "John Doe"
            "contact" : {
                "email" : "johndoe@acmecorp.com"
            },
            "_id" : ObjectId("5b18d38c869d6c409e17292f"),
            "role" : "Network Engineer"
        }
    ]
}

When trying to implement this query in the node.js app, a 'user' model function is defined as:

// Get member by email
module.exports.getMemberByEmail = function(email, callback) {
  var query = {{"members.contact.email": email}, {"members.$": 1}};
  User.find(query, callback);
};

But there's a syntax error:

var query = {{"members.contact.email": email}, {"members.$": 1}};
           ^

SyntaxError: Unexpected token {
    at createScript (vm.js:74:10)
    at Object.runInThisContext (vm.js:116:10)
    at Module._compile (module.js:533:28)
    at Object.Module._extensions..js (module.js:580:10)
    at Module.load (module.js:503:32)
    at tryModuleLoad (module.js:466:12)
    at Function.Module._load (module.js:458:3)
    at Module.require (module.js:513:17)
    at require (internal/module.js:11:18)
    at Object.<anonymous> (/home/demoapp/routes/users.js:5:14)
    at Module._compile (module.js:569:30)
    at Object.Module._extensions..js (module.js:580:10)
    at Module.load (module.js:503:32)
    at tryModuleLoad (module.js:466:12)
    at Function.Module._load (module.js:458:3)
    at Module.require (module.js:513:17)

Bit naive to MEAN stack. Any help on the correct syntax is appreciated.

Environment:

  • node v8.0.0
  • mongoose v4.10.4
  • mongoDB v3.4.10
Man In
  • 43
  • 1
  • 6
  • It's not "one object" but "two" arguments: `User.find({"members.contact.email": email}, {"members.$": 1},callback)`. Or even `var arguments = [{"members.contact.email": email}, {"members.$": 1}, callback]; User.find(...arguments)` using the spread operator where supported. – Neil Lunn Jun 09 '18 at 07:00
  • @NeilLunn The other question is about how to write a query in MongoDB; on the other hand, this question is more about how to implement search query in the node.js app. `User.find({"members.contact.email": email}, {"members.$": 1},callback)` does the job well. Can you please elaborate more on using 'arguments' variable and passing it to 'find' function using spread operator. – Man In Jun 09 '18 at 10:58
  • The "other question" is just a "generic" example that shows "two arguments" and not all bundled into a single object like you did in the question. Therefore it's valid. The `...arguments` "thingy" is just another way of "spreading" the array of content to the required arguments of the function. This is valid with ES6 compatible environments, or you can use [`apply()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/apply) to the same effect in earlier JavaScript environments. Just another way of being able to construct the arguments separately. – Neil Lunn Jun 09 '18 at 11:01
  • I see. As a beginner, this is so much to absorb :) Thanks a ton, for your time! – Man In Jun 09 '18 at 11:22

0 Answers0