Find by Sub Document Id and update its data

377 views
Skip to first unread message

kart...@calibraint.com

unread,
Dec 18, 2017, 3:14:23 PM12/18/17
to mongodb-user
Find by Sub Document by multiple Id and update its data.
Below example says that find the data by grades.id and update its corresponding mean value to 100.

MongoDB Version : 3.4 and 3.6 

**Update subdocument by subdocument Id - Array of Subdocument ID**

Schema: 

    {
        "_id" : 1,
        "grades" : [ 
            {
                "id" : 1,
                "grade" : 80,
                "mean" : 75,
                "std" : 6
            }, 
            {
                "id" : 2,
                "grade" : 85,
                "mean" : 90,
                "std" : 4
            }, 
            {
                "id" : 3,
                "grade" : 85,
                "mean" : 85,
                "std" : 6
            }
        ]
    }

Query: 

    db.students.update(
        {_id: 1},
        { $set: { "grades.$[elem].mean" : 100 } },
        {
            multi: true,
            arrayFilters: [ { "elem._id": { $in: [1, 2] } } ]
        }
    )

OutPut
 
    {
        "_id" : 1,
        "grades" : [ 
            {
                "id" : 1,
                "grade" : 100,
                "mean" : 75,
                "std" : 6
            }, 
            {
                "id" : 2,
                "grade" : 100,
                "mean" : 90,
                "std" : 4
            }, 
            {
                "id" : 3,
                "grade" : 85,
                "mean" : 85,
                "std" : 6
            }
        ]
    }

I have tried above query but its fail

Kevin Adistambha

unread,
Jan 7, 2018, 10:49:26 PM1/7/18
to mongodb-user

Hi

I believe your question was answered in https://stackoverflow.com/questions/47874605/find-by-sub-document-id-and-update-its-data, where the posted answer is correct:

  1. The arrayFilters part of your query is incorrect, it should be elem.id instead of elem._id: {arrayFilters: [ {'elem.id': {$in: [1,2]}}]}
  2. arrayFilters is a new MongoDB 3.6 feature, so the query won’t work with versions lower than 3.6. See db.collection.update() for more details.

The query as posted works in MongoDB 3.6, once the elem.id term is fixed:

> db.test.find()
{
  "_id": 1,
  "grades": [
    {
      "id": 1,
      "grade": 80,
      "mean": 75,
      "std": 6
    },
    {
      "id": 2,
      "grade": 85,
      "mean": 90,
      "std": 4
    },
    {
      "id": 3,
      "grade": 85,
      "mean": 85,
      "std": 6
    }
  ]
}

> db.test.update({_id:1}, {$set:{'grades.$[elem].mean': 100}}, {arrayFilters: [ {'elem.id': {$in: [1,2]}} ]})
WriteResult({
  "nMatched": 1,
  "nUpserted": 0,
  "nModified": 1
})

> db.test.find()
{
  "_id": 1,
  "grades": [
    {
      "id": 1,
      "grade": 80,
      "mean": 100,
      "std": 6
    },
    {
      "id": 2,
      "grade": 85,
      "mean": 100,
      "std": 4
    },
    {
      "id": 3,
      "grade": 85,
      "mean": 85,
      "std": 6
    }
  ]
}

Best regards
Kevin

Reply all
Reply to author
Forward
0 new messages