Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

$elemMatch projection in node.js

dbo.collection("users")
            .findOne({email: emailGiven, "friends.email": element.email},
                     { friends: { $elemMatch: { email: element.email } } },
                     function(errT, resultT) {})

I have the query above in node.js but for some reason the query's elemMatch part doesn't work on node.js but when I execute the same query in mongo terminal it works, so I'm thinking maybe node.js doesn't support $elemMatch? If this is the case, could anyone tell me what would be the equivalent of this query in node.js?

Sample data from my DB:

/* 4 */
{
    "_id" : ObjectId("5ad20cef8248544860ce3dc1"),
    "username" : "test",
    "email": "",
    "fullName" : "",
    "friends" : [{email: "",
                 status :""}],
    "alarms": [ {"id":111,
        "title": "TITLE",
        "location": "",
        "startTime": "10-10-1996 10:18:00",
        "endTime": "10-10-1996 10:18:00" }, {},{}

    ],
     "pnumber" : ""
}
like image 678
cs guy Avatar asked Dec 22 '25 10:12

cs guy


1 Answers

The node.js driver findOne has a different call signature than the findOne in the MongoDB shell. You pass the field selection object as the projection element of the options parameter:

dbo.collection("users")
    .findOne({"friends.email": email}, 
             {projection: { friends: { $elemMatch: { email: email } } } },
             function(errT, resultT) {...});
like image 185
JohnnyHK Avatar answered Dec 24 '25 02:12

JohnnyHK



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!