Home > Mobile >  MongoDB query to get records which matching conditions
MongoDB query to get records which matching conditions

Time:10-07

I have below records in mongodb. I need to write a query that will return records whose Name: whose CITY is Bihar & region is null Example: from below records, It returns two collections Name : Anil and Kumar (because anil & kumar has City:Bihar & region is null

[
  {
    "id": "A1",
    "Name": "Anil",
    "details": {
      "State": "INDIA",
      "Address": [
        {
          "CITY": "BIHAR",
          "region": null
        },
        {
          "CITY": "Delhi",
          "region": null
        }
      ],
      "DateTime": "2021-03-30T10:14:22.203Z"
    }
  },
  {
    "id": "A2",
    "Name": "Sunil",
    "details": {
      "State": "INDIA",
      "Address": [
        {
          "CITY": "Banglore",
          "region": null
        },
        {
          "CITY": "Delhi",
          "region": null
        }
      ],
      "DateTime": "2021-03-30T10:14:22.203Z"
    }
  },
  {
    "id": "A3",
    "Name": "Kumar",
    "details": {
      "State": "INDIA",
      "Address": [
        {
          "CITY": "BIHAR",
          "region": null
        },
        {
          "CITY": "assam",
          "region": null
        }
      ],
      "DateTime": "2021-03-30T10:14:22.203Z"
    }
  }
]

CodePudding user response:

find

db.collection.find({
  "details.Address.CITY": "BIHAR"
})

mongoplayground

  • Related