简体   繁体   English

如何在MongoDB中的嵌入式文档中获得同一级别的所有字段

[英]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. 在这种情况下,我需要在同一级别获取所有JSON数据。

{
   "_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 您可以像JSON对象一样迭代对象键

Below sample will print name and age 下面的示例将打印nameage

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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