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

119
Vistas
Filter data from array in array mongo

I have this data structure:

{
  date: 0,
  darwin: [
    {
      d: ['string1', 1, 0, 0, 0]
    },
    {
      d: ['string2', 1, 0, 0, 0]
    },
    {
      d: ['string3', 1, 0, 0, 0]
    }
  ]
}

and this schema:

const mongoose = require('mongoose');

const { Schema } = mongoose;

const dataSchema = new Schema({
  date: { type: Number, required: false, unique: true, index: true },
  humanDate: { type: Date, required: false },
  darwin: [
    {
      d: {
        type: Array,
        required: false
      }
    }
  ]
});

module.exports = mongoose.model('data', dataSchema);

I need a list of strings inside "d", i try this

db.getCollection('data').find({date:0},{'darwin.d.$': 1})

but i have this error

Error: error: {
"operationTime" : Timestamp(1635348468, 1),
"ok" : 0,
"errmsg" : "positional operator '.$' couldn't find a matching element in the array",
"code" : 51246,
"codeName" : "Location51246",
"$clusterTime" : {
    "clusterTime" : Timestamp(1635348468, 1),
    "signature" : {
        "hash" : BinData(0,"pfgMUIJkpgjlfnQ6cfiEDpSY+3o="),
        "keyId" : NumberLong("7020434099001098244")
    }
}}

I have tried several things but I can't get it to get the list of strings, I don't know if I'm applying the '$' operator wrong

I expected some like this

{
  date: 0,
  darwin: [
    {
      d: 'string1'
    },
    {
      d: 'string2'
    },
    {
      d: 'string3'
    }
  ]
}
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

You can use aggregate method like this:

  • First $match the docuemnts where date has the desired value
  • Then $project to map over the array and get values you want: For each element in darwin array get the first element in d array using $arrayElemAt.
db.collection.aggregate([
  {
    "$match": {
      "date": 0
    }
  },
  {
    "$project": {
      "_id": 0,
      "date": 1,
      "darwin": {
        "$map": {
          "input": "$darwin",
          "as": "m",
          "in": {
            "d": {
              "$arrayElemAt": [
                "$$m.d",
                0
              ]
            }
          }
        }
      }
    }
  }
])

Which output is:

[
  {
    "darwin": [
      {
        "d": "string1"
      },
      {
        "d": "string2"
      },
      {
        "d": "string3"
      }
    ],
    "date": 0
  }
]

Example here

about 4 years ago · Juan Pablo Isaza 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