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
});
你能把你的疑问贴出来吗?