gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 10:41:08 26 4
gpt4 key购买 nike

我有一个 subdoc Id,我需要使用 MongooseMongoDB 返回 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
}

最佳答案

我认为你查询错了。

试试这个:

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

关于node.js - 从 Mongoose 的子文档 ID 中查找父文档,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42398898/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com