Last active
August 29, 2015 14:21
-
-
Save GMadorell/32327d3c407817bc29b8 to your computer and use it in GitHub Desktop.
mongo_test
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import com.mongodb.*; | |
import java.net.UnknownHostException; | |
import java.util.ArrayList; | |
import java.util.Collections; | |
import java.util.List; | |
public class Main { | |
static final String DELIMITER = "---------------\n"; | |
public static void main(String[] args) throws UnknownHostException { | |
// http://mongodb.github.io/mongo-java-driver/ | |
MongoClient mongoClient = new MongoClient(); | |
DB db = mongoClient.getDB("test-ats-db"); | |
DBCollection products = db.getCollection("products"); | |
// Clean up the collection. | |
products.drop(); | |
// Create an object and insert it into the collection. | |
BasicDBObject potato = new BasicDBObject("name", "potato") | |
.append("price", 300) | |
.append("type", "potato"); | |
products.insert(potato); | |
// Find the object we've already created. | |
DBObject doc = products.findOne(); | |
System.out.println(doc); | |
// -> { "_id" : { "$oid" : "5551b53644ae5de7dde67ae6"} , "name" : "potato" , "price" : 300 , "type" : "potato"} | |
// Bulk insert. | |
List<DBObject> productsToInsert = new ArrayList<DBObject>(); | |
productsToInsert.add( | |
new BasicDBObject("name", "raspberry").append("price", 400).append("type", "fruit") | |
); | |
productsToInsert.add( | |
new BasicDBObject("name", "caviar").append("price", 1000).append("type", "fish") | |
); | |
productsToInsert.add( | |
new BasicDBObject("name", "apple").append("price", 150).append("type", "fruit") | |
); | |
productsToInsert.add( | |
new BasicDBObject("name", "orange").append("price", 890).append("type", "fruit") | |
); | |
products.insert(productsToInsert); | |
// Search them all. | |
System.out.println(DELIMITER + "Search them all:"); | |
DBCursor allProductsCursor = products.find(); | |
for (DBObject product : allProductsCursor) { | |
System.out.println(product); | |
} | |
allProductsCursor.close(); | |
// -> { "_id" : { "$oid" : "5551b53644ae5de7dde67ae6"} , "name" : "potato" , "price" : 300 , "type" : "potato"} | |
// { "_id" : { "$oid" : "5551b53644ae5de7dde67ae7"} , "name" : "raspberry" , "price" : 400 , "type" : "fruit"} | |
// { "_id" : { "$oid" : "5551b53644ae5de7dde67ae8"} , "name" : "caviar" , "price" : 1000 , "type" : "fish"} | |
// { "_id" : { "$oid" : "5551b53644ae5de7dde67ae9"} , "name" : "apple" , "price" : 150 , "type" : "fruit"} | |
// { "_id" : { "$oid" : "5551b53644ae5de7dde67aea"} , "name" : "orange" , "price" : 890 , "type" : "fruit"} | |
// Count. | |
System.out.println(DELIMITER + "Count:"); | |
System.out.println(products.count()); | |
// -> 5 | |
// Query for potato. | |
System.out.println(DELIMITER + "Query for potato:"); | |
DBObject potatoQuery = new BasicDBObject("name", "potato"); | |
DBCursor potatoCursor = products.find(potatoQuery); | |
for (DBObject product : potatoCursor) { | |
System.out.println(product); | |
} | |
potatoCursor.close(); | |
// -> "_id" : { "$oid" : "5551b53644ae5de7dde67ae6"} , "name" : "potato" , "price" : 300 , "type" : "potato"} | |
// Query for products cheaper than 500. | |
System.out.println(DELIMITER + "Query for products cheaper than 500:"); | |
DBObject cheaperThan500Query = new BasicDBObject("price", new BasicDBObject("$lt", 500)); | |
DBCursor cheaperThan500Cursor = products.find(cheaperThan500Query); | |
for (DBObject product : cheaperThan500Cursor) { | |
System.out.println(product); | |
} | |
cheaperThan500Cursor.close(); | |
// -> { "_id" : { "$oid" : "5551b53644ae5de7dde67ae6"} , "name" : "potato" , "price" : 300 , "type" : "potato"} | |
// { "_id" : { "$oid" : "5551b53644ae5de7dde67ae7"} , "name" : "raspberry" , "price" : 400 , "type" : "fruit"} | |
// { "_id" : { "$oid" : "5551b53644ae5de7dde67ae9"} , "name" : "apple" , "price" : 150 , "type" : "fruit"} | |
// In the same way, we can remove objects that match a query using: | |
System.out.println(DELIMITER + "Remove cheaper than 500."); | |
products.remove(cheaperThan500Query); | |
System.out.println("... and then query all products."); | |
allProductsCursor = products.find(); | |
for (DBObject product : allProductsCursor) { | |
System.out.println(product); | |
} | |
allProductsCursor.close(); | |
// -> { "_id" : { "$oid" : "5551b53644ae5de7dde67ae8"} , "name" : "caviar" , "price" : 1000 , "type" : "fish"} | |
// { "_id" : { "$oid" : "5551b53644ae5de7dde67aea"} , "name" : "orange" , "price" : 890 , "type" : "fruit"} | |
// Clean the db and insert the products again. | |
System.out.println(DELIMITER + "Clean the db and insert the products again."); | |
products.drop(); | |
products.insert(potato); | |
products.insert(productsToInsert); | |
allProductsCursor = products.find(); | |
for (DBObject product : allProductsCursor) { | |
System.out.println(product); | |
} | |
allProductsCursor.close(); | |
// -> { "_id" : { "$oid" : "5551b53644ae5de7dde67ae6"} , "name" : "potato" , "price" : 300 , "type" : "potato"} | |
// { "_id" : { "$oid" : "5551b53644ae5de7dde67ae7"} , "name" : "raspberry" , "price" : 400 , "type" : "fruit"} | |
// { "_id" : { "$oid" : "5551b53644ae5de7dde67ae8"} , "name" : "caviar" , "price" : 1000 , "type" : "fish"} | |
// { "_id" : { "$oid" : "5551b53644ae5de7dde67ae9"} , "name" : "apple" , "price" : 150 , "type" : "fruit"} | |
// { "_id" : { "$oid" : "5551b53644ae5de7dde67aea"} , "name" : "orange" , "price" : 890 , "type" : "fruit"} | |
// Group by: get the minimum price for every type. | |
System.out.println(DELIMITER + "Group by: get the minimum price for every type."); | |
List<DBObject> minPricePerTypePipeline = buildListOutOfDBObjects( | |
new BasicDBObject("$group", | |
new BasicDBObject("_id", "$type") | |
.append("minimum", new BasicDBObject("$min", "$price")) | |
) | |
); | |
AggregationOutput minPricePerTypeOutput = products.aggregate(minPricePerTypePipeline); | |
for (DBObject result : minPricePerTypeOutput.results()) { | |
System.out.println(result); | |
} | |
// -> { "_id" : "fish" , "minimum" : 1000} | |
// { "_id" : "fruit" , "minimum" : 150} | |
// { "_id" : "potato" , "minimum" : 300} | |
// Filter price > 500 and then group by minimum price per type. | |
System.out.println(DELIMITER + "Filter price > 500 and then group by minimum price per type."); | |
List<DBObject> moreThan500PriceAndMinPricePerTypePipeline = buildListOutOfDBObjects( | |
new BasicDBObject("$match", | |
new BasicDBObject("price", new BasicDBObject("$gt", 500)) | |
), | |
new BasicDBObject("$group", | |
new BasicDBObject("_id", "$type") | |
.append("minimum", new BasicDBObject("$min", "$price")) | |
) | |
); | |
AggregationOutput moreThan500PriceAndMinPricePerTypeOutput = products.aggregate(moreThan500PriceAndMinPricePerTypePipeline); | |
for (DBObject result : moreThan500PriceAndMinPricePerTypeOutput.results()) { | |
System.out.println(result); | |
} | |
// -> { "_id" : "fruit" , "minimum" : 890} | |
// { "_id" : "fish" , "minimum" : 1000} | |
mongoClient.close(); | |
} | |
private static List<DBObject> buildListOutOfDBObjects(BasicDBObject... basicDBObjects) { | |
ArrayList<DBObject> dbObjects = new ArrayList<DBObject>(); | |
Collections.addAll(dbObjects, basicDBObjects); | |
return dbObjects; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment