By
using update() method of collection, you can update documents.
>
db.employee.find()
{
"_id" : 1, "firstName" : "Joel",
"lastName" : "chelli" }
{
"_id" : 2, "firstName" : "Ananad",
"lastName" : "Bandaru" }
{
"_id" : 3, "firstName" : "Gopi",
"lastName" : "Battu" }
{
"_id" : 4, "firstName" : "Ritwik",
"lastName" : "Mohenthy" }
{
"_id" : 5, "firstName" : "Ananad",
"lastName" : "Bandaru", "designation" :
"Project Manager" }
Update document
where firstName is “Joel” to “Jessi”.
BasicDBObject
query = new BasicDBObject();
BasicDBObject
update = new BasicDBObject();
query.put("firstName",
"Joel");
update.append("$set",
new BasicDBObject().append("firstName", "Jessi"));
collection.update(query,
update);
Above
snippet updates document, where firstName is “Joel” to “Jessi”.
import java.net.UnknownHostException; import com.mongodb.BasicDBObject; import com.mongodb.DBCursor; import com.mongodb.MongoClient; import com.mongodb.DB; import com.mongodb.DBCollection; public class UpdateDocument { /* Step 1 : get mongoClient */ public static MongoClient getMongoClient(){ MongoClient mongoClient = null; try { mongoClient = new MongoClient( "localhost" , 27017 ); } catch (UnknownHostException e) { e.printStackTrace(); } return mongoClient; } public static void main(String args[]){ MongoClient mongoClient = getMongoClient(); /* Step 2: Connect to DB */ DB db = mongoClient.getDB("sample"); /*Step 3 : Get collection */ DBCollection collection = db.getCollection("employee"); /* Step 4 : Create Query and update objects */ BasicDBObject query = new BasicDBObject(); BasicDBObject update = new BasicDBObject(); query.put("firstName", "Joel"); update.append("$set", new BasicDBObject().append("firstName", "Jessi")); System.out.println("Query"); System.out.println("*******************"); System.out.println(query + "\n"); System.out.println("Before updating"); DBCursor cursor = collection.find(); while(cursor.hasNext()){ System.out.println(cursor.next()); } /* Step 5 : Update document */ collection.update(query, update); System.out.println("After updating"); cursor = collection.find(); while(cursor.hasNext()){ System.out.println(cursor.next()); } } }
Output
Query ******************* { "firstName" : "Joel"} Before updating { "_id" : 1.0 , "firstName" : "Joel" , "lastName" : "chelli"} { "_id" : 2.0 , "firstName" : "Ananad" , "lastName" : "Bandaru"} { "_id" : 3.0 , "firstName" : "Gopi" , "lastName" : "Battu"} { "_id" : 4.0 , "firstName" : "Ritwik" , "lastName" : "Mohenthy"} { "_id" : 5.0 , "firstName" : "Ananad" , "lastName" : "Bandaru" , "designation" : "Project Manager"} After updating { "_id" : 1.0 , "firstName" : "Jessi" , "lastName" : "chelli"} { "_id" : 2.0 , "firstName" : "Ananad" , "lastName" : "Bandaru"} { "_id" : 3.0 , "firstName" : "Gopi" , "lastName" : "Battu"} { "_id" : 4.0 , "firstName" : "Ritwik" , "lastName" : "Mohenthy"} { "_id" : 5.0 , "firstName" : "Ananad" , "lastName" : "Bandaru" , "designation" : "Project Manager"}
1. Add new field
designation to the document where firstName is “Jesse”
BasicDBObject
query = new BasicDBObject();
BasicDBObject
update = new BasicDBObject();
query.put("firstName",
"Jessi");
update.append("$set",
new BasicDBObject().append("designation", "Software
Engineer"));
collection.update(query,
update);
Above
snippet do the task.
import java.net.UnknownHostException; import com.mongodb.BasicDBObject; import com.mongodb.DBCursor; import com.mongodb.MongoClient; import com.mongodb.DB; import com.mongodb.DBCollection; public class UpdateDocument { /* Step 1 : get mongoClient */ public static MongoClient getMongoClient(){ MongoClient mongoClient = null; try { mongoClient = new MongoClient( "localhost" , 27017 ); } catch (UnknownHostException e) { e.printStackTrace(); } return mongoClient; } public static void main(String args[]){ MongoClient mongoClient = getMongoClient(); /* Step 2: Connect to DB */ DB db = mongoClient.getDB("sample"); /*Step 3 : Get collection */ DBCollection collection = db.getCollection("employee"); /* Step 4 : Create Query and update objects */ BasicDBObject query = new BasicDBObject(); BasicDBObject update = new BasicDBObject(); query.put("firstName", "Jessi"); update.append("$set", new BasicDBObject().append("designation", "Software Engineer")); System.out.println("Query"); System.out.println("*******************"); System.out.println(query + "\n"); System.out.println("Before updating"); DBCursor cursor = collection.find(); while(cursor.hasNext()){ System.out.println(cursor.next()); } /* Step 5 : Update document */ collection.update(query, update); System.out.println("After updating"); cursor = collection.find(); while(cursor.hasNext()){ System.out.println(cursor.next()); } } }
Output
******************* { "firstName" : "Jessi"} Before updating { "_id" : 1.0 , "firstName" : "Jessi" , "lastName" : "chelli"} { "_id" : 2.0 , "firstName" : "Ananad" , "lastName" : "Bandaru"} { "_id" : 3.0 , "firstName" : "Gopi" , "lastName" : "Battu"} { "_id" : 4.0 , "firstName" : "Ritwik" , "lastName" : "Mohenthy"} { "_id" : 5.0 , "firstName" : "Ananad" , "lastName" : "Bandaru" , "designation" : "Project Manager"} After updating { "_id" : 1.0 , "firstName" : "Jessi" , "lastName" : "chelli" , "designation" : "Software Engineer"} { "_id" : 2.0 , "firstName" : "Ananad" , "lastName" : "Bandaru"} { "_id" : 3.0 , "firstName" : "Gopi" , "lastName" : "Battu"} { "_id" : 4.0 , "firstName" : "Ritwik" , "lastName" : "Mohenthy"} { "_id" : 5.0 , "firstName" : "Ananad" , "lastName" : "Bandaru" , "designation" : "Project Manager"}
No comments:
Post a Comment