4

I have one big json document in collection like this:

{
  "_id": {
    "$oid": "5e7a50e7f1d41b3ea05aa46e"
  },
  "response": {
    "count": 2,
    "items": [
      {
        "id": 1,
        "first_name": "Unknown",
        "last_name": "Unknown",
        "is_closed": false,
        "can_access_closed": true,
        "photo": "1.jpg",
        "track_code": "d9e2ca2eG4GbHAQSwz8Ne4WBiVx"
      },
      {
        "id": 2,
        "first_name": "Lorem",
        "last_name": "Ipsum",
        "is_closed": false,
        "can_access_closed": true,
        "photo": "2.jpg",
        "track_code": "23f1219a7j1xyWba69jz7p"
      }
    ]
  }
}

How I can split "items" by individual objects (for example by id in items) in the collection? As a result I want to have something like:

Object #1

{
  "_id": {
    "$oid": "5e7a50e7f1d41b3ea05aa46e"
  },
  "id": 1,
  "first_name": "Unknown",
  "last_name": "Unknown",
  "is_closed": false,
  "can_access_closed": true,
  "photo": "1.jpg",
  "track_code": "d9e2ca2eG4GbHAQSwz8Ne4WBiVx"
}

Object #2

{
  "_id": {
    "$oid": "ae2a40e7ffd41b3ea05aa46e"
  },
  "id": 2,
  "first_name": "Lorem",
  "last_name": "Ipsum",
  "is_closed": false,
  "can_access_closed": true,
  "photo": "2.jpg",
  "track_code": "23f1219a7j1xyWba69jz7p"
}

I can’t understand how to look for this in the documentation.

3
  • You should be able to do this with aggregation, have you tried anything? Commented Mar 24, 2020 at 20:46
  • Do you just want to split array into objects or those objects as new docs ? Can you provide required o/p after splitting Commented Mar 24, 2020 at 21:02
  • Updated the post. I want to split array into objects in the collection. I will check what can I do with aggregation, thanks Commented Mar 24, 2020 at 21:17

1 Answer 1

7

You can try below query, with this your result can have more no.of docs than actual number in collection as we're exploding items array :

db.collection.aggregate([
  /** Adds '_id' field to each object inside items array, this can be done after 'unwind',
   *  if it's done after 'unwind' no.of docs to be iterated in 'unwind' is more, So better be done as first stage*/
  {
    $addFields: {
      "response.items._id": "$_id"
    }
  },
  /** Unwind items array, will exclude docs where items is not an array/doesn't exists */
  {
    $unwind: "$response.items"
  },
  /** Replace 'response.items' object as new root(document) */
  {
    $replaceRoot: {
      newRoot: "$response.items"
    }
  }
])

Test : MongoDB-Playground

Ref : aggregation-pipeline

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.