Xaxage
Xaxage

Reputation: 299

Get child object mongoDB

I want to query all child objects filtering them by one field inside of child object.

For example:

{

"_id" : ObjectId("61a8adfc153b52805029b694"),

"date" : "2021-12-02T11:29:00.724Z",

"playerOne" : {
    "trophies" : 8,
    "isBot" : true
}

}

How can i get _id of a document which playerOne child object has more than 100 trophies. Or 5 or less trophies for example.

Upvotes: 0

Views: 79

Answers (1)

NoobCoder
NoobCoder

Reputation: 675

It can be done by $or operator

db.collection.find({
  "$or": [
    {
      "playerOne.trophies": {
        "$gt": 100
      }
    },
    {
      "playerOne.trophies": {
        "$lte": 8
      }
    }
  ]
},
{
  "_id": 1
})

Upvotes: 2

Related Questions