我正在尝试仅返回具有匹配电子邮件的子文档。我相信,我已经完成了文档所说的here。这是我正在尝试的:
function lookupReferral(email) {
return getConnection().then(db => db.collection('Referrals').findOne(
{
emails: {$elemMatch: {name: email}}
},
{
"emails.$": 1
}
));
}
这是一个文档示例(为简洁起见,我将电子邮件数组削减了):
{
"_id": {
"$oid": "5b65979d84b8942e04f4e346"
},
"accountCode": "auth0|5b4de18d8bed60110409ded5",
"accountEmail": "abc@gmail.com",
"emails": [
{
"name": "a@ddd.dpp",
"signedUp": false,
"updated": {
"$date": "2018-08-04T12:10:05.752Z"
}
},
{
"name": "a@ddd.dpp",
"signedUp": false,
"updated": {
"$date": "2018-08-04T12:10:05.752Z"
}
}
],
"created": {
"$date": "2018-08-04T12:10:05.985Z"
},
"updated": {
"$date": "2018-08-04T12:10:05.985Z"
}
由于某种原因,它返回null(我猜这意味着它不存在吗?),但是如果我删除了我特别想要的东西,那么我会得到整个文档。
答案 0 :(得分:1)
您可以尝试
db.collection.find({
emails: {
$elemMatch: {
name: "a@ddd.dpp"
}
}
},
{
emails: {
$elemMatch: {
name: "a@ddd.dpp"
}
}
})
请参见example