package test;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Locale;
import org.bson.Document;
import com.mongodb.Block;
import com.mongodb.MongoClient;
import com.mongodb.client.AggregateIterable;
import com.mongodb.client.FindIterable;
import com.mongodb.client.MongoDatabase;
import com.mongodb.client.model.Filters;
import com.mongodb.client.model.Sorts;
import com.mongodb.client.result.UpdateResult;
/*
* you can find anything you need at
* 1) https://docs.mongodb.org/getting-started/java/aggregation/
* 2) http://api.mongodb.org/java/3.0/overview-summary.html
*/
public class Demo_MangoDB {
public static void main(String[] args) {
try {
MongoClient mongoClient = new MongoClient("localhost", 27017);
MongoDatabase db = mongoClient.getDatabase("test");
DateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'", Locale.ENGLISH);
// If you attempt to add documents to a collection that does not exist, MongoDB will create the collection for you.
db.getCollection("restaurants").insertOne(
new Document("address",
new Document()
.append("street", "2 Avenue")
.append("zipcode", "10075")
.append("building", "1480")
.append("coord", Arrays.asList(-73.9557413, 40.7720266)))
.append("borough", "Manhattan")
.append("cuisine", "Italian")
.append("grades", Arrays.asList(
new Document()
.append("date", format.parse("2014-10-01T00:00:00Z"))
.append("grade", "A")
.append("score", 11),
new Document()
.append("date", format.parse("2014-01-16T00:00:00Z"))
.append("grade", "B")
.append("score", 17)))
.append("name", "Vella")
.append("restaurant_id", "41704620"));
FindIterable<Document> iterable = db.getCollection("restaurants").find();
iterable.forEach(new Block<Document>() {
@Override
public void apply(Document document) {
System.out.println(document);
}
});
//Find or Query Data
iterable = db.getCollection("restaurants").find(new Document("borough", "Manhattan"));
//iterable = db.getCollection("restaurants").find(Filters.eq("borough", "Manhattan"));
iterable = db.getCollection("restaurants").find(new Document("address.zipcode", "10075"));
iterable = db.getCollection("restaurants").find(Filters.eq("address.zipcode", "10075"));
iterable = db.getCollection("restaurants").find(Filters.eq("grades.grade", "B"));
iterable = db.getCollection("restaurants").find(new Document("grades.score", new Document("$gt", 30)));
iterable = db.getCollection("restaurants").find(Filters.gt("grades.score", 30));
iterable = db.getCollection("restaurants").find(new Document("cuisine", "Italian").append("address.zipcode", "10075"));
iterable = db.getCollection("restaurants").find(Filters.and(Filters.eq("cuisine", "Italian"), Filters.eq("address.zipcode", "10075")));
iterable = db.getCollection("restaurants").find(
new Document("$or", Arrays.asList(new Document("cuisine", "Italian"),
new Document("address.zipcode", "10075"))));
iterable = db.getCollection("restaurants").find(Filters.or(Filters.eq("cuisine", "Italian"), Filters.eq("address.zipcode", "10075")));
iterable = db.getCollection("restaurants").find().sort(new Document("borough", 1).append("address.zipcode", 1)); //1 for ascending and -1 for descending.
db.getCollection("restaurants").find().sort(Sorts.ascending("borough", "address.zipcode"));
//Update Data
UpdateResult ur = db.getCollection("restaurants").updateOne(new Document("name", "Juni"), new Document("$set", new Document("cuisine", "American (New)")).append("$currentDate", new Document("lastModified", true)));
System.out.println(ur.getModifiedCount());
db.getCollection("restaurants").updateOne(new Document("restaurant_id", "41156888"),
new Document("$set", new Document("address.street", "East 31st Street")));
db.getCollection("restaurants").updateMany(new Document("address.zipcode", "10016").append("cuisine", "other"),
new Document("$set", new Document("cuisine", "Category To Be Determined")).append("$currentDate", new Document("lastModified", true)));
db.getCollection("restaurants").replaceOne(new Document("restaurant_id", "41704620"),
new Document("address",
new Document()
.append("street", "2 Avenue")
.append("zipcode", "10075")
.append("building", "1480")
.append("coord", Arrays.asList(-73.9557413, 40.7720266)))
.append("name", "Vella 2"));
//Remove Data
db.getCollection("restaurants").deleteMany(new Document("borough", "Manhattan")); //Remove All Documents That Match a Condition
//db.getCollection("restaurants").deleteMany(new Document()); //Remove All Documents
//db.getCollection("restaurants").drop(); //Drop a Collection
//Data Aggregation
AggregateIterable<Document> aggIterable = db.getCollection("restaurants").aggregate(Arrays.asList(
new Document("$group", new Document("_id", "$building").append("count", new Document("$sum", 1)))));
aggIterable = db.getCollection("restaurants").aggregate(Arrays.asList(
new Document("$match", new Document("address.street", "2 Avenue").append("address.building", "1480")),
new Document("$group", new Document("_id", "$address.zipcode").append("count", new Document("$sum", 1)))
));
aggIterable.forEach(new Block<Document>() {
@Override
public void apply(Document document) {
// TODO Auto-generated method stub
System.out.println(document.toJson());
}
});
//db.drop(); //Drop the current Database
//mongoClient.dropDatabase("test");
mongoClient.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}