Watch keynotes and sessions from MongoDB.live, our virtual developer conference.

I need help to query nested arrays

I want to updateOne Document. I need 2 filters to find ("_id", And the “_id” in the Display) and set as shown below.

From:

{
        "_id" : ObjectId("5edfced6c677ea092ddf1f4a"),
        "email" : "rupertbogensperger21@gmail.com",
        "lastuserip" : "0:0:0:0:0:0:0:1",
        "userlaw" : 1,
        "validationhash" : null,
        "registered" : ISODate("2020-06-09T18:03:02.259Z"),
        "_lc" : ISODate("2020-06-11T15:17:49.986Z"),
        "password" : "b1d56015b4280e2a08c45943631635cd74d0e1e00f9db38a0e63c817dbd2816a5f7fd72a5b7b7baceaedc604b07b9a18be4eb61b4c4583f9681106ebec324a6a",
        "lastlogin" : ISODate("2020-06-11T15:17:39.777Z"),
        "Name" : {
                "lastname" : "Bogensperger",
                "surname" : "Rupert"
        },
        "birthdate" : ISODate("1978-09-29T23:00:00Z"),
        "Display" : [
                {
                        "_id" : ObjectId("5ee24b30c4b930419306be17"),
                        "Size" : {
                                "height" : 0,
                                "lenght" : 0,
                                "width" : 0
                        },
                        "type" : 1
                }
        ],
        "image" : "20200611171749984ff26a4252.png"
}

To:

{
        "_id" : ObjectId("5edfced6c677ea092ddf1f4a"),
        "email" : "rupertbogensperger21@gmail.com",
        "lastuserip" : "0:0:0:0:0:0:0:1",
        "userlaw" : 1,
        "validationhash" : null,
        "registered" : ISODate("2020-06-09T18:03:02.259Z"),
        "_lc" : ISODate("2020-06-11T15:17:49.986Z"),
        "password" : "b1d56015b4280e2a08c45943631635cd74d0e1e00f9db38a0e63c817dbd2816a5f7fd72a5b7b7baceaedc604b07b9a18be4eb61b4c4583f9681106ebec324a6a",
        "lastlogin" : ISODate("2020-06-11T15:17:39.777Z"),
        "Name" : {
                "lastname" : "Bogensperger",
                "surname" : "Rupert"
        },
        "birthdate" : ISODate("1978-09-29T23:00:00Z"),
        "Display" : [
                {
                        "_id" : ObjectId("5ee24b30c4b930419306be17"),
                        "Size" : {
                                "height" : 0,
                                "lenght" : 0,
                                "width" : 0
                        },
                        "Image" : [{"name": "test"}]
                        "type" : 1
                }
        ],
        "image" : "20200611171749984ff26a4252.png"
}

:wave:

Hi @Rupert_Bogensperger and welcome to the community forums.

A couple tips to make it easier for people to help you.

  1. When posting code blocks, please use three backticks ``` on a like by themselves, Paste code on the next line and then place three more backticks all by themselves after the last line of code. Try to keep the original whitespace as well as this will make the code much easier to read. The following is much easier to glance at and see the structure of the document than the formatting in your original post.
{
  “_id” : ObjectId(“5edfced6c677ea092ddf1f4a”),
  “email” : “rupertbogensperger21@gmail.com”,
  “lastuserip” : “0:0:0:0:0:0:0:1”,
  “userlaw” : 1,
  “validationhash” : null,
  “registered” : ISODate(“2020-06-09T18:03:02.259Z”),
  “_lc” : ISODate(“2020-06-11T15:17:49.986Z”),
  “password” : “b1d56015b4280e2a08c45943631635cd74d0e1e00f9db38a0e63c817dbd2816a5f7fd72a5b7b7baceaedc604b07b9a18be4eb61b4c4583f9681106ebec324a6a”,
  “lastlogin” : ISODate(“2020-06-11T15:17:39.777Z”),
  “Name” : {
    “lastname” : “Bogensperger”,
    “surname” : “Rupert”
  },
  “birthdate” : ISODate(“1978-09-29T23:00:00Z”),
  “Display” : [
    {
      “_id” : ObjectId(“5ee24b30c4b930419306be17”),
      “Size” : {
          “height” : 0,
          “lenght” : 0,
          “width” : 0
      },
      “type” : 1
    }
  ],
  “image” : “20200611171749984ff26a4252.png”
}
  1. Instead of saying I want this turned into that, please state explicitly what you’re adding updating or removing from the document. This will make it so someone doesn’t have to perform a diff on the code to see what changed. While having the two documents shows explicitly what you’re wanting, I had to copy and paste both and do a diff on them to see that you wanted to add a new field to the document in the Display array.

As for helping you get to where you want to get to with your code, you want to look at the $ positional operator.

2 Likes