These are chat archives for Automattic/mongoose

18th
Dec 2017
Kannan T
@kannan007
Dec 18 2017 11:14
Hi
can someone helpme out here?
struck with past 30 mins
sorry 3 hours
Karthi
@Karthi-SRV
Dec 18 2017 11:16
you need to find indexed data?
Kannan T
@kannan007
Dec 18 2017 11:18
yes bro
ank26oct
@ank26oct
Dec 18 2017 12:03
hello everyone. I am new to play framework and mongodb.How to connect and access mongodb using scala?
Karthi
@Karthi-SRV
Dec 18 2017 18:39

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