mongoDB groupBy Id,颜色
我在mongodb有树记录 但可能还有更多,我从前端通过一个身份证去商店mongoDB groupBy Id,颜色,mongodb,mongoose,Mongodb,Mongoose,我在mongodb有树记录 但可能还有更多,我从前端通过一个身份证去商店 { "_id" : ObjectId("6072c2d7ea13fb0338f6cf05"), "shopId" : "shop1", <- this is mongodb id "shopItems" : [ { _id: ...,
{
"_id" : ObjectId("6072c2d7ea13fb0338f6cf05"),
"shopId" : "shop1", <- this is mongodb id
"shopItems" : [
{
_id: ...,
itemId: 1, // mongodb id
itemCount: 5,
colorId: colorId1
}
{
_id: ...,
itemId: 2, // mongodb id
itemCount: 3,
colorId: colorId2
}
]
}
{
"_id" : ObjectId("6072c2d7ea13fb0338f6cf05"),
"shopId" : "shop2", <- this is mongodb id
"shopItems" : [
{
_id: ...,
itemId: 2, // mongodb id
itemCount: 5,
colorId: colorId1
}
{
_id: ...,
itemId: 3, // mongodb id
itemCount: 3,
colorId: colorId2
}
]
}
{
"_id" : ObjectId("6072c2d7ea13fb0338f6cf05"),
"shopId" : "shop3", <- this is mongodb id
"shopItems" : [
{
_id: ...,
itemId: 3, // mongodb id
itemCount: 5,
colorId: colorId1
}
{
_id: ...,
itemId: 1, // mongodb id
itemCount: 3,
colorId: colorId1
}
]
}
我的代码是:
const stores = await Store.aggregate([
{ $match: query },
{ $project: { shopId: 1, tt: { $slice: [ "$shopItems", 3 ] } } },
])
如果商店中不存在具有itemId和colorId的商品,我需要值0
多谢各位 您可以使用下面的聚合查询
db.collection.aggregate([
{
"$match": {} // <-- Highly recommend you to use match due to the complexity of this query
},
{
"$unwind": {
"path": "$shopItems",
}
},
{
"$facet": {
"shopIds": [
{
"$group": {
"_id": null,
"shopIds": {
"$addToSet": "$shopId"
}
}
},
{
"$unwind": {
"path": "$shopIds",
}
},
{
"$sort": {
"shopIds": 1
}
},
{
"$group": {
"_id": null,
"shopIds": {"$push": "$shopIds"}
}
},
],
"docRoot": [
{
"$group": {
"_id": {
"itemId": "$shopItems.itemId",
"colorId": "$shopItems.colorId",
"shopIds": "$shopId",
},
"count": {"$sum": 1}
}
},
{
"$group": {
"_id": {
"itemId": "$_id.itemId",
"colorId": "$_id.colorId",
},
"shopCount": {
"$push": {
"shopId": "$_id.shopIds",
"count": "$count",
}
}
},
},
],
}
},
{
"$unwind": {
"path": "$docRoot",
}
},
{
"$project": {
"itemId": "$docRoot._id.itemId",
"colorId": "$docRoot._id.colorId",
"shopId": "$complete.shopId",
"count": "$complete.count",
"keySwap": {
"$reduce": {
"input": {
"$map": {
"input": {
"$map": {
"input": {
"$concatArrays": [
{
"$map": {
"input": {
"$setDifference": [
{"$arrayElemAt": ["$shopIds.shopIds", 0]},
{
"$map": {
"input": "$docRoot.shopCount",
"as": "elem",
"in": "$$elem.shopId"
}
},
],
},
"as": "elem",
"in": {
"shopId": "$$elem",
"count": 0
},
},
},
"$docRoot.shopCount",
]
},
"as": "elem",
"in": {
"$objectToArray": "$$elem"
}
},
},
"as": "elem1",
"in": {
"$arrayToObject": [
[{
"k": {"$arrayElemAt": ["$$elem1.v", 0]},
"v": {"$arrayElemAt": ["$$elem1.v", 1]},
}]
],
},
},
},
"initialValue": {},
"in": {
"$mergeObjects": ["$$value", "$$this"]
}
},
},
}
},
{
"$replaceRoot": {
"newRoot": {
"$mergeObjects": [
{"itemId": "$itemId", "colorId": "$colorId"},
"$keySwap",
]
}
}
}
], {
"allowDiskUse": true
})
db.collection.aggregate([
{
“$match”:{}//我找到了解决方案,如果您仍在寻找,请ping。@hhharsha36 ping;)@hhharsha36++++++
db.collection.aggregate([
{
"$match": {} // <-- Highly recommend you to use match due to the complexity of this query
},
{
"$unwind": {
"path": "$shopItems",
}
},
{
"$facet": {
"shopIds": [
{
"$group": {
"_id": null,
"shopIds": {
"$addToSet": "$shopId"
}
}
},
{
"$unwind": {
"path": "$shopIds",
}
},
{
"$sort": {
"shopIds": 1
}
},
{
"$group": {
"_id": null,
"shopIds": {"$push": "$shopIds"}
}
},
],
"docRoot": [
{
"$group": {
"_id": {
"itemId": "$shopItems.itemId",
"colorId": "$shopItems.colorId",
"shopIds": "$shopId",
},
"count": {"$sum": 1}
}
},
{
"$group": {
"_id": {
"itemId": "$_id.itemId",
"colorId": "$_id.colorId",
},
"shopCount": {
"$push": {
"shopId": "$_id.shopIds",
"count": "$count",
}
}
},
},
],
}
},
{
"$unwind": {
"path": "$docRoot",
}
},
{
"$project": {
"itemId": "$docRoot._id.itemId",
"colorId": "$docRoot._id.colorId",
"shopId": "$complete.shopId",
"count": "$complete.count",
"keySwap": {
"$reduce": {
"input": {
"$map": {
"input": {
"$map": {
"input": {
"$concatArrays": [
{
"$map": {
"input": {
"$setDifference": [
{"$arrayElemAt": ["$shopIds.shopIds", 0]},
{
"$map": {
"input": "$docRoot.shopCount",
"as": "elem",
"in": "$$elem.shopId"
}
},
],
},
"as": "elem",
"in": {
"shopId": "$$elem",
"count": 0
},
},
},
"$docRoot.shopCount",
]
},
"as": "elem",
"in": {
"$objectToArray": "$$elem"
}
},
},
"as": "elem1",
"in": {
"$arrayToObject": [
[{
"k": {"$arrayElemAt": ["$$elem1.v", 0]},
"v": {"$arrayElemAt": ["$$elem1.v", 1]},
}]
],
},
},
},
"initialValue": {},
"in": {
"$mergeObjects": ["$$value", "$$this"]
}
},
},
}
},
{
"$replaceRoot": {
"newRoot": {
"$mergeObjects": [
{"itemId": "$itemId", "colorId": "$colorId"},
"$keySwap",
]
}
}
}
], {
"allowDiskUse": true
})