$ elem在node.js中匹配投影 [英] $elemMatch projection in node.js

查看:74
本文介绍了$ elem在node.js中匹配投影的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

我在node.js中有上面的查询,但是由于某种原因,查询的elemMatch部分在node.js上不起作用,但是当我在mongo终端中执行相同的查询时,它起作用了,所以我在想也许是node.js不支持$ elemMatch?如果是这样的话,谁能告诉我在node.js中相当于该查询的内容是什么?

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" : ""
}

推荐答案

node.js驱动程序

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) {...});

这篇关于$ elem在node.js中匹配投影的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆