How to add an array to a MongoDB document using Java?
Asked Answered
S

4

19

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

{
  "_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"
  }
}
Sulphur answered 12/3, 2013 at 20:48 Comment(0)
S
43

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);
Sayer answered 12/3, 2013 at 21:8 Comment(0)
R
5

You can create an ArrayList which takes in DBObjects.

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

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

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

Finally, put the array in the main document object.

document.put("milestones", array);
Realm answered 19/6, 2015 at 19:26 Comment(0)
L
1

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();
Lixivium answered 10/6, 2017 at 14:1 Comment(0)
P
0

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);
Pallette answered 16/10, 2018 at 14:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.