Mongoose 如何从mongodb中对象类型的嵌套文档中检索选定数据
我很难定义mongo查询,也很难向这个论坛寻求支持 这是我的文件:Mongoose 如何从mongodb中对象类型的嵌套文档中检索选定数据,mongoose,mongodb-query,aggregation-framework,hierarchical-data,Mongoose,Mongodb Query,Aggregation Framework,Hierarchical Data,我很难定义mongo查询,也很难向这个论坛寻求支持 这是我的文件: 它遵循一个树又称层次模型 我有下面这种格式的嵌套文档 我想要达到的目标是什么 能够在任何时候只查询他们的直系子女,即,而不返回孙子女 i、 e当我们查询111个子文档详细信息时: 我只想看看 以下是主要文档: { "_id" : 11, "type" : "parent", "children" : { "111" : { "_id" : 111,
- 它遵循一个树又称层次模型
- 我有下面这种格式的嵌套文档
- 能够在任何时候只查询他们的直系子女,即,而不返回孙子女 i、 e当我们查询111个子文档详细信息时: 我只想看看
{
"_id" : 11,
"type" : "parent",
"children" : {
"111" : {
"_id" : 111,
"type" : "parent",
"children" : {
"1111" : {
"_id" : "1111",
"type" : "parent",
"children" : {
"11111" : {
"type" : "parent",
"_id" : "11111",
"children" : {}
}
}
},
"1112" : {
"_id" : "1112",
"type" : "parent",
"children" : {
"11112" : {
"_id" : "11112",
"children" : {}
}
}
}
}
},
"112" : {
"_id" : "112",
"type" : "parent",
"children" : {}
},
"113" : {
"_id" : "113",
"type" : "parent",
"children" : {}
}
}
}
你试过这个吗?如果子对象是数组形式而不是嵌套对象,那么您的查询将类似于db.your_db_name.find({},{“children.children”:0}),但作为它的对象,我们不能直接使用*值来查找它内部存在的所有键。如果是,它最初是数组。但要服务于更新的用例。我变成了一个嵌套对象,现在检索已经成为数组的瓶颈!使用可变键只是最糟糕的想法之一。如果需要,可以请求更新。有工具(更新操作符)可以更新数组的相应条目。您尝试过这个吗?如果子对象是数组形式而不是嵌套对象,那么您的查询将类似于db.your_db_name.find({},{“children.children”:0}),但作为它的对象,我们不能直接使用*值来查找它内部存在的所有键。如果是,它最初是数组。但要服务于更新的用例。我变成了一个嵌套对象,现在检索已经成为数组的瓶颈!使用可变键只是最糟糕的想法之一。如果需要,可以请求更新,有工具(更新操作符)可以更新数组的适当条目。
{
"_id" : 11,
"type" : "parent",
"children" : {
"111" : {
"_id" : 111,
"type" : "parent",
"children" : {
"1111" : {
"_id" : "1111",
"type" : "parent",
"children" : {
"11111" : {
"type" : "parent",
"_id" : "11111",
"children" : {}
}
}
},
"1112" : {
"_id" : "1112",
"type" : "parent",
"children" : {
"11112" : {
"_id" : "11112",
"children" : {}
}
}
}
}
},
"112" : {
"_id" : "112",
"type" : "parent",
"children" : {}
},
"113" : {
"_id" : "113",
"type" : "parent",
"children" : {}
}
}
}