Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

151
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda