Reputation: 939
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" : ""
}
Upvotes: 2
Views: 2618
Reputation: 311885
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) {...});
Upvotes: 8
Reputation: 10148
If you want to find
for any values which is stored in some variable, you use regex
for that. Your query should look like
dbo.collection("users").findOne({
email: new RegExp(emailGiven, 'i'),
"friends.email": new RegExp(element.email, 'i')
}, {
friends: {
$elemMatch: {
email: new RegExp(element.email, 'i')
}
}
}, function(errT, resultT) {})
i
is for case insensitive comparison here
Upvotes: 0