简体   繁体   中英

How to get all fields in a same level in embedded documents in MongoDB

Thanks to embedded documents, we don't need join operations.

However, I need to get all fields in a same level to prepare a list or report.

Are there any easy-way to get all fields in a same level?

For instance; I want to prepare a report with all the following fields. In this case, I need to get all JSON data in a same level.

{
   "_id": "1",
   "date" : "2014-07-30 02:00",
   "personnel" : {
       "_id": "14",
       "name": "xxxx Stackton",
       "address" : {
             "city": "nevercity",
             "province": "neverland"
              }
     },
    "customer": {
         "_id": "473",
         "name": "xxxx Jordan"
     },
    "payment": {
        "method": "cash",
        "amount": "67.40"
     }
}

You can iterate the object keys similar like a JSON object

Below sample will print name and age

var myObj = { name : 'Tom', age: 21 };

for(key in myObj) {
  print(key);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM