Add a field inside an objectid

Hello.
I would like to know if it is possible to add a field inside an object, which is of type objectid and is random.

Example:
{
_id: ObjectId(‘65d4d03f8b966253bf4e3f9f’),
idCaja: Long(‘01’),
idEstatus: Long(‘1’),
nombreCaja: ‘Caja 1’,
idEstatus: Long(‘1’),
productos: [
{
new random objectid type field
codigoTipo:‘A’,
idOrigen: 08,
nombre: ‘mesa’,
precio: 1000
}
]
}

It would apply to n number of documents.

I would greatly appreciate your support.
Greetings.

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

Hello,

In this case I do not need to filter since I would like to add a new unique and different objectid to each of the documents, within the “product” array even if it contains more than one object:

With the provided script I share an example of the result:

[
  {
    _id: ObjectId('66340d52b5896b0ea5eddpl7'),
    idEstatus: Long('1'),
    idTipoVenta: 2,
    productos: [
      {
        codigoTipo: 'C',
        idOrigen: 2,
        nombre: 'Producto 2',
        precio: 123.12,
        new_id: ObjectId('66340da3b5896b0ea5eddcf7')
      }
    ]
  },
  {
    _id: ObjectId('66340d52b5896b0ea5eddyy7'),
    idEstatus: Long('1'),
    idTipoVenta: 2,
    productos: [
      {
        codigoTipo: 'C',
        idOrigen: 2,
        nombre: 'Silla',
        precio: 131.23,
        new_id: ObjectId('66340da3b5896b0ea5eddcf7')
      },
      {
        codigoTipo: 'C',
        idOrigen: 2,
        nombre: 'Mesa',
        precio: 250
      },
      {
        codigoTipo: 'C',
        idOrigen: 2,
        nombre: 'Ropero',
        precio: 5000
      }
    ]
  },
  {
    _id: ObjectId('66340d52b5896b0ea5eddss7'),
    idEstatus: Long('1'),
    idTipoVenta: 2,
    productos: [
      {
        codigoTipo: 'C',
        idOrigen: 2,
        nombre: 'Producto 1',
        precio: 1231.23,
        new_id: ObjectId('66340da3b5896b0ea5eddcf7')
      }
    ]
  },
  {
    _id: ObjectId('66340d52b5896b0ea5edddd6'),
    idEstatus: Long('1'),
    idTipoVenta: 2,
    productos: [
      {
        codigoTipo: 'C',
        idOrigen: 2,
        nombre: 'Producto 3',
        precio: 0.11,
        new_id: ObjectId('66340da3b5896b0ea5eddcf7')
      }
    ]
  }
]
);

I would like this ObjectId to be different for each object and be assigned to all of them since some are left empty in the example of the array that contains 3 objects.

Thanks for your help.

@edith_t, you still did not mark turivishal’s answer as the solution in your other thread.

Indicating that a thread is resolve and which post is the solution is the best way to keep this useful by providing confidence to others facing the same issue that you did.

To modify all elements of an array you need to use $map.

1 Like