Add more elements to an array with an object ID

Hi,

I am new designing DBs with Mongo.

I am creating a Db in Mongo, not sure if my schema is designed correctly, I need some help.

I have some arrays inside a document that I need them to reference other documents from the DataBase- I am doing this reference via Object ID. Despite, I would like to know if it is possible to add more element on arrays where object IDs are present.

For example:

image

As you may see, “domicilio” has several elements embeded inside of it. I tried to do that on the case of diagnosis, “alergias”, “Citas”. For example something like this:

"Alergias:" [
     {
     "$oid": "5fd83705fc9e4a97dc261a68"
    "Tipo": "Food Allergie".
    "Date": 2020-01-24 ...
     }, 

     {
     "$oid": "5fd83705fc9e4a98yc261a70"
      "Tipo": "Another Type".
    "Date": 2020-01-30...
     }
]

Is it possible to create such an arrangement as the one above? Please help, I tried the method above but I was not successful. Currently using the Compass GUI.

Regards,

Hi @Luis_Guzman,

Of course its possible to add or save rich embedded docs in an array.

I think your main problem is with the extended json represtation of the objectId ($oid). It cannot be used that way in a complex object as you specify it as a field name rather than a type. Please try:

“Alergias” : [
{
"id" : { “$oid”: “5fd83705fc9e4a97dc261a68”},
“Tipo”: “Food Allergie”,
“Date”: 2020-01-24 …
},

 {
"id" : { "$oid": "5fd83705fc9e4a98yc261a70"},
  "Tipo": "Another Type",
"Date": 2020-01-30...
 }
]

Also try to use compass field editor rather than free text.

Best
Pavel

1 Like

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.