我怀疑如何使用mongoclient在mongodb中映射两个集合。我尝试了一下,但是不起作用。如何解决
group_promotion 集合
PendingIntent launchIntent = notif.getNotification().contentIntent;
if (launchIntent != null) {
try {
launchIntent.send();
} catch (PendingIntent.CanceledException e) {
e.printStackTrace();
}
}
产品集合
{
"_id" : ObjectId("5cf7679a0b0bed2e7483b998"),
"group_name" : "Latest",
"products" : [ObjectId("5cecc161e8c1e73478956333"),ObjectId("5cecc161e8c1e73478956334")]
}
预期输出:
{
"_id" : ObjectId("5cecc161e8c1e73478956333"),
"product_name" : "bourbon"
},
{
"_id" : ObjectId("5cecc161e8c1e73478956334"),
"product_name" : "bour"
}
答案 0 :(得分:2)
您可以直接在products
数组上运行$lookup以获得预期结果:
db.group_promotion.aggregate([
{
$lookup: {
from: "product",
localField: "products",
foreignField: "_id",
as: "products"
}
}
])
这将从数组中提取products
,在products
集合中找到相应的文档,并在products
中覆盖现有的group_production
数组