简体   繁体   English

使用Java驱动器在mongoDB中创建一个包含paired_value(键及其值)集的数组

[英]Create an array which contains set of paired_value (key and its value) in mongoDB using java Drive

I create a MongoDB and I can put data on it, but I cannot make this structure ( I put converted db into JSON format using mongoexport): 我创建了一个MongoDB,可以在上面放数据,但是我不能建立这种结构(我使用mongoexport将转换后的数据库放入JSON格式):

{
    "main":[
       {
          "name":"E",
          "value":"6"
       },
       {
           "name":"P",
           "value":"1",
       }
    ]
}

In fact I want to crate an array which contains set of paired_value of key and its value, for example a pair of "name" and the value which assigned to it. 实际上,我想创建一个数组,其中包含键的paired_value和它的值的集合,例如一对“名称”和分配给它的值。

Before I tested this code: 在测试此代码之前:

BasicDBObject document = new BasicDBObject();
ArrayList ar = new ArrayList();
ar.add((new BasicDBObject("name", "e")));
ar.add((new BasicDBObject("value", 6)));
document.put(ar);

Try this (untested): 试试这个(未试用):

BasicDBObject document = new BasicDBObject();
ArrayList ar = new ArrayList();
ar.add((new BasicDBObject("name", "e").append("value","6")));
ar.add((new BasicDBObject("name", "p").append("value","1")));
document.put("main",ar);

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

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