1

I am currently trying to query a array in mongodb, I want to look for all the fields that have the id: "ZhU76Ta" in them, but the array to search is barryed in a other doc, so my query looks something like this: {"punishdata.Entrys.0.id": "ZhU76Ta"} but this only query the first doc in the array, I tryed something like this {"punishdata.Entrys.*.id": "ZhU76Ta" } but this also does not work, dose anybody know how to do this?

Document structure:
{
"_id" : ObjectId("5b212e540a975a231c85419c"),
"name" : "Starmixcraft",
"punishdata" : {
    "Entrys" : [ 
        {
            "id" : "ZhU76Ta",
            "end" : NumberLong(-2),
            "start" : NumberLong(1528901227837),
            "time" : NumberLong(-2)
        }
    ]
}
}
Ashh
  • 44,693
  • 14
  • 105
  • 132
Lost2
  • 138
  • 1
  • 13

2 Answers2

1

Try this code:

db.collectionName.find({"punishdata.Entrys.id": "ZhU76Ta" })
felipsmartins
  • 13,269
  • 4
  • 48
  • 56
Serdar
  • 478
  • 3
  • 11
1

You need to use $filter aggregation here which simply eliminates the unmatched documents from the array

db.collection.aggregate([
  {
    "$project": {
      "name": 1,
      "punishdata.Entrys": {
        "$filter": {
          "input": "$punishdata.Entrys",
          "as": "entry",
          "cond": {
            "$eq": [
              "$$entry.id",
              "ZhU76Ta"
            ]
          }
        }
      }
    }
  }
])
Ashh
  • 44,693
  • 14
  • 105
  • 132