Update app

parent 8e34c2cc
Pipeline #703 failed with stages
in 1 minute and 11 seconds
package com.bee42.mongo; package com.bee42.mongo;
import java.net.UnknownHostException;
import java.util.Date; import java.util.Date;
import com.mongodb.BasicDBObject;
import com.mongodb.DB; import com.mongodb.*;
import com.mongodb.DBCollection; import com.mongodb.client.MongoCollection;
import com.mongodb.DBCursor; import com.mongodb.client.MongoCursor;
import com.mongodb.MongoClient; import com.mongodb.client.MongoDatabase;
import com.mongodb.MongoException;
import static com.mongodb.client.model.Filters.eq;
/** /**
* Java + MongoDB Hello world Example * Java + MongoDB Hello world Example
...@@ -19,30 +20,26 @@ public class App { ...@@ -19,30 +20,26 @@ public class App {
try { try {
/**** Connect to MongoDB ****/ /**** Connect to MongoDB ****/
// Since 2.10.0, uses MongoClient MongoClientURI connectionString = new MongoClientURI("mongodb://localhost:27017");
MongoClient mongo = new MongoClient("localhost", 27017); MongoClient mongo = new MongoClient("localhost", 27017);
/**** Get database ****/ /**** Get database ****/
// if database doesn't exists, MongoDB will create it for you // if database doesn't exists, MongoDB will create it for you
DB db = mongo.getDB("testdb"); MongoDatabase db = mongo.getDatabase("testdb");
/**** Get collection / table from 'testdb' ****/ /**** Get collection / table from 'testdb' ****/
// if collection doesn't exists, MongoDB will create it for you // if collection doesn't exists, MongoDB will create it for you
DBCollection table = db.getCollection("user"); MongoCollection table = db.getCollection("user");
/**** Insert ****/ /**** Insert ****/
// create a document to store key and value // create a document to store key and value
BasicDBObject document = new BasicDBObject(); BasicDBObject document = new BasicDBObject();
document.put("name", "mkyong"); document.put("name", "bee");
document.put("age", 30); document.put("age", "20102");
document.put("createdDate", new Date()); document.put("createdDate", new Date());
table.insert(document); table.insertOne(document);
/**** Find and display ****/ MongoCursor cursor = table.find().filter(eq("name","bee")).iterator();
BasicDBObject searchQuery = new BasicDBObject();
searchQuery.put("name", "mkyong");
DBCursor cursor = table.find(searchQuery);
while (cursor.hasNext()) { while (cursor.hasNext()) {
System.out.println(cursor.next()); System.out.println(cursor.next());
...@@ -51,21 +48,21 @@ public class App { ...@@ -51,21 +48,21 @@ public class App {
/**** Update ****/ /**** Update ****/
// search document where name="mkyong" and update it with new values // search document where name="mkyong" and update it with new values
BasicDBObject query = new BasicDBObject(); BasicDBObject query = new BasicDBObject();
query.put("name", "mkyong"); query.put("name", "bee");
BasicDBObject newDocument = new BasicDBObject(); BasicDBObject newDocument = new BasicDBObject();
newDocument.put("name", "mkyong-updated"); newDocument.put("name", "bee-updated");
BasicDBObject updateObj = new BasicDBObject(); BasicDBObject updateObj = new BasicDBObject();
updateObj.put("$set", newDocument); updateObj.put("$set", newDocument);
table.update(query, updateObj); table.updateOne(query, updateObj);
/**** Find and display ****/ /**** Find and display ****/
BasicDBObject searchQuery2 BasicDBObject searchQuery2
= new BasicDBObject().append("name", "mkyong-updated"); = new BasicDBObject().append("name", "mkyong-updated");
DBCursor cursor2 = table.find(searchQuery2); MongoCursor cursor2 = table.find().filter(eq("name","bee-updated")).iterator();
while (cursor2.hasNext()) { while (cursor2.hasNext()) {
System.out.println(cursor2.next()); System.out.println(cursor2.next());
...@@ -74,8 +71,6 @@ public class App { ...@@ -74,8 +71,6 @@ public class App {
/**** Done ****/ /**** Done ****/
System.out.println("Done"); System.out.println("Done");
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (MongoException e) { } catch (MongoException e) {
e.printStackTrace(); e.printStackTrace();
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment