Thursday, January 23, 2014

PyMongo - Installation

Note: PyMongo installation at Ubuntu

- sudo apt-get install python-pip
- sudo apt-get install build-essential python-dev
- sudo pip install pymongo

MongoDB 2.4 - Mongo and Java (remove a sub-document from a list)

Note: ways to remove a sub-document which is the element of an list

1. document

{
        "_id" : 0,
        "scores" : [
                {
                        "type" : "exam",
                        "score" : 1.463179736705023
                },
                {
                        "type" : "quiz",
                        "score" : 11.78273309957772
                },
                {
                        "type" : "homework",
                        "score" : 6.676176060654615
                },
                {
                        "type" : "homework",
                        "score" : 35.8740349954354
                }
        ]
}

2. $unset

- Mongo: db.students.update({"_id":0},{$unset:{"scores.3":true}})
- Java

collection.update(new BasicDBObject("_id", 0), new BasicDBObject("$unset", new BasicDBObject("scores.3", true)));

- Note: scores.3 will be 'null'

3. $pull

- Mongo: db.students.update({"_id":0},{$pull:{"scores":{"type":"homework","score":35.8740349954354}}})
- Java

collection.update(new BasicDBObject("_id", document.get("_id")),  new BasicDBObject("$pull", new BasicDBObject("scores", new BasicDBObject("type", "homework").append("score", scoreTemp))));

Wednesday, January 22, 2014

Java - Map

import java.util.HashMap;
import java.util.Hashtable;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.TreeMap;

public class Test {

final static int LOOP_COUNT = 1000;
final static String STR_DATA = "abcdefghijklmnopqrstuvwxyz";
static Map<Integer, String> map;

public static void main(String[] args) {
warmUp();

map = new Hashtable<Integer, String>();

showUsedTime();

map = new HashMap<Integer, String>();

showUsedTime();

map = new LinkedHashMap<Integer, String>();

showUsedTime();

map = new TreeMap<Integer, String>();

showUsedTime();
}

private static void warmUp() {
map = new HashMap<Integer, String>();

for (int i = 0; i < LOOP_COUNT; i++) {
map.put(i, STR_DATA);
}
}

private static void showUsedTime() {
long startTime = System.nanoTime();

for (int i = 0; i < LOOP_COUNT; i++) {
map.put(i, STR_DATA);
}

long endTime = System.nanoTime();
double elaspedTime = (endTime - startTime) / 1000000.0;

System.out.println("Put Time: " + elaspedTime);

startTime = System.nanoTime();

for (int i = 0; i < LOOP_COUNT; i++) {
map.get(i);
}

endTime = System.nanoTime();
elaspedTime = (endTime - startTime) / 1000000.0;

System.out.println("Get Time: " + elaspedTime);
}
}

Java - List

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Vector;

public class Test {

final static int LOOP_COUNT = 1000;
static List<Integer> list;

public static void main(String[] args) {
warmUp();

list = new ArrayList<Integer>();

System.out.println("===== ArrayList =====");
showUsedTime();

list = new Vector<Integer>();

System.out.println("===== Vector =====");
showUsedTime();

list = new LinkedList<Integer>();

System.out.println("===== LinkedList =====");
showLinkedListUsedTime();
}

private static void warmUp() {
list = new ArrayList<Integer>();

for (int i = 0; i < LOOP_COUNT; i++) {
list.add(i);
}
}

private static void showLinkedListUsedTime() {
int dummy = 0;

showUsedTime();

long startTime = System.nanoTime();

for (int i = 0; i < LOOP_COUNT; i++) {
dummy = ((LinkedList<Integer>) list).peek();
}

long endTime = System.nanoTime();
double elaspedTime = (endTime - startTime) / 1000000.0;

System.out.println("Peek Time: " + elaspedTime);
}

private static void showUsedTime() {
int dummy = 0;
long startTime = System.nanoTime();

for (int i = 0; i < LOOP_COUNT; i++) {
list.add(i);
}

long endTime = System.nanoTime();
double elaspedTime = (endTime - startTime) / 1000000.0;

System.out.println("Add Time: " + elaspedTime);

startTime = System.nanoTime();

for (int i = 0; i < LOOP_COUNT; i++) {
dummy = list.get(i);
}
endTime = System.nanoTime();
elaspedTime = (endTime - startTime) / 1000000.0;

System.out.println("Get Time: " + elaspedTime);
}
}

Java - Set

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.Set;
import java.util.TreeSet;

public class Test {

final static int LOOP_COUNT = 1000;
final static String STR_DATA = "abcdefghijklmnopqrstuvwxyz";
static Set<String> set;

public static void main(String[] args) {
warmUp();

set = new HashSet<String>();

showUsedTime();

set = new LinkedHashSet<String>();

showUsedTime();

set = new TreeSet<String>();

showUsedTime();
}

private static void warmUp() {
set = new HashSet<String>();

for (int i = 0; i < LOOP_COUNT; i++) {
set.add(STR_DATA + i);
}
}

private static void showUsedTime() {
String dummy = null;

long startTime = System.nanoTime();

for (int i = 0; i < LOOP_COUNT; i++) {
set.add(STR_DATA + i);
}

long endTime = System.nanoTime();
double elaspedTime = (endTime - startTime) / 1000000.0;

System.out.println("Add Time: " + elaspedTime);

startTime = System.nanoTime();

Iterator<String> iterator = set.iterator();

while (iterator.hasNext()) {
dummy = iterator.next();
}

endTime = System.nanoTime();
elaspedTime = (endTime - startTime) / 1000000.0;

System.out.println("Get Time: " + elaspedTime);
}
}

Java - String

public class Test {

public static void main(String[] args) {
final int LOOP_COUNT = 10000;
final String str = "test";
String a = new String();
StringBuffer b = new StringBuffer(); // thread-safe
StringBuilder c = new StringBuilder(); // thread-not-safe

long startTime = System.nanoTime();

for (int i = 0; i < LOOP_COUNT; i++) {
a += str;
}

long endTime = System.nanoTime();
double elaspedTime = (endTime - startTime) / 1000000.0;

System.out.println(elaspedTime);

startTime = System.nanoTime();

for (int i = 0; i < LOOP_COUNT; i++) {
b.append(str);
}

b.toString();

endTime = System.nanoTime();
elaspedTime = (endTime - startTime) / 1000000.0;

System.out.println(elaspedTime);

startTime = System.nanoTime();

for (int i = 0; i < LOOP_COUNT; i++) {
c.append(str);
}

c.toString();

endTime = System.nanoTime();
elaspedTime = (endTime - startTime) / 1000000.0;

System.out.println(elaspedTime);
}
}

MongoDB 2.4 - Java (GridFS)

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;

import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.MongoClient;
import com.mongodb.gridfs.GridFS;
import com.mongodb.gridfs.GridFSDBFile;
import com.mongodb.gridfs.GridFSInputFile;

public class MongoDB {

public static void main(String[] args) throws IOException {
MongoClient client = new MongoClient();
DB database = client.getDB("databaseName");
FileInputStream fileInputStream = null;

GridFS videos = new GridFS(database, "videos");

try {
fileInputStream = new FileInputStream("video.mp4");
} catch (FileNotFoundException e) {
e.printStackTrace();
client.close();
System.exit(1);
}

GridFSInputFile video = videos.createFile(fileInputStream, "video.mp4");
BasicDBObject metadata = new BasicDBObject("description",
"Jennier Singing");
ArrayList<String> tags = new ArrayList<String>();

tags.add("Singing");
tags.add("Opera");
metadata.append("tags", tags);
video.setMetaData(metadata);
video.save();

System.out
.println("Object ID in Files Collection: " + video.get("_id"));
System.out.println("Saved the file to MongoDB");
System.out.println("Now let's read it back out");

GridFSDBFile gridFSDBFile = videos.findOne(new BasicDBObject(
"filename", "video.mp4"));
FileOutputStream fileOutputStream = new FileOutputStream(
"video_copy.mp4");
gridFSDBFile.writeTo(fileOutputStream);

System.out.println("Write the file back out");

fileInputStream.close();
fileOutputStream.close();
client.close();
}
}

MongoDB 2.4 - Java (findAndModify)

import java.net.UnknownHostException;

import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.DBObject;
import com.mongodb.MongoClient;
import com.mongodb.ServerAddress;

public class MongoDB {

private static MongoClient client;
private static DBCollection collection;

public static void main(String[] args) throws UnknownHostException {
createCollection();

int first;
int numNeeded;

numNeeded = 2;
first = getRange(numNeeded);

printCollection();
System.out.println("Range: " + first + "-" + (first + numNeeded - 1));

numNeeded = 3;
first = getRange(numNeeded);

printCollection();
System.out.println("Range: " + first + "-" + (first + numNeeded - 1));

numNeeded = 10;
first = getRange(numNeeded);

printCollection();
System.out.println("Range: " + first + "-" + (first + numNeeded - 1));

client.close();
}

private static int getRange(int range) {
DBObject document = collection.findAndModify(new BasicDBObject("_id",
"abc"), null, null, false, new BasicDBObject("$inc",
new BasicDBObject("counter", range)), true, true);

return (Integer) document.get("counter") - range + 1;
}

private static void createCollection() throws UnknownHostException {
client = new MongoClient(new ServerAddress("localhost", 27017));

DB database = client.getDB("databaseName");

collection = database.getCollection("collectionName");

collection.drop();
}

private static void printCollection() {
DBCursor result = null;

try {
result = collection.find();

while (result.hasNext()) {
DBObject document = result.next();

System.out.println(document);
}
} finally {
result.close();
}
}
}

MongoDB 2.4 - Java (update, remove)

import java.net.UnknownHostException;
import java.util.Arrays;
import java.util.List;

import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.DBObject;
import com.mongodb.MongoClient;
import com.mongodb.ServerAddress;

public class MongoDB {

private static MongoClient client;
private static DBCollection collection;

private static void createCollection() throws UnknownHostException {
MongoDB.client = new MongoClient(new ServerAddress("localhost", 27017));

DB database = client.getDB("databaseName");

MongoDB.collection = database.getCollection("collectionName");

collection.drop();
}

public static void main(String[] args) throws UnknownHostException {
createCollection();

List<String> names = Arrays.asList("alice", "bobby", "cathy", "david",
"ethan");

for (String name : names) {
collection.insert(new BasicDBObject("_id", name));
}

printCollection();
System.out.println("==========");

collection.update(new BasicDBObject("_id", "alice"), new BasicDBObject(
"age", 24));

printCollection();
System.out.println("==========");

collection.update(new BasicDBObject("_id", "alice"), new BasicDBObject(
"gender", "F"));

printCollection();
System.out.println("==========");

collection.update(new BasicDBObject("_id", "bobby"), new BasicDBObject(
"$set", new BasicDBObject("age", "26")));

printCollection();
System.out.println("==========");

collection.update(new BasicDBObject("_id", "bobby"), new BasicDBObject(
"$set", new BasicDBObject("gender", "F")));

printCollection();
System.out.println("==========");

collection.update(new BasicDBObject("_id", "frank"), new BasicDBObject(
"$set", new BasicDBObject("gender", "F")), true, false);

printCollection();
System.out.println("==========");

collection.update(new BasicDBObject("_id", "frank"), new BasicDBObject(
"$set", new BasicDBObject("gender", "M")), true, false);

printCollection();
System.out.println("==========");

collection.update(new BasicDBObject(), new BasicDBObject("$set",
new BasicDBObject("title", "Dr.")), false, true);

printCollection();
System.out.println("==========");

collection.remove(new BasicDBObject("_id", "alice"));

printCollection();
System.out.println("==========");

collection.remove(new BasicDBObject());

printCollection();
System.out.println("==========");

client.close();
}

private static void printCollection() {
DBCursor result = null;

try {
result = collection.find();

while (result.hasNext()) {
DBObject document = result.next();

System.out.println(document);
}
} finally {
result.close();
}
}
}

MongoDB 2.4 - Java (sort, skip, limit)

import java.net.UnknownHostException;
import java.util.Random;

import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.DBObject;
import com.mongodb.MongoClient;
import com.mongodb.ServerAddress;

public class MongoDB {

private static DBCollection collection;

public static void main(String[] args) throws UnknownHostException {
MongoClient client = new MongoClient(new ServerAddress("localhost",
27017));
DB database = client.getDB("databaseName");

collection = database.getCollection("collectionName");
collection.drop();

Random random = new Random();

for (int i = 0; i < 10; i++) {
collection.insert(new BasicDBObject("_id", i).append(
"start",
new BasicDBObject("x", random.nextInt(2)).append("y",
random.nextInt(90) + 10)).append(
"end",
new BasicDBObject("x", random.nextInt(2)).append("y",
random.nextInt(90) + 10)));
}

show();

client.close();
}

private static void show() {
DBCursor result = null;

try {
result = collection.find().sort(new BasicDBObject("_id", -1));

showResult(result);
System.out.println("==========");

result = collection.find().sort(new BasicDBObject("_id", -1))
.skip(2);

showResult(result);
System.out.println("==========");

result = collection.find().sort(new BasicDBObject("_id", -1))
.skip(2).limit(3);

showResult(result);
System.out.println("==========");

result = collection.find().sort(new BasicDBObject("start.x", 1))
.skip(2).limit(7);

showResult(result);
System.out.println("==========");

result = collection
.find()
.sort(new BasicDBObject("start.x", 1).append("start.y", -1))
.skip(2).limit(7);

showResult(result);
} finally {
result.close();
}
}

private static void showResult(DBCursor result) {
while (result.hasNext()) {
DBObject document = result.next();

System.out.println(document);
}
}
}

MongoDB 2.4 - Java (dot notation)

import java.net.UnknownHostException;
import java.util.Random;

import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.DBObject;
import com.mongodb.MongoClient;
import com.mongodb.QueryBuilder;
import com.mongodb.ServerAddress;

public class MongoDB {

private static DBCollection collection;

public static void main(String[] args) throws UnknownHostException {
MongoClient client = new MongoClient(new ServerAddress("localhost",
27017));
DB database = client.getDB("databaseName");

collection = database.getCollection("collectionName");
collection.drop();

Random random = new Random();

for (int i = 0; i < 10; i++) {
collection.insert(new BasicDBObject("_id", i).append(
"start",
new BasicDBObject("x", random.nextInt(90) + 10).append("y",
random.nextInt(90) + 10)).append(
"end",
new BasicDBObject("x", random.nextInt(90) + 10).append("y",
random.nextInt(90) + 10)));
}

DBObject query = QueryBuilder.start().get();

show(query);

query = QueryBuilder.start("start.x").greaterThan(50).get();

show(query);

client.close();
}

private static void show(DBObject query) {
DBCursor result = null;

try {
result = collection.find(query);

showResult(result);

result = collection.find(query, new BasicDBObject("start.y", true));

showResult(result);

result = collection.find(query,
new BasicDBObject("start.y", true).append("_id", false));

showResult(result);
} finally {
result.close();
}
}

private static void showResult(DBCursor result) {
while (result.hasNext()) {
DBObject document = result.next();

System.out.println(document);
}
}
}

MongoDB 2.4 - Java (field selection)

import java.net.UnknownHostException;
import java.util.Random;

import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.DBObject;
import com.mongodb.MongoClient;
import com.mongodb.QueryBuilder;
import com.mongodb.ServerAddress;

public class MongoDB {

private static DBCollection collection;

public static void main(String[] args) throws UnknownHostException {
MongoClient client = new MongoClient(new ServerAddress("localhost",
27017));
DB database = client.getDB("databaseName");

collection = database.getCollection("collectionName");
collection.drop();

Random random = new Random();

for (int i = 0; i < 10; i++) {
collection
.insert(new BasicDBObject("x", random.nextInt(2)).append(
"y", random.nextInt(100)).append("z",
random.nextInt(1000)));
}

DBObject query = QueryBuilder.start("x").is(0).and("y").greaterThan(10)
.lessThan(90).get();

show(query);

client.close();
}

private static void show(DBObject query) {
DBCursor result = null;

try {
result = collection.find(query, new BasicDBObject("x", false));

showResult(result);

result = collection.find(query, new BasicDBObject("y", true));

showResult(result);

result = collection.find(query,
new BasicDBObject("y", true).append("_id", false));

showResult(result);

} finally {
result.close();
}
}

private static void showResult(DBCursor result) {
while (result.hasNext()) {
DBObject document = result.next();

System.out.println(document);
}
}
}

MongoDB 2.4 - Java (query criteria)

import java.net.UnknownHostException;
import java.util.Random;

import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.DBObject;
import com.mongodb.MongoClient;
import com.mongodb.QueryBuilder;
import com.mongodb.ServerAddress;

public class MongoDB {

private static DBCollection collection;

public static void main(String[] args) throws UnknownHostException {
MongoClient client = new MongoClient(new ServerAddress("localhost",
27017));
DB database = client.getDB("databaseName");
collection = database.getCollection("collectionName");

collection.drop();

for (int i = 0; i < 10; i++) {
collection.insert(new BasicDBObject("x", new Random().nextInt(2))
.append("y", new Random().nextInt(100)));
}

DBObject query = new BasicDBObject("x", 0);

show(query);

query = new BasicDBObject("x", 0).append("y", new BasicDBObject("$gt",
10).append("$lt", 90));

show(query);

QueryBuilder queryBuilder = QueryBuilder.start("x").is(0).and("y")
.greaterThan(10).lessThan(90);

show(queryBuilder.get());

client.close();
}

private static void show(DBObject query) {
showCount(query);
showFind(query);
}

private static void showCount(DBObject query) {
long count = collection.count(query);

System.out.println(count);
}

private static void showFind(DBObject query) {
DBCursor find = collection.find(query);

try {
while (find.hasNext()) {
DBObject document = find.next();

System.out.println(document);
}
} finally {
find.close();
}
}
}

MongoDB 2.4 - Java (find, findOne, count)

import java.net.UnknownHostException;
import java.util.Random;

import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.DBObject;
import com.mongodb.MongoClient;
import com.mongodb.ServerAddress;

public class MongoDB {

public static void main(String[] args) throws UnknownHostException {
MongoClient client = new MongoClient(new ServerAddress("localhost",
27017));
DB database = client.getDB("databaseName");
DBCollection collection = database.getCollection("collectionName");

collection.drop();

for (int i = 0; i < 10; i++) {
collection
.insert(new BasicDBObject("x", new Random().nextInt(100)));
}

DBCursor find = collection.find();

try {
while (find.hasNext()) {
DBObject document = find.next();

System.out.println(document);
}
} finally {
find.close();
}

DBObject findOne = collection.findOne();

System.out.println(findOne);

long count = collection.count();

System.out.println(count);
client.close();
}
}

Tuesday, January 21, 2014

MongoDB 2.4 - Java (insert)

import java.net.UnknownHostException;
import java.util.Arrays;

import org.bson.types.ObjectId;

import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBObject;
import com.mongodb.MongoClient;
import com.mongodb.ServerAddress;

public class MongoDB {

public static void main(String[] args) throws UnknownHostException {
MongoClient client = new MongoClient(new ServerAddress("localhost",
27017));
DB database = client.getDB("databaseName");
DBCollection collection = database.getCollection("collectionName");

collection.drop();

DBObject document01 = new BasicDBObject().append("a", 1);

System.out.println(document01);
collection.insert(document01);
System.out.println(document01);

DBObject document02 = new BasicDBObject("_id", new ObjectId()).append(
"b", 2);

System.out.println(document02);
collection.insert(document02);
System.out.println(document02);

DBObject document03 = new BasicDBObject("c", 3);
DBObject document04 = new BasicDBObject();

System.out.println(document03);
System.out.println(document04);
collection.insert(Arrays.asList(document03, document04));
System.out.println(document03);
System.out.println(document04);

client.close();
}
}

MongoDB 2.4 - Java (document)

import java.util.Arrays;
import java.util.Date;

import com.mongodb.BasicDBObject;

public class MongoDB {

public static void main(String[] args) {
BasicDBObject document = new BasicDBObject();
document.put("userName", "mungeol");
document.put("developer", true);
document.put("age", 29);
document.put("languages", Arrays.asList("C", "Java"));
document.put(
"address",
new BasicDBObject("street", "20 Main").append("town",
"Westfield").append("zip", 12345));
document.put("registerDate", new Date());

System.out.println(document);
}
}

Monday, January 20, 2014

MongoDB 2.4 - Java Driver

1. Java Driver

- Download URL: https://github.com/mongodb/mongo-java-driver/downloads

2. Java Example

import java.net.UnknownHostException;

import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBObject;
import com.mongodb.MongoClient;
import com.mongodb.ServerAddress;

public class MongoDB {

public static void main(String[] args) throws UnknownHostException {
MongoClient client = new MongoClient(new ServerAddress("localhost",
27017));
DB database = client.getDB("databaseName");
DBCollection collection = database.getCollection("collectionName");
DBObject document = collection.findOne();

System.out.println(document);

client.close();
}
}