Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

150
Visualizações
MongoDB JsonObject and JsonArray

I'm new MongoDB. I have a collection is named "surveyCollection". I insert some data to collection with shell,

db.surveyCollection.insert({
   "count":30, 
   "people": [
        {
           "firstName":"John",
           "lastName":"Jason"
        }
   ]
})
db.surveyCollection.insert({
   "count":30, 
   "people": [
        {
           "firstName":"Sarah",
           "lastName":"Smith"
        }
   ]
})

And I have data below ( db.surveyCollection.find().pretty());

{
    "_id" : ObjectId("5873d0c86c3319a579754d00"),
    "count" : 30,
    "people" : [
        {
            "firstName" : "John",
            "lastName" : "Jason"
        }
    ]
}
{
    "_id" : ObjectId("5873d0c86c3319a579754d00"),
    "count" : 30,
    "people" : [
        {
            "firstName" : "Sarah",
            "lastName" : "Smith"
        }
    ]
}

I don't know how can I insert a new people to any survey. I mean, I want to have datas like below;

{
    "_id" : ObjectId("5873d0c86c3319a579754d00"),
    "count" : 30,
    "people" : [
        {
            "firstName" : "John",
            "lastName" : "Jason"
        }
    ]
}
{
    "_id" : ObjectId("5873d0c86c3319a579754d00"),
    "count" : 30,
    "people" : [
        {
            "firstName" : "Sarah",
            "lastName" : "Smith"
        },
        {
            "firstName" : "George",
            "lastName" : "Smith"
        }
    ]
}

I mean, how can I reach values in JsonArray?

Thank you

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

Use $push method to push an item into array of a document as shown below :

db.surveyCollection.update(
 { "_id" : ObjectId("5873d0c86c3319a579754d00") },
 { $push: { people: { "firstName" : "George", "lastName" : "Smith"} } }
) 

This will add the object { "firstName" : "George", "lastName" : "Smith"} to the array people in the document of {"_id" : ObjectId("5873d0c86c3319a579754d00")}

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda