1

Suppose you have the following documents in my collection:

{
    "_id": ObjectId("5c7e12aba50f3b32d42as472"),
    "KJK": {
        "overall": {
            "some_data": 1,
        },
        "date_wise": {
            "2018-07-26": {
                "last_inserted_id": "11",
                "overall": {
                    // some data
                },
                "data": ["temp1": 2, "temp2": 3, ]
            }
        }
    },
    "YNG": {
        "overall": {
            "some_data": 3,
        },
        "date_wise": {
            "2018-07-26": {
                "last_inserted_id": "11",
                "overall": {
                    // some data
                },
                "data": ["temp1": 4, "temp2": 4, ]
            }
        }
    },
    ....
}

There are some lists of operations I want to perform, but getting no clue as I am comparing it with MySQL and it's my 3rd Hard day.

  1. How to search for data by values of a key? e.g. KJK & YNG are dynamic, and I want to search KJK and get all data off it.
  2. How to get only overall of the searched key? e.g. if I search YNG then, it should only return YNG.overall only.

I am not actually able to give efforts, as I don't know how to map keys to find and fetch that data.

I am getting keys I tried this, working, but don't know what to do next.

db.tbl_mytable.find().forEach(function(doc){Object.keys(doc).forEach(function(key){allKeys[key]=1})});

EDIT

The question is I want to perform search operation directly on Key and get overall data of it instead of search anywhere inside subdocument.

EDIT 2
I have checked both the questions which are duplicated. Not a single is related to questions I asked.

Rahul
  • 18,271
  • 7
  • 41
  • 60

0 Answers0