Home > Enterprise >  How to find() Data with object keys with Mongodb
How to find() Data with object keys with Mongodb

Time:08-28

If I have a Json Like

{
  "_id": "6307cec82ed2f6578c2c4f5a",
  "01 January 2023 Sunday": [
    {
      "date": "01 January 2023 Sunday",
      "location": "Kolkata, India",
      "Sunrise": [
        "06:17 AM"
      ],
      "Sunset": [
        "05:03 PM"
      ],
      "Moonrise": [
        "12:52 PM"
      ],
      "Moonset": [
        "02:08 AM, January 02"
      ]
    }
  ]
}

I want to find() with the key "01 January 2023 Sunday" in MongoDB

CodePudding user response:

You can just use $exists:

db.collection.find({
  "01 January 2023 Sunday": {
    $exists: true
  }
})

Mongo Playground

I don't know your product and your schema could make sense for your needs, with that said I would consider reading about the attribute pattern, you could benefit from restructuring your data.

  • Related