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

75
Vistas
Return only one field from MongoDB query

Suppose my document looks like this

{
    "_id": 2,
    "Name": "Abcd",
    "Connected": [1,3]
}

Now, I want to retrieve the array [1,3] via the _id or Name. The purpose is to save this array into a JavaScript array.

Hence I tried using db.myCollection.find({"_id":2},{"Connected":1}) but it return a document again moreover with _id still there like this

{ "_id" : 2, "Connected" : [ 1, 3 ] }

How can I retrieve only that array?

over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

You can optionally remove the ID from your result, by setting it to 0 in the projection:

db.myCollection.find({"_id":2},{"Connected":1, "_id":0})

The ID field is the only field that can be excluded in projections while other fields are included.

If you want to get the Connected field directly, you can use findOne:

db.myCollection.findOne({"_id":2}, {"Connected":1, "_id":0}).Connected
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