4

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"})

2 Answers 2

16

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
)
Sign up to request clarification or add additional context in comments.

4 Comments

This does the trick. Haven't used the positional operator before. A small type though, {"multi": true}, but yes, this solves it.
How can I update the fields in a bulk? ex - users.email, users.address, users.first, etc. In this case I have a users object. I do not want to mention each field. I want something like {$set: {"users.$.": users}}
@VikasChauhan, you can achieve that using {$set: {"users.$": user}}. i.e user is entire user object
@JyothiBabuAraja thanks, I tried this too but not working. I found a way how to do it.
0

You can do this using updateMany function along with arrayFilter option

Home.updateMany(
{'users.name': 'johnk'}, //matches the condition to fetch documents
{ $set: { 'users.$[elem].name': 'johndoe' }}, //updates matched documents
{ arrayFilters: [{ 'elem.name': 'johnk' }] //filter documents to update
})

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.