Update app

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