How to insert a key/value to a embedded document in array?
Hi Pluse,
You can add a new field into an embedded document in array using the positional $ operator. The operator identifies an element in an array to update without explicitly specifying the position of the element in the array. Also see Update documents in an array
For example, to add a field ‘number’ into the embedded document array of _id: 'value1':
db.collection.update(
{"device._id":"value1"},
{$set:
{"device.$.number":"value3"}
}
)
Also see $set operator for more information on $set.
how about batch insert?
If you are referring to adding multiple fields with a single update, the same operators apply as the above example. For example:
db.collection.update(
{"device._id":"value1"},
{$set:
{"device.$.number":"value3",
"device.$.string":"value4",
}
}
)
If you are referring to bulk operations, please see bulk.insert() to perform a list of insert operations.
Regards,
Wan.