简体   繁体   中英

MongoDB nested array aggregation correctly

Ok so i've tired many things to solve this issue, what im facing with is, i have this document

{
    "_id": "5ef9c6a9691c33e13c455413",
    "tags": [
      "chicken wings",
      "pizza",
      "burgers"
    ],
    "shopName": "Emily",
    "isOpen": true,
    "address": "919 Fulton St, Brooklyn, NY 11238",
    "image": "random-2.jpg",
    "category": "Asian",
    "stars": [
      12,
      22,
      32,
      0,
      0
    ],
    "logo": "logo.png",
    "phone": "+96501234567",
    "totalCompletedOrders": 22,
    "eta": "60",
    "products": [
      {
       ...
        "category": "Appeteasers"
      },
      {
       ...
        "category": "Appeteasers"
      },
      {
        ...
        "category": "Appeteasers"
      },
      {
        ...
        "category": "Appeteasers"
      },
      {
       ...
        "category": "Appeteasers"
      },
      {
       ...
        "category": "Appeteasers"
      },
      {
        ...
        "category": "Appeteasers"
      },
      {
        ...
        "category": "Fino sides"
      },
      {
        ...
        "category": "Fino sides"
      },
      {
        ...
        "category": "Fino sides"
      },
      {
        ...
        "category": "Fino sides"
      },
      {
       ...
        "category": "Peri-peri chicken"
      },
      {
        ...
        "category": "Peri-peri chicken"
      },
      {
        ...
        "category": "Peri-peri chicken"
      },
      {
        ...
        "category": "Peri-peri chicken"
      },
      {
        ...
        "category": "Peri-peri chicken"
      },
      {
        ...
        "category": "Peri-peri chicken"
      },
      {
        ...
        "category": "Peri-peri chicken"
      },
      {
        ...
        "category": "Peri-peri chicken"
      },
      {
        ...
        "category": "Peri-peri chicken"
      },
      {
        ...
        "category": "Peri-peri chicken"
      },
      {
        ...
        "category": "Peri-peri chicken"
      },
      {
        ...
        "category": "Peri-peri chicken"
      },
      {
        ...
        "category": "Peri-peri chicken"
      },
      {
        ...
        "category": "Peri-peri chicken"
      },
      {
        ...
        "category": "Peri-peri chicken"
      },
      {
       ...
        "category": "Peri-peri chicken"
      },
      {
        ...
        "category": "Peri-peri chicken"
      }
    ]
  }

so my issue is i need to group the products array and make it look into something like this or close to it

{
    "_id": "5ef9c6a9691c33e13c455413",
    "tags": [
      "chicken wings",
      "pizza",
      "burgers"
    ],
    "shopName": "Emily",
    "isOpen": true,
    "address": "919 Fulton St, Brooklyn, NY 11238",
    "image": "random-2.jpg",
    "category": "Asian",
    "stars": [
      12,
      22,
      32,
      0,
      0
    ],
    "logo": "logo.png",
    "phone": "+96501234567",
    "totalCompletedOrders": 22,
    "eta": "60",
    "products": [{
        category: "Appeteasers",
        products:[...all Appeteasers chicken here]
      },{
        category: "Fino sides chicken",
        products:[...all Fino sides chicken here]
      },{
        category: "Peri-peri chicken",
        products:[...all Peri-peri chicken here]
     }]
  }

what i've tired is


Store.aggregate([
    {$match:{ _id: id}, },
    { $unwind: "$products" },
    {
      $group : {
        _id: "$products.category",
        "products" : {"$addToSet": "$products"},
        
      }
    },
  ]);

and i got so close to the result but i the root fields are not included

[
  {
    "_id": "Peri-peri chicken",
    "products": [...]
  },
  {
    "_id": "Appeteasers",
    "products": [...]
  },
  {
    "_id": "Fino sides",
    "products": [...]
  }
]


would love some help, thank you

To get all other keys in the document you have mention the key in $project , then only they will be accessible. In below code, I have just added three of them to show how it has to be done. You can add rest of them.

Store.aggregate([
{
    $match:{ _id: id}
},
{
    $project: { products: "$products", shopName: "$shopName", stars: "$stars", tags: "$tags", }
},
{
    $unwind: "$products" 
},
{
    $group : {
        _id: "$products.category",
        "products" : {"$addToSet": "$products"},
        "tags": {"$addToSet": "$tags"},
        "stars": {"$addToSet": "$stars"},
        "shopName": {"$first": "$shopName"}
    }
},
{
    $project: {products: "$products", tags: "$tags", shopName: "$shopName", stars: "$stars" }
},
]);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM