Add a field inside an objectid

Like mentioned in at least one of your other post (Aggregate with filters - #2 by steevej ),

Please update your post after reading
Formatting code and log snippets in posts
If you are including code and log snippets in a post, consider if there are ways to make these more readable using some of the available formatting options. Well formatted posts will help other users understand and engage with your discussion. Some quick tips for improved formatting: Include short code and log snippets as inline text rather than images. Text is searchable for other users who might encounter a similar issue or need the exact text to help investigate the issue you are experien…

While at it you may mark turivishal’s answer as the solution of your issue in

Your collaboration will be appreciated by all.

Now with your current issue at hand.

Please provide more details about which documents. Top level documents of the collection or documents inside the productos array? If not all documents of either please indicate what is the criteria you want to limit the documents for which to apply.

For example:

db.a.updateMany( { "productos.idOrigen" : 8 } ,
    { "$set" : { "productos.$.new_oid" : new ObjectId() }})

will add the field new_oid as an object id to the first element of productos array of all the top level documents which as the field idOrigen equals 8. Top documents will then looks like


{ _id: ObjectId("66323759fabac22b5c9aa33e"),
  productos: [ { idOrigen: 8, new_oid: ObjectId("663237d0fabac22b5c9aa33f") } ] }
2 Likes