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
]
Luca Kiebel
  • 9,790
  • 7
  • 29
  • 44
Brother Eye
  • 689
  • 1
  • 11
  • 27
  • 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? – dnickless Sep 18 '18 at 07: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 – Brother Eye Sep 18 '18 at 07:42
  • If you post some sample data (input data and desired result) I'm pretty sure we can find a solution for you. – dnickless Sep 18 '18 at 07:48
  • @dnickless I've added sample data as you said – Brother Eye Sep 18 '18 at 08:19
  • Possible duplicate of [How to Update Multiple Array Elements in mongodb](https://stackoverflow.com/questions/4669178/how-to-update-multiple-array-elements-in-mongodb) – dnickless Sep 18 '18 at 11:31

1 Answers1

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" } })
dnickless
  • 10,733
  • 1
  • 19
  • 34
  • 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 – Brother Eye Sep 18 '18 at 08:57
  • I don't understand what exactly you mean. Can you please post the exact JSON shape of the involved documents? – dnickless Sep 18 '18 at 09:01
  • 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. – Brother Eye Sep 18 '18 at 09:08
  • Check my updated answer. I originally thought you had individual documents per Namecard – dnickless Sep 18 '18 at 10:48
  • Posibly duplicated post https://stackoverflow.com/questions/27874469/mongodb-push-in-nested-array – vitomadio Sep 18 '18 at 11:20
  • @vitomadio: this question is slightly different because he wants to update all elements in the array but, true, that, too, has been asked her lots of times. ;) – dnickless Sep 18 '18 at 11:24