我有一个subdoc Id,我需要使用Mongoose和MongoDB返回parent Doc。我在这里读到:MongoDB: How to find by subdocument ID?,我应该能够使用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
}发布于 2017-02-23 18:00:36
我想你问错了。
试试这个:
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
});https://stackoverflow.com/questions/42398898
复制相似问题