Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Update multiple documents by array of objects field value in mongoose

Here is my Schema.

 Home = new Schema({
        name: { type: String, required: true},
        description: {type: String},
        administrator: {type : mongoose.Schema.Types.ObjectId, ref: 'User', required: true},

    users: [{ 
        _id: {type : mongoose.Schema.Types.ObjectId, ref: 'User'},
        email: {type: String},
        name: { type: String},
        status: { type: Number}
    }],
    rooms: [Room]


});
module.exports = mongoose.model('Home', Home);

If I want to find a specific user across multiple documents, I could do

Home.find({"users.email": "[email protected]"},{users:1})

This would return all homes with users.email = [email protected]

But the user's field is an array.

"users" : [
        {
            "status" : 0,
            "name" : "Yaknow",
            "email" : "[email protected]",
            "_id" : ObjectId("5875a42ea469f40c684de385")
        },
        {
            "status" : 1,
            "name" : "johnk",
            "email" : "[email protected]",
            "_id" : ObjectId("586e31c6ce07af6f891f80fd")
        }
    ]

(this is just from one home, if there are many homes, there will be many arrays like this) So the above query will return each user array of each home the user has email in. My question is, how do I update all instances of that JohnK into JohnKirtster? Using this would update every user in the array's name to JohnKirster

Home.update(query, {users.name: "JohnKirster"})

like image 466
TradeRaider Avatar asked Dec 03 '22 23:12

TradeRaider


1 Answers

You can use mongodb $ positional operator by setting multi:true

Home.update(
 {"users.name": "johnk"}, //query, you can also query for email
 {$set: {"users.$.name": "JohnKirster"}},
 {"multi": true} //for multiple documents
)
like image 70
Jyothi Babu Araja Avatar answered May 06 '23 07:05

Jyothi Babu Araja