1

Hello and sorry for the title, it is quite hard to explain with words.

I have data with multiple levels in the mongo collection.

I am trying to insert a value inside the maps array which is already inside favorite_lists array of objects. Below is the user collection schema:

{
    "_id" : ObjectId("60f3fdae2367163726c6716b"),
    "username" : "Johnny65",
    "first_name" : "Thaddeus",
    "last_name" : "Lueilwitz",
    "email" : "[email protected]",
    "mobile" : "834XXXX",
    "profile_photo" : "http://placeimg.com/640/480",
    "login_type" : 2,
    "maps_created" : ISODate("2020-04-08T20:00:28.453Z"),
    "created_date" : ISODate("2021-06-24T16:19:07.010Z"),
    "favorite_lists" : [
        {
            "maps" : [ ],
            "name" : "name 1",
            "created_date" : ISODate("2021-07-20T13:27:56.050Z")
        },
        {
            "maps" : [ ],
            "name" : "name 2",
            "created_date" : ISODate("2021-07-20T13:31:21.985Z")
        },
        {
            "maps" : [ ],
            "name" : "name 3",
            "created_date" : ISODate("2021-07-20T13:35:32.959Z")
        }
    ]
}

I want to insert value inside maps array where favorite_lists.name = "name 2"

Expected Result

{
        "_id" : ObjectId("60f3fdae2367163726c6716b"),
        "username" : "Johnny65",
        "first_name" : "Thaddeus",
        "last_name" : "Lueilwitz",
        "email" : "[email protected]",
        "mobile" : "834XXXX",
        "profile_photo" : "http://placeimg.com/640/480",
        "login_type" : 2,
        "maps_created" : ISODate("2020-04-08T20:00:28.453Z"),
        "created_date" : ISODate("2021-06-24T16:19:07.010Z"),
        "favorite_lists" : [
            {
                "maps" : [ ],
                "name" : "name 1",
                "created_date" : ISODate("2021-07-20T13:27:56.050Z")
            },
            {
                "maps" : ["value 1", "value 2" ],
                "name" : "name 2",
                "created_date" : ISODate("2021-07-20T13:31:21.985Z")
            },
            {
                "maps" : [ ],
                "name" : "name 3",
                "created_date" : ISODate("2021-07-20T13:35:32.959Z")
            }
        ]
    }

2 Answers 2

1

You can use $ positional with $addToSet to append elements in array and $each for insert multiple elements,

db.collection.updateOne({
  "favorite_lists.name": "name 2"
},
{
  $addToSet: {
    "favorite_lists.$.maps": {
      $each: [
        "value 1",
        "value 2"
      ]
    }
  }
})

Playground

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

Comments

1

You can use positional operator $ into an update to modify the value you want.

yourModel.update({
  "favorite_lists.name": "name 2"
},
{
  "$push": {
    "favorite_lists.$.maps": {
      "$each": [
        "value 1",
        "value 2"
      ]
    }
  }
})

This query says to mongo: "Find the document where exists "favorite_lists.name": "name 2" and push inside favorite_lists.$.maps (nothe how $ tells mongo which document inside array upadte) each value from the array".

Example here

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.