Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to find() Data with object keys with Mongodb

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading