I am new to mongoDB but I know couchDB pretty well. In couchdb we have JSONObject and JSONArray so we can easily insert anything into document like
doc.put("user", new JSONArray());
or
doc.put("user", new JSONObject());
I want to do something like this below
ArrayList<String> stringArray = new ArrayList<String>();
BasicDBObject document = new BasicDBObject();
document.put("master", stringArray);
db.getCollection("master").insert(document);
So there is a list of string or it can be jsonarray or any array of string, integer type, this array put in document on the key name "master".
This is the whole idea about how to insert an array in mongodb.
I use BasicBSONObject or BasicDBObject in the place of JSONObject (like in couchdb).
What I use for inserting value in the form of array. So, I want this same thing in mongoDB. How can I insert any kind of array whether it would be jsonarray, arraylist or any other kind of array.
Please give me some example.
I think this should do the Trick.
MongoClient mongoClient = new MongoClient( "localhost" , 27017 );
DB db = mongoClient.getDB( "test" );
DBCollection coll = db.getCollection("mycol");
List<String> tags = new ArrayList<String>();
tags.add("Demo");
tags.add("MongoDB");
tags.add("Java");
BasicDBObject doc = new BasicDBObject("title", "MongoDB").
append("tags", tags);
coll.insert(doc);
Could not try it at the moment but this should work.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.