2017-02-22 25 views
1

我有一個subdoc Id,我需要使用MongooseMongoDB返回parent Doc。我在這裏讀到:MongoDB: How to find by subdocument ID?,我應該可以使用Polls.find({'options': id},但它返回任何空數組而不是適當的文檔。從Mongoose中的subdoc 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 
} 
+0

能否請您發表您的查詢? –

回答

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