显示类别与快速蒙戈
我有一个像mongo一样的数据
{'id':id,'name':'root','child'=['id1','id2','id3']}
为CH1:
{'id':id1,'name':'chi1','child'=['id11','id12','id13']}
卡方
{'id':id1,'name':'chi2','child'=['id21','id22','id23']}
chi11
{'id':id1,'name':'chi11','child'=['id111','id112','id113']}
现在我想显示类似的模板:
root.name->chi1.name->chi11.name ->chi2.name
如果我们可以查询孩子的父母。
您可以尝试以下查询: –
db.collName.find({_id : {$in:['id1','id2','id3']}}).toArray(function(err, results){ console.log(results); //Will give you array of child results. })
现在,您可以遍历结果数组并获取相应的子名称。
请参阅$ in-doc以了解如何在查询中使用$in
。
编辑:-
使用如下:
function getResults(id, callback) { db.collection.find({_id : parentId}).toArray(function(err, pres) { db.collName.find({_id : {$in:pres[0].child}}).toArray( function(err, results){ console.log(results); //Will give you array of child results. console.log(pres[0]); // Result of root; callback(err, results, pres) ; //Return all the results }) }); }
希望这会帮助你。
Mongoose – 通过 mongoose 4.0 populate 标准查找子文档是查询儿童对象的最佳解决schemehttp://mongoosejs.com/docs/populate.html