Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 从Mongoose中的子文档ID查找父文档_Node.js_Mongodb_Express_Mongoose - Fatal编程技术网

Node.js 从Mongoose中的子文档ID查找父文档

Node.js 从Mongoose中的子文档ID查找父文档,node.js,mongodb,express,mongoose,Node.js,Mongodb,Express,Mongoose,我有一个子文档Id,我需要使用Mongoose和MongoDB返回父文档。我在这里读到:我应该能够使用Polls.find({'options':id}),但它返回的是任何空数组,而不是相应的文档 模式 var Polls = new Schema({ name: String, options: [{ name: String, count: Number }] } 抽样调查 { "_id": { "$oid": "58ac96

我有一个
子文档Id
,我需要使用
Mongoose
MongoDB
返回
父文档。我在这里读到:我应该能够使用
Polls.find({'options':id}
),但它返回的是任何空数组,而不是相应的文档

模式

var Polls = new Schema({
    name: String,
    options: [{
        name: String,
        count: Number
    }]
}
抽样调查

{
"_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
}

我认为你的质疑是错误的

试试这个:

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

你能把你的疑问贴出来吗?