MongoDB- how to use an array items?

Database Structure:

[
 {
  "_id": "12",
  "title": "Vanella Icream",
  "contain":"sugar",
  "details": [
      {
        "flavour": "Vanella"
      },
      {
        "weight": "6KG"
      }
   ]
 },
 {
  "_id": "3",
  "title": "Pretzels",
  "contain":"salt",
  "details": [
      {
        "flavour": "Wheat"
      },
      {
        "weight": "2KG"
      }
   ]
 }
]

Above database, contains an array details

  "details": [
      {
        "flavour": "Vanella"
      },
      {
        "weight": "6KG"
      }
   ]

How to get the flavour and weight to use in $addToSet and $group in my Aggeration?

Like this:

  '$group': {
    '_id': 1,
    'flavour': {
   '$addToSet': '$flavour'
   },
   'weight': {
   '$addToSet': '$weight'
  }
}

after that my expected output

{
      "_id": 0,
      "flavour": [
        "Vanella",
        "Wheat",
      ],
      "weight": [
        "12KG",
        "6KG",
      ]
}

How do I do this for an array? Anyone Knows ??

Hello @Vishal_Sharma.
Do you only want array of flavor and weight? not required title and contain to be included in the output.

btw could you let me know me which MongoDB version you are using?

Yes, i don’t want title or contain

Hi @HomAskIe THis is the reference question what I want. javascript - MongoDB - Structure an array without using key field in Aggregration - Stack Overflow

Hello @Vishal_Sharma . Your case is interesting. I had to think for a white to figure out a good pipeline for it.
Here try this out: Mongo playground
It’s not 100% great! but I think it’s shorter than your second approach.

Let me know how it turn out for you!

@Vishal_Sharma My bad. I copied wrong link

Try this one: Mongo playground

1 Like

@Vishal_Sharma If this helps you, please consider mark this as the solution :slight_smile: Thank you

yes. @HomAskIe You did it very right !!

You need to change the schema, the thing you want to do is easy, and both those queries are so complicated and slow, even the second that is much smaller has 2 $unwind and 3 $group with 3 $arrayToObject and 8 stages total because of the schema and the schema of the answer.

Don’t store data in the keys of the documents, people that are new to MongoDB do those, i was doing it also, but it makes all things harder.(i can’t say like never do it but you dont need it here)

Your schema should be something like

{
    "_id": "2",
    "title": "Pretzels",
    "contain": "salt",
    "details": [
      {
        "type" : "flavour",
        "value" : "Wheat"
      },
      {
        "type" : "weight",
        "value" : "10KG"
      },
      {
        "type" : "sugar",
        "value" : "15KG"
      }
    ]
  }

See this example

Converts your schema, to the new schema and produce the results you
want but without data in keys (the first part you wouldnt need it you would need only the bellow query if you had that schema from start)

Query with the new Schema (no data in keys)

[{"$unwind": { "path": "$details"}},
  {"$replaceRoot": {"newRoot": "$details"}},
  {
    "$group": {
      "_id": {
        "type": "$type",
        "value": "$value"
      },
      "sum": {"$sum": 1}
    }
  },
  {
    "$replaceRoot": {
      "newRoot": {"$mergeObjects": ["$_id","$$ROOT"]}
    }
  },
  {"$project": {"_id": 0}},
  {
    "$group": {
      "_id": "$type",
      "values": {
        "$push": {
          "value": "$value",
          "sum": "$sum"
        }
      }
    }
  },
  {"$addFields": {"type": "$_id"}},
  {"$project": {"_id": 0}}
]

MongoDB operators are not made to support for data in keys or dynamic keys(uknown keys) (to do it you do complicated things like the above)

If you want to change your schema, either do it with update in the database,
Or take the documents to the application and do it with javascript, and re-insert.

Even if you solve this question in the next one, you will have again problems.

I agree with @Takis. The aggregation will be much more easier if you store you data that way.
Although the pipeline works but it will be memory consuming.
You’re encourage to update the schema. If you have problems with it just ask for help
@Vishal_Sharma

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.