0

Here I want to update 1 field for all elements in an array of objects. How to update that field with only one query without looping and updating each of the elements or scanning the whole database and matching?

My model schema:

let NameCardSchema = mongoose.Schema({
    fullname: {
        type: String,
        required: true
    },
    occupation: {
        type: String,
        default: null
    },
    company: {
        type: String,
        default: null,
    },
    position: {
        type: String,
        default: null,
    },
    userID: [String] // field to update
});

Sample data:

User:
    _id:5b8e4c6a879ac54ee0f30bb3
    username:"Duc"

Namecard array:
[
    namecard1:
        _id:5b9615c6b157af5afc8ce426
        occupation:null
        company:"KIS"
        position:"Developer"
        fullname:"Duc Nguyen Trung"
        userID:"5b9a173f0749c52b583818ec"

    namecard2:
        _id:5b96162fb157af5afc8ce428
        occupation:null
        company:"KIS"
        position:"Developer"
        fullname:"Hieu Vuong"
        userID:"5b9a16700749c52b583818ea"
]

Expected result:

[
    namecard1:
        _id:5b9615c6b157af5afc8ce426
        occupation:null
        company:"KIS"
        position:"Developer"
        fullname:"Duc Nguyen Trung"
        userID:"5b9a173f0749c52b583818ec", "5b8e4c6a879ac54ee0f30bb3" // username Duc's ID added

    namecard2:
        _id:5b96162fb157af5afc8ce428
        occupation:null
        company:"KIS"
        position:"Developer"
        fullname:"Hieu Vuong"
        userID:"5b9a16700749c52b583818ea", "5b8e4c6a879ac54ee0f30bb3" // username Duc's ID added
]
5
  • Could you please share some sample data and tell us which field you actually want to change in what way? Also, what have you tried? Commented Sep 18, 2018 at 7:21
  • @dnickless I want to append new userID data to the userID field. All I can think of is just loop and scan mentioned above Commented Sep 18, 2018 at 7:42
  • If you post some sample data (input data and desired result) I'm pretty sure we can find a solution for you. Commented Sep 18, 2018 at 7:48
  • @dnickless I've added sample data as you said Commented Sep 18, 2018 at 8:19
  • Possible duplicate of How to Update Multiple Array Elements in mongodb Commented Sep 18, 2018 at 11:31

1 Answer 1

1

In order to add a new entry into the userID array of all documents just execute this:

db.collection.updateMany({ /* no filter */ }, { $push: { "Namecards.$[].userID": "5b8e4c6a879ac54ee0f30bb3" } })
Sign up to request clarification or add additional context in comments.

6 Comments

Yes, I know this. But I want to update the documents belong to the elements of the object array only, not all documents of the collection
I don't understand what exactly you mean. Can you please post the exact JSON shape of the involved documents?
I mean I have an array of NameCard object with 2 elements as the sample above, and I just want to update their userID field of those elements only. And I want to do it with some kind of query, not looping the whole array or scanning the whole database to match their _id. I wonder if there are any kinds of query for this kind of stuff.
Check my updated answer. I originally thought you had individual documents per Namecard
|

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.