MongoDB:如何在客户端解析DBRef? [英] MongoDB: How to resolve DBRef on client-side?
本文介绍了MongoDB:如何在客户端解析DBRef?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我是mongodb的新手,所以很抱歉,如果这个问题很愚蠢:我已提取具有以下结构的文档:
I'm new to mongodb, so sorry if this questions is dumb: I've pulled a document with the following structure:
{
"_id" : ObjectId("575df70512a1aa0adbc2b496"),
"name" : "something",
"content" : {
"product" : {
"$ref" : "products",
"$id" : ObjectId("575ded1012a1aa0adbc2b394"),
"$db" : "mdb"
},
"client" : {
"$ref" : "clients",
"$id" : ObjectId("575ded1012a1aa0adbc2b567"),
"$db" : "mdb"
}
}
我指的是products
和clients
集合中的文档.我已经读到可以在客户端解析这些DBRef( https://stackoverflow.com/a/4067227/1114975 ).
where I'm referring to documents in the products
and clients
collection. I've read that it is possible to resolve these DBRefs on client-side (https://stackoverflow.com/a/4067227/1114975).
我该怎么做?我想避免查询这些对象并将它们嵌入文档中.谢谢
How do I do this? I'd like to avoid querying these objects and embedding them into the document. Thank you
推荐答案
You can resolve this with the $lookup
operator. Consider the following aggregation pipeline:
// Execute aggregate, notice the pipeline is expressed as an Array
collection.aggregate([
{
"$lookup": {
"from": "product",
"localField": "content.product.$id",
"foreignField": "_id",
"as": "products"
}
},
{
"$lookup": {
"from": "clients",
"localField": "content.client.$id",
"foreignField": "_id",
"as": "clients"
}
},
], function(err, result) {
console.log(result);
});
这篇关于MongoDB:如何在客户端解析DBRef?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文