I have the below code which works:
if (aDBCursor.hasNext()) {
DBObject aDbObject = aDBCursor.next();
aDbObject.put("title", "Test Title");
ArrayList<DBObject> department = new ArrayList<DBObject>();
DBObject nested1 = new BasicDBObject();
nested1.put("name", "Department A");
nested1.put("id", 1);
department.add(nested1);
DBObject nested2 = new BasicDBObject();
nested2.put("name", "Department B");
nested2.put("id", 2);
department.add(nested2);
aDbObject.put("department", department);
collection.save(aDbObject);
}
However I have the data for Department A and B in a map like:
Map<Object,Object> map = new HashMap<Object,Object>();
map.put("1", "Department A");
map.put("2", "Department B");
What would the best/easiest way be to save this data? Is there a way to put the map straight into the mongo DB? Or would I have to loop over the map?
The data that goes into the map already is taken from the database like so:
String[] values = req.getParameterValues("departments");
Map<Object,Object> map = new HashMap<Object,Object>();
DBCollection collection = database.getCollection("Departments");
BasicDBObject query = new BasicDBObject();
query.put("id", new BasicDBObject("$in", values));
DBCursor cursor = collection.find(query);
Would be even better is I could just put the DBCursor object back into the database.
Any ideas?
Thanks for any help or suggestions!