node.js 中的 $elemMatch 投影

cs *_*guy 1 mongodb node.js

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

我在 node.js 中有上面的查询,但由于某种原因,查询的 elemMatch 部分在 node.js 上不起作用,但是当我在 mongo 终端中执行相同的查询时,它可以工作,所以我想也许 node.js 没有支持 $elemMatch?如果是这种情况,有人能告诉我 node.js 中这个查询的等价物是什么吗?

来自我的数据库的示例数据:

/* 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" : ""
}
Run Code Online (Sandbox Code Playgroud)

Joh*_*yHK 5

node.js 驱动程序findOne具有findOne与 MongoDB shell 中不同的调用签名。您将字段选择对象作为参数的projection元素传递options

dbo.collection("users")
    .findOne({"friends.email": email}, 
             {projection: { friends: { $elemMatch: { email: email } } } },
             function(errT, resultT) {...});
Run Code Online (Sandbox Code Playgroud)