首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Mongoose下子单据ID查找上级单据

Mongoose下子单据ID查找上级单据
EN

Stack Overflow用户
提问于 2017-02-23 01:54:56
回答 1查看 1.1K关注 0票数 2

我有一个subdoc Id,我需要使用MongooseMongoDB返回parent Doc。我在这里读到:MongoDB: How to find by subdocument ID?,我应该能够使用Polls.find({'options': id},但是它返回任何空数组,而不是适当的文档。

模式

代码语言:javascript
复制
var Polls = new Schema({
    name: String,
    options: [{
        name: String,
        count: Number
    }]
}

样本调查

代码语言:javascript
复制
{
"_id": {
    "$oid": "58ac963a8a84500de89c1080"
},
"name": "Here is one Poll",
"options": [
    {
        "name": "This is the first one",
        "count": 0,
        "_id": {
            "$oid": "58ac963a8a84500de89c1083"
        }
    },
    {
        "name": "Second One",
        "count": 0,
        "_id": {
            "$oid": "58ac963a8a84500de89c1082"
        }
    }
],
"__v": 0
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-02-23 18:00:36

我想你问错了。

试试这个:

代码语言:javascript
复制
Polls.find({'options._id': id},function(err,result){
    //result will be an array of matched document
    //result[i]._id will give you the parent id.
    //if there is only one such document, you can try : result[0]._id
});
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42398898

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档