简体   繁体   中英

Retrieving and updating array from nested array MongoDB

I am hoping to get some help regarding querying nested documents and arrays in MongoDB as I am new to it and hit a roadblock.

I have a document with the following structure,

{
"_id": {
    "$oid": "5bd0fdfebc54ef73c6b2c841"
},
"name": "Joe",
"email": "joe@test.com",
"password": "$2a$10$6piR5uuLqZxEXbazwHBw7egFYJwnwEs3AYdpJGvlW1/cu6CGj1f8a",
"shows": [
    2790,
    13,
    4,
    1850
],
"ep_info": [
    {
        "show_id": "169",
        "no_of_eps": "62",
        "watched_eps": "1",
        "watched_eps_names": [
            "Pilot"
        ]
    },
    {
        "show_id": 2790,
        "no_of_eps": 39,
        "watched_eps": 0,
        "watched_eps_names": []
    }

]}

I have been trying to find out a way to be able to update the watched_eps and watched_eps_names fields depending on the given email to identify the user first and then the given show_id to find the correct nested array object.

I am sure I am missing something as I have only just started using MongoDB and when I search for similar solutions I don't really see something like this.

Would appreciate any help possible.

You can use below query:

db.getCollection('tests').update({
    "email":"joe@test.com", "ep_info.show_id" : "169"
},
{
    "$set": {"ep_info.$.watched_eps": 10, "ep_info.$.watched_eps_names" : ["Pilot", "abc", "def"]}
})

Before:

/* 1 */
{
    "_id" : ObjectId("5bd0fdfebc54ef73c6b2c841"),
    "name" : "Joe",
    "email" : "joe@test.com",
    "password" : "$2a$10$6piR5uuLqZxEXbazwHBw7egFYJwnwEs3AYdpJGvlW1/cu6CGj1f8a",
    "shows" : [ 
        2790, 
        13, 
        4, 
        1850
    ],
    "ep_info" : [ 
        {
            "show_id" : "169",
            "no_of_eps" : "62",
            "watched_eps" : "1",
            "watched_eps_names" : [ 
                "Pilot"
            ]
        }, 
        {
            "show_id" : 2790,
            "no_of_eps" : 39,
            "watched_eps" : 0,
            "watched_eps_names" : []
        }
    ]
}

After update:

/* 1 */
{
    "_id" : ObjectId("5bd0fdfebc54ef73c6b2c841"),
    "name" : "Joe",
    "email" : "joe@test.com",
    "password" : "$2a$10$6piR5uuLqZxEXbazwHBw7egFYJwnwEs3AYdpJGvlW1/cu6CGj1f8a",
    "shows" : [ 
        2790, 
        13, 
        4, 
        1850
    ],
    "ep_info" : [ 
        {
            "show_id" : "169",
            "no_of_eps" : "62",
            "watched_eps" : 10.0,
            "watched_eps_names" : [ 
                "Pilot", 
                "abc", 
                "def"
            ]
        }, 
        {
            "show_id" : 2790,
            "no_of_eps" : 39,
            "watched_eps" : 0,
            "watched_eps_names" : []
        }
    ]
}

Hope it helps you.

In addition to @Hardik Shah answer :

This will update only the first array element matching show_id criteria. If you can have multiple elements to update in the same array, you have to use positional filtered operator to match all array element that need to be upated.

Here's an example :

db['01'].update(
  {"email": "joe@test.com"},
  {$set:{"ep_info.$[elem].watched_eps":10,"ep_info.$[elem].watched_eps_names":["a","b","c"]}},
  {arrayFilters:[{"elem.show_id":2790}]}
)

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