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

116
Visualizações
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 Respostas
Responde à pergunta

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 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