Add document subdocuments - Mongoose

Asked

Viewed 293 times

1

Good evening! I have the following document:

{ 
    "_id" : ObjectId("5b21a4332a5e3333cc64a12f"), 
    "nome" : "João", 
    "sobrenome" : "Barbosa", 
    "cpf" : "12345678910", 
    "senha" : "Fabet@1010", 
    "viagem" : [
        {
            "empresa_origem" : "BRF", 
            "cidade_origem" : "Concórdia", 
            "uf_origem" : "SC", 
            "pais_origem" : "Brasil", 
            "empresa_destino" : "Santiago Foods", 
            "cidade_destino" : "Santiago", 
            "uf_destino" : "CH", 
            "pais_destino" : "Chile", 
            "peso" : "20000", 
            "carga" : "Queijo", 
            "placa_caminhao" : "MMM-9999", 
            "_id" : ObjectId("5b21a4332a5e3333cc64a130")
        }
    ], 
    "__v" : NumberInt(0)
}

Note that there is the vaige subdocument.

In the example above, there is the registration of a user and a trip. I would like to register more trips on this same user, as follows:

{ 
    "_id" : ObjectId("5b21a4332a5e3333cc64a12f"), 
    "nome" : "João", 
    "sobrenome" : "Barbosa", 
    "cpf" : "12345678910", 
    "senha" : "Fabet@1010", 
    "viagem" : [
        {
            "empresa_origem" : "BRF", 
            "cidade_origem" : "Concórdia", 
            "uf_origem" : "SC", 
            "pais_origem" : "Brasil", 
            "empresa_destino" : "Santiago Foods", 
            "cidade_destino" : "Santiago", 
            "uf_destino" : "CH", 
            "pais_destino" : "Chile", 
            "peso" : "20000", 
            "carga" : "Queijo", 
            "placa_caminhao" : "MMM-9999", 
            "_id" : ObjectId("5b21a4332a5e3333cc64a130")
        },
        {
            "empresa_origem" : "BRF", 
            "cidade_origem" : "Capinzal", 
            "uf_origem" : "SC", 
            "pais_origem" : "Brasil", 
            "empresa_destino" : "Santiago Foods", 
            "cidade_destino" : "Santiago", 
            "uf_destino" : "CH", 
            "pais_destino" : "Chile", 
            "peso" : "22000", 
            "carga" : "Carne Suína", 
            "placa_caminhao" : "MMM-9999", 
            "_id" : ObjectId("5b21a4332a5e3333cc64a131")
        }
    ], 
    "__v" : NumberInt(0)
}

Is it possible? How can I do this using Mongoose? I know that I should use the _id of the "João" user as a parameter.

  • Hello Fred, I was wondering how you generate this _ID of each item within the 'trip', or how you assembled this model/schema

  • @Odirleiborgert _id is an automatically generated attribute. See this guide on the site of Mongoose. Right at the beginning has an example of a schema with a subdocument. Note that there is no _id key, this is because, it is generated automatically. But if you want, you can declare _id manually as well, as in this question.

  • found what was missing in the schema, thank you very much...

1 answer

3


Use the operator $push:

Collection.findByIdAndUpdate(id, { $push: {viagem: OBJETO } }, options, callback)  

Browser other questions tagged

You are not signed in. Login or sign up in order to post.