[英]fetching mongodb collection records from multiple collections using nodejs
below is all the description collection 1 : users以下是所有描述集合1:用户
"_id" : ObjectId("5e2977e1cc1208c65c00648b"),
"mappedShops" : [
ObjectId("5e2976cbcc1208c65c006488"),
ObjectId("5e2976cbcc1208c65c00690c"),
ObjectId("5e2976cbcc1208c65c006499")
"phoneNo" : 6789012345,
"name" : "acdbcs",
"address" : "address 2",
"shopCode" : "D3137",
"state" : "M.P",
"city" : "Indore"
NOTE : deatils of mappedShops for eg: ObjectId("5e2976cbcc1208c65c00690c") is in the same users collection注意: mappedShops 的详细信息,例如:ObjectId("5e2976cbcc1208c65c00690c") 在同一个用户集合中
collection 2 : orders系列 2 : 订单
"_id" : ObjectId("5e27f998a42d441fe8a8957f"),
"isApproved" : false,
"orderCreatedOn" : ISODate("2020-01-22T18:30:00Z"),
"shopOrder" : [],
"frequency" : "WE",
"orderCreatedBy" : ObjectId("5e2976cbcc1208c65c00690c")
Collection 3: payments集合 3:付款
"_id" : ObjectId("5dd7900bcd00d33c245abbfa"),
"paymentOfTheDay" : 400,
"outstanding" : 100,
"paymentDoneBy":ObjectId("5e2976cbcc1208c65c00690c")
scenario is : i will get _id(ie admin id) from req.body Here is what i need to do 1: i need to know all the mappedShops objectId's & than for all those objectId's i need to find the details of all those objectId's 2: than i need to look what order that shop has created from order collection.(for all those id's) 3: than i need to look what is the outstanding from the payments collection(again for all the id's) Here is what i need to send to the frontend in the mentioned object Array {"name","phoneNo","address"}(from users collection)+{"orderCreatedOn": ISODate("2020-01-22T18:30:00Z"),"isApproved"}(from orders collection)+{"outstanding"}(from payments collection) expected response may look like this:场景是:我将从 req.body 获取 _id(即管理员 ID)这是我需要做的1:我需要知道所有映射商店的 objectId 和所有那些 objectId 我需要找到所有这些 objectId 的详细信息 2 :比我需要查看商店从订单集合中创建的订单。(对于所有这些 ID) 3:比我需要查看付款集合中的未付款项(再次对于所有 ID)这是我需要的发送到提到的对象数组中的前端{"name","phoneNo","address"}(from users collection)+{"orderCreatedOn": ISODate("2020-01-22T18:30:00Z"),"isApproved "}(来自订单收款)+{"outstanding"}(来自付款收款)预期响应可能如下所示:
"shopsListDetails":[{
"phoneNo","name","address","shopCode" ,"isApproved","outstanding"
}]
POSTMAN REQUEST this is the id of admin POSTMAN REQUEST这是管理员的 id
{
"_id": "5e2977e1cc1208c65c00648b"
}
ultimately front end shall receive all the data which i mentioned in shopsListDetails[{}] for all the mappedShops in the user collection against that admin I am kind of very much stuck in this if any can help me out最终前端将收到我在shopsListDetails[{}] 中提到的所有数据,用于针对该管理员的用户集合中的所有mappedShops,如果有任何可以帮助我的话,我非常陷入困境
Considering the first schema you shared is of User
考虑到您共享的第一个架构是User
I am assuming You are storing order_id
inside payments
Schemama我假设您正在payments
Schemama 中存储order_id
You can write aggregate query like this您可以像这样编写聚合查询
User.aggregate([
{
$match:{
_id:req.body.admin_id
}
},{
$unwind:'$mappedShops'
},{
$lookup:{
from:'users',
localField:'mappedShops',
foreignField:'_',
as:'mappedShops'
}
},{
$unwind:'$mappedShops'
},{
$lookup:{
from:'orders',
localField:'mappedShops._id',
foreignField:'orderCreatedBy',
as:'order'
}
},{
$unwind:'$order'
},{
$lookup:{
from:'payments',
localField:'order._id',
foreignField:'order_id',
as:'payment'
}
},{
$unwind:'$payment'
},{
$project:{
_id:0,
name:'$mappedShops.name',
phoneNo:'$mappedShops.phoneNo',
address:'$mappedShops.address',
orderCreatedOn:'$order.orderCreatedOn',
isApproved:'$order.isApproved',
outstanding:'$payment.outstanding'
}
}
])
mappedshops
from orders
collection by $lookup
& $unwind
same |然后,我们通过$lookup
和$unwind
same | 从orders
集合中找到每个mappedshops
匹配订单。 This gives us the order details for final data这为我们提供了最终数据的订单详细信息order_id
inside payments collection through $lookup
& get payment status of each order |最后,我们通过$lookup
匹配付款集合中的order_id
并获取每个订单的付款状态 | This gives us the payment status for final data这为我们提供了最终数据的付款状态PS : For this to worker, you must have the entry of each order in both orders
& payments
collection, whether or not payment is completed PS:对于这个工作人员,您必须在orders
和payments
收集中输入每个orders
,无论付款是否完成
Comment below if you need more clarification / any change is needed here如果您需要更多说明/此处需要任何更改,请在下面发表评论
Please try this :请试试这个:
db.users.aggregate([
{
$match: {
_id: req.body.adminID
}
}, {
$lookup: {
from: 'users',
localField: 'mappedShops',
foreignField: '_id',
as: 'mappedShops'
}
}, { $unwind: '$mappedShops'}, { $replaceRoot: { newRoot: "$mappedShops" } },
{
$lookup:
{
from: "orders",
let: { mappedShopsId: "$_id" },
pipeline: [
{
$match: { $expr: { $eq: ["$orderCreatedBy", "$$mappedShopsId"] } }
},
{ $project: { orderCreatedOn: 1, isApproved: 1 } }
],
as: "orders"
}
},{
$lookup:
{
from: "payments",
let: { mappedShopsId: "$_id" },
pipeline: [
{
$match: { $expr: { $eq: ["$paymentDoneBy", "$$mappedShopsId"] } }
},
{ $project: { outstanding: 1 } }
],
as: "payments"
}
},
{ $project: { name: 1, phoneNo: 1, address: 1, shopCode: 1, orders: 1, payments: 1 } }
])
Test : MongoDB-Playground测试: MongoDB-Playground
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.