Pushing an element into an array type in MongoDB collection using Java Driver is not so obvious at the first attempt. I would share a sample program today that create a MongoDB connection and insert an element into an array type within a collection.
We are going to connect to a MongoD server installed in the local machine and running on default port, The DB name is incidentLog and the collection name is incidents. Please find the structure of the incidents collection below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
> db.incidents.find().pretty() { "_id" : 1, "title" : "Vendor failed to deliver service on time", "description" : "Vendor abcd failed to deliver service on time", "author" : "John", "severity" : "high", "comments" : [ { "author" : "Paul", "comment" : "This is not good" } ] } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
package com.ourownjava.mongo; import java.net.UnknownHostException; import com.mongodb.BasicDBObject; import com.mongodb.DB; import com.mongodb.DBCollection; import com.mongodb.Mongo; import com.mongodb.WriteResult; /** * Insert element into array — mongodb * * @author ourownjava.com */ public class MongoClient { private static final String HOST = "localhost"; private static final int PORT = 27017; /** * @return Mongo * @throws UnknownHostException */ public static Mongo getConnection() throws UnknownHostException { return new Mongo(HOST, PORT); } /** * @param dbName * @param collectionName * @return * @throws UnknownHostException */ private static DBCollection getCollection(final String dbName, final String collectionName) throws UnknownHostException { Mongo mongo = getConnection(); DB db = mongo.getDB(dbName); return db.getCollection(collectionName); } /** * @param args * @throws UnknownHostException */ public static void main(final String[] args) throws UnknownHostException { WriteResult result = addComment(new BasicDBObject("_id", 1), "Amit", "We should take legal actions"); if (null == result.getError()) { System.out.println("Comment is Successfully added"); } } private static WriteResult addComment(final BasicDBObject incidentObject, final String author, final String comment) throws UnknownHostException { final BasicDBObject commentObject = new BasicDBObject(); commentObject.put("author", author); commentObject.put("comment", comment); final DBCollection incidentsCollection = getCollection("incidentLog", "incidents"); return incidentsCollection.update(incidentObject, new BasicDBObject( "$push", new BasicDBObject("comments", commentObject)), false, false); } } |
After running the program find the record to verify that the comment is added.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
> db.incidents.find().pretty() { "_id" : 1, "author" : "John", "comments" : [ { "author" : "Paul", "comment" : "This is not good" }, { "author" : "Amit", "comment" : "We should take legal actions" } ], "description" : "Vendor abcd failed to deliver service on time", "severity" : "high", "title" : "Vendor failed to deliver service on time" } |
Pingback: MongoDB – Add an Element to an Array using Java Driver. | Clean Java
Pingback: Ray Ban Sale