如何使用 Java 将数组添加到 MongoDB 文档?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15371839/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 13:06:40  来源:igfitidea点击:

How to add an array to a MongoDB document using Java?

mongodbmongodb-java

提问by user2162796

I want to create the following document schema in mongoDB using the java driver

我想使用 java 驱动程序在 mongoDB 中创建以下文档模式

{
  "_id": {
    "$oid": "513e9820c5d0d8b93228d7e8"
  },
  "suitename": "testsuite_name",
  "testname": "testcase_name",
  "milestones": [
    {
      "milestone_id": "359",
      "testplans": [
        {
          "pland_id": "965",
          "runs": [
            6985,
            5896
          ]
        },
        {
          "plan_id": "984",
          "runs": [
            9856,
            3684
          ]
        }
      ]
    }
  ]
}

I have the following code

我有以下代码

BasicDBObject testObject = new BasicDBObject();
BasicDBObject milestoneObject = new BasicDBObject();

testObject.put("suitename", testsuite);
testObject.put("testname", testcase);
testObject.put("milestones", new BasicDBObject("milestone_id", "2333"));
locations.insert(testObject);

But this is not generating milestone as an array. How can I add milestone as an array? I currently get this using my code

但这并没有将里程碑生成为数组。如何将里程碑添加为数组?我目前使用我的代码得到这个

{
  "_id": {
    "$oid": "513f93dac5d0e2439d34308e"
  },
  "suitename": "test_deployment_disable_client.TestDeploymentDisableClient",
  "testname": "test_deployment_disable_client",
  "milestones": {
    "milestone_id": "2333"
  }
}

回答by Ori Dar

Change to something like this:

改成这样:

testObject.put("suitename", testsuite);
testObject.put("testname", testcase);         
List<BasicDBObject> milestones = new ArrayList<>();
milestones.add(new BasicDBObject("milestone_id", "2333"));
testObject.put("milestones", milestones);
locations.insert(testObject);

回答by vjrocks5503

You can create an ArrayList which takes in DBObjects.

您可以创建一个接受 DBObjects 的 ArrayList。

List<DBObject> array = new ArrayList<DBObject>();

Add the created DBObject for the object inside the array and add it to the array object created.

为数组内的对象添加创建的 DBObject,并将其添加到创建的数组对象中。

array.add(/* some object */);

Finally, put the array in the main document object.

最后,将数组放入主文档对象中。

document.put("milestones", array);

回答by Shiv Krishna Jaiswal

Better use:

更好的使用:

MongoClient client = new MongoClient("localhost",27017);

MongoCollection<Document> collection =        client.getDatabase("db").getCollection("collection");

List<Document> docs=new ArrayList<>();
docs.add();

collection.insertMany(docs);

client.close();

回答by Devraj Giri

Little extending to previous answer

很少延伸到以前的答案

    BasicDBObject testObject = new BasicDBObject();
    testObject.put("type", "milestones");
    testObject.put("usecase", "milestone_type");

    List<BasicDBObject> testplans = new ArrayList<>();
    testplans.add(new BasicDBObject("plan_id","3232"));
    testplans.add(new BasicDBObject("plan_day","sunday"));


    BasicDBObject milestoneObject = new BasicDBObject();
    milestoneObject.put("milestone_id", "3232");
    milestoneObject.put("plans", testplans);


    List<BasicDBObject> milestones = new ArrayList<>();
    milestones.add( milestoneObject);
    testObject.put("milestones", milestones);


    locations.insert(testObject);