Home > Software design >  Filter data from array in array mongo
Filter data from array in array mongo

Time:10-28

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'
    }
  ]
}

CodePudding user response:

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

  • Related