Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

115
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!