我有一个如下所示的查询,它连接 3 个表和项目数据,查询如下所示。
db.incomedistros.aggregate([
{
$lookup: {
from: "orders",
localField: "orderId",
foreignField: "_id",
as: "orderData"
}
},
{
$lookup: {
from: "settlementsdatas",
localField: "_id",
foreignField: "incomeDistroId",
as: "settlementInfo"
}
},
{
"$unwind": "$orderData"
},
{
"$unwind": "$settlementInfo"
},
{
$lookup: {
from: "productvariants",
localField: "orderData.productIds.id",
foreignField: "_id",
as: "productData"
}
},
{
$lookup: {
from: "users",
localField: "orderData.smcId",
foreignField: "_id",
as: "smcData"
}
},
{
"$unwind": "$smcData"
},
{
$skip: 1
},
{
$limit: 2
},
{
$group: { _id: "$productData._id", documents: { $push: "$$ROOT" } }
},
{
$project: {
"_id": 0,
"documents._id": 1,
"documents.createdBy": 1,
"documents.settlementFor": 1,
"documents.taxes": 1,
"documents.deliveryFee": 1,
"documents.platformFree": 1,
"documents.productPrice": 1,
"documents.packingHandlingCharges": 1,
"documents.convenienceFee": 1,
"documents.totalAmt": 1,
"documents.appliedDiscountAmount": 1,
"documents.appliedCouponId": 1,
"documents.productData._id": 1,
"documents.productData.variantName": 1,
"documents.productData.variantWight": 1,
"documents.productData.variantWeightUnit": 1,
"documents.productData.variantPrice": 1,
"documents.orderData.productIds.quantity": 1,
"documents.orderData.productIds.id": 1,
"documents.productData.variantPriceUnit": 1,
"documents.productData.sku": 1,
"documents.smcData.mobile": 1,
"documents.smcData.address": 1,
"documents.settlementInfo": 1
}
}
])
这个查询的输出如下所示,
{
"documents" : [
{
"_id" : ObjectId("65685f425cb3c6a6c7b14511"),
"createdBy" : ObjectId("65682bee420ed0783835ed8e"),
"settlementFor" : "SMC_SETTLEMENT",
"taxes" : 15.4,
"deliveryFee" : 0,
"platformFree" : 2,
"productPrice" : 308,
"packingHandlingCharges" : 4,
"convenienceFee" : 18,
"totalAmt" : 347.4,
"appliedDiscountAmount" : null,
"appliedCouponId" : null,
"orderData" : {
"productIds" : [
{
"id" : ObjectId("652f8bb9c8d17287f5b3ceb4"),
"quantity" : 2
},
{
"id" : ObjectId("652f8c3eb6271488577ff7e4"),
"quantity" : 3
}
]
},
"settlementInfo" : {
"_id" : ObjectId("65685f425cb3c6a6c7b14513"),
"settlementFor" : "SMC_SETTLEMENT",
"orderId" : ObjectId("65685ec6790536a918034030"),
"incomeDistroId" : ObjectId("65685f425cb3c6a6c7b14511"),
"driverShare" : 0,
"gatewayCharges" : 5.210999999999999,
"smcShare" : 308,
"platformFree" : 2,
"taxes" : 15.4
},
"productData" : [
{
"_id" : ObjectId("652f8bb9c8d17287f5b3ceb4"),
"variantName" : "Lays Red Chilli",
"variantWight" : 530,
"variantWeightUnit" : "gm",
"variantPrice" : 64,
"variantPriceUnit" : "rs",
"sku" : "2a58d44e-4d1f-413e-9003-4b95c63cb53c"
},
{
"_id" : ObjectId("652f8c3eb6271488577ff7e4"),
"variantName" : "Lays White Chilli",
"variantWight" : 530,
"variantWeightUnit" : "gm",
"variantPrice" : 60,
"variantPriceUnit" : "rs",
"sku" : "fb6c15dd-8764-4ebd-a725-40a5c1e7518b"
}
],
"smcData" : {
"mobile" : "8999909999",
"address" : [
{
"geoLocation" : {
"coordinates" : [ 77.9, 12.9 ],
"type" : "Point"
},
"isDefault" : false,
"_id" : ObjectId("652f52c0bec8e373187f1c73"),
"fullAddress" : "7th cros naidu layout, Electronic city, Phase II",
"fullAddressToLower" : "7th cros naidu layout, electronic city, phase ii",
"city" : "Bangalore",
"state" : "Karnataka",
"pinCode" : "560100",
"landMark" : "NY First Food",
"buildingName" : "Cat House 2"
}
]
}
}
]
}
现在的问题是
documents.orderData.productIds.id
相当于 documents.productData._id
现在我需要一个输出,在其中我可以在 documents.orderData.productIds.quantity
的每个对象中看到 documents.productData
的值,其中 id 和 _id 匹配。
请找到这三个集合的样本数据,
收入发行版
[{
"_id": {
"$oid": "65685f425cb3c6a6c7b14511"
},
"createdBy": {
"$oid": "65682bee420ed0783835ed8e"
},
"settlementFor": "SMC_SETTLEMENT",
"orderId": {
"$oid": "65685ec6790536a918034030"
},
"taxes": 15.4,
"deliveryFee": 0,
"platformFree": 2,
"productPrice": 308,
"packingHandlingCharges": 4,
"convenienceFee": 18,
"totalAmt": 347.4,
"appliedDiscountAmount": null,
"appliedCouponId": null
}]
定居点
[{
"_id": {
"$oid": "65685f425cb3c6a6c7b14513"
},
"settlementFor": "SMC_SETTLEMENT",
"orderId": {
"$oid": "65685ec6790536a918034030"
},
"incomeDistroId": {
"$oid": "65685f425cb3c6a6c7b14511"
},
"driverShare": 0,
"gatewayCharges": 5.210999999999999,
"smcShare": 308,
"platformFree": 2,
"taxes": 15.4
}]
订单
[{
"_id": {
"$oid": "65685ec6790536a918034030"
},
"pickUpPoint": {
"geoLocation": {
"coordinates": [
77.71,
12.82
],
"type": "Point"
},
"storeAddress": "7th Cross, naidu Layout, EC2",
"storePinCode": "787654321",
"storeName": "Chennai Potato Shop"
},
"dropAddress": {
"geoLocation": {
"coordinates": [
77.71,
12.82
],
"type": "Point"
},
"isDefault": false,
"fullAddress": "9th cros naidu layout, Electronic city, Phase II",
"fullAddressToLower": "9th cros naidu layout, electronic city, phase ii",
"city": "Bangalore",
"state": "Karnataka",
"pinCode": "560100",
"landMark": "NY First Food",
"buildingName": "Cat House 2"
},
"smcId": {
"$oid": "6526287bab1f0e46e59f0552"
},
"productIds": [
{
"_id": {
"$oid": "65685ec6790536a918034032"
},
"id": {
"$oid": "652f8bb9c8d17287f5b3ceb4"
},
"quantity": 2,
"variantPrice": 64
},
{
"_id": {
"$oid": "65685ec6790536a918034031"
},
"id": {
"$oid": "652f8c3eb6271488577ff7e4"
},
"quantity": 3,
"variantPrice": 60
}
],
"orderReferenceNumber": "06561cfa-8734-4776-9f98-466c0b8a463f",
"orderDate": {
"$date": "2023-11-30T10:07:02.923Z"
},
"orderId": "O753452680",
"createdBy": {
"$oid": "65682bee420ed0783835ed8e"
},
"subTotal": 308,
"deliveryCharges": 0,
"distanceToTravel": "1 m",
"distanceToTravelInMeter": 0,
"eta": "1 min",
"currency": "INR",
"orderStatus": "READY_TO_CHECKOUT",
"paymentMethod": "CREDIT_CARD",
"collectedTaxAmount": 15.4,
"platformFee": 2,
"packingHandlingCharges": 4,
"convenienceFee": 18,
"totalAmt": 347.4,
"createdAt": {
"$date": "2023-11-30T10:07:02.931Z"
},
"updatedAt": {
"$date": "2023-11-30T10:07:02.931Z"
},
"__v": 0
}]
请帮我找到这个。
提前致谢
在
$group
的 "$productData._id"
阶段之前,您需要添加一个 $set
阶段,通过将每个元素与 productData
数组中的第一个匹配元素合并来更新 rderData.productIds
数组。
{
$set: {
productData: {
$map: {
input: "$productData",
as: "pd",
in: {
$mergeObjects: [
"$$pd",
{
$first: {
$filter: {
input: "$orderData.productIds",
cond: {
$eq: [
"$$pd._id",
"$$this.id"
]
}
}
}
}
]
}
}
}
}
}
并确保添加
"documents.productData.quantity": 1,
在投影阶段。