alyx
alyx

Reputation: 2733

Mongo find() query with embedded JSON object not returning data

I'm using Mongoose to query Mongo in Node.js, using this command:

    var qw = {
        'permissions': {'ownerID':req.user._id}
    };   

    landmarkSchema.find(qw, function(err, lm) {
        console.log(lm);
    });

Where req.user._id = "53baf81408802a00002166b8" But nothing is returning. I tried the same query in the Mongo terminal.

My data objects are constructed as:

{
    "_id" : ObjectId("53bb736dbe211d0000f01837"),
    "name" : "and",
    "permissions" : {
        "ownerID" : "53baf81408802a00002166b8",
        "admins" : [ ],
        "viewers" : [ ]
    },
    "__v" : 0
}

I also tried storing the ownerID as a Mongo ObjectID, i.e. ObjectId("53baf81408802a00002166b8") but that didn't seem to work either.

Upvotes: 0

Views: 660

Answers (1)

Peter Lyons
Peter Lyons

Reputation: 146064

Use dot notation:

 var qw = {
    'permissions.ownerID': req.user._id
 };

As written, your query above is searching for a document where the permissions property exactly matches your object, meaning it only has an ownerID property with that value but no other properties.

Upvotes: 2

Related Questions