1

I have a MongoDB collection with the following format:

{
        "_id" : ObjectId("5b6b112d1bbd972848b97df4"),
        "team" : "Switzerland",
        "Game" : [
                {
                        "City" : "Nizhny Novgorod",
                        "T2N" : "Costa Rica",
                        "T1N" : "Switzerland",
                        "ST" : "Nizhny Novgorod Stadium",
                        "T1S" : "2",
                        "Date" : "6/27/2018",
                        "T2S" : "2"
                },
                {
                        "City" : "Kaliningrad",
                        "T2N" : "Serbia",
                        "T1N" : "Switzerland",
                        "ST" : "Kaliningrad Stadium",
                        "T1S" : "2",
                        "Date" : "6/22/2018",
                        "T2S" : "1"
                },
                {
                        "City" : "Rostov-on-Don",
                        "T2N" : "Brazil",
                        "T1N" : "Switzerland",
                        "ST" : "Rostov Arena",
                        "T1S" : "1",
                        "Date" : "6/17/2018",
                 }]
}

I am trying to run the following query: db.test.find({"Game.T1S":{$gte:3}}).pretty() But this returns data in the following format:

{
        "_id" : ObjectId("5b6b112d1bbd972848b97df4"),
        "team" : "Switzerland",
        "Game" : [
                {
                        "City" : "Nizhny Novgorod",
                        "T2N" : "Costa Rica",
                        "T1N" : "Switzerland",
                        "ST" : "Nizhny Novgorod Stadium",
                        "T1S" : "2",
                        "Date" : "6/27/2018",
                        "T2S" : "2"
                },
                {
                        "City" : "Kaliningrad",
                        "T2N" : "Serbia",
                        "T1N" : "Switzerland",
                        "ST" : "Kaliningrad Stadium",
                        "T1S" : "2",
                        "Date" : "6/22/2018",
                        "T2S" : "1"
                },
                {
                        "City" : "Rostov-on-Don",
                        "T2N" : "Brazil",
                        "T1N" : "Switzerland",
                        "ST" : "Rostov Arena",
                        "T1S" : "1",
                        "T2S" : "3"
                        "Date" : "6/17/2018",
                 }]
}

I want result to be just

db.test.find({"Game.T1S":{$gte:3}}).pretty() But this returns data in the following format:

{
        "_id" : ObjectId("5b6b112d1bbd972848b97df4"),
        "team" : "Switzerland",
        "Game" : [
                {
                        "City" : "Rostov-on-Don",
                        "T2N" : "Brazil",
                        "T1N" : "Switzerland",
                        "ST" : "Rostov Arena",
                        "T1S" : "1",
                        "T2S" : "3"
                        "Date" : "6/17/2018",
                 }]
}

That is the result for game should only contain the record with T1S >= 3 and not the other records as in the first output. Is there any way I can achieve this?

Helping hand
  • 2,800
  • 2
  • 19
  • 31
noopur kharche
  • 59
  • 1
  • 11

1 Answers1

0

Try this:

db.collection.find({
  "Game.T1S": {
    $gte: "2"
  }
}, {
  "team": 1,
  "Game.$": 1
})

You can see it working here

The idea is to use the find projection and the $ projection operator

Akrion
  • 18,117
  • 1
  • 34
  • 54