David
David

Reputation: 733

Add new field to an Array of type object to a collection in MongoDB

See example below (json format from mongochef). I need to add a new field to each element in our collection called isReadOnly (bool). How would I do this? I can easily add it to the root of the document but battling to do it within each array element.

{ 
"_id" : "fda42f22-0fa4-4e4a-94bf-245b1124c8b9", 
"_t" : "FullConfiguration", 
"DeviceConfigName" : "Illinois", 
"Elements" : [
    {
        "_t" : "NumberConfigurationElement", 
        "AsciiConfigNumber" : NumberInt(10), 
        "DataType" : NumberInt(1), 
        "ByteOffset" : NumberInt(0), 
        "Name" : "alcohol_limit", 
        "Description" : "The alcohol limit of the device (ug/l)", 
        "AckResponse" : "HT-CONFG,010,00119", 
        "DevicePart" : NumberInt(1), 
        "Value" : NumberInt(119), 
        "DefaultValue" : NumberInt(119), 
        "Min" : NumberInt(50), 
        "Max" : NumberInt(500)
    }, .....

Upvotes: 0

Views: 168

Answers (3)

Saeed Khan
Saeed Khan

Reputation: 3

@David I've working solution with MongoDB 3.6+.

db.collection.update({},
    { $set: { "Elements.$[].isReadOnly": false } }, { multi: true }
)

This command will add isReadOnly = false field in all Element array field of documents.

The $position modifier specifies the location in the array at which the $push operator inserts elements. Without the $position modifier, the $push operator inserts elements to the end of the array. See $push modifiers for more information.

More details you can find on position operator docs

Upvotes: 0

guymaor86
guymaor86

Reputation: 466

You can do it like this(if your collection is called 'coll'):

db.coll.find().forEach(function(e) {
    var t = e.Elements;
    t.forEach(function(e) {
        e.isReadOnly=false
    });
    db.coll.update({ _id : e._id }, { $set: { Elements : t } } );
})

Upvotes: 2

dyouberg
dyouberg

Reputation: 2332

@Styvane , this is what I came up with, it still seems a bit hacky.

var query = {
    Elements: {
        $elemMatch: {
            _t: { $exists: true },
            isReadOnly: { $exists: false }
        }
    }
};

while (db.collection.find(query).count() > 0) {
    db.collection.update(
        query, 
        { $set: { "Elements.$.isReadOnly": true } }
    );    
}

Upvotes: 0

Related Questions