1.Installing
Download MnogoDB (in this case linux version) and untar.
create the directory /data/db, if don't want to or can't create the directory under root then create it somewhere else (/opt/data/db) and create a symbolic link in the / directory.
Start the MongoDB daemon with
mongodb-linux-x86_64-1.6.4]$ bin/mongodFrom another command shell connect to the DB with
bin/mongod --help for help and startup options
Thu Nov 25 16:43:57 MongoDB starting : pid=5221 port=27017 dbpath=/data/db/ 64-bit
Thu Nov 25 16:43:57 db version v1.6.4, pdfile version 4.5
Thu Nov 25 16:43:57 git version: 4f5c02f8d92ff213b71b88f5eb643b7f62b50abc
Thu Nov 25 16:43:57 sys info: Linux domU-12-31-39-06-79-A1 2.6.21.7-2.ec2.v1.2.fc8xen #1 SMP Fri Nov 20 17:48:28 EST 2009 x86_64
BOOST_LIB_VERSION=1_41
Thu Nov 25 16:43:57 [initandlisten] waiting for connections on port 27017
Thu Nov 25 16:43:57 [websvr] web admin interface listening on port 28017
Thu Nov 25 16:44:15 [initandlisten] connection accepted from 127.0.0.1:52961 #1
Thu Nov 25 16:45:21 allocating new datafile /data/db/asangadb.ns, filling with zeroes...
Thu Nov 25 16:45:21 done allocating datafile /data/db/asangadb.ns, size: 16MB, took 0.029 secs
Thu Nov 25 16:45:21 allocating new datafile /data/db/asangadb.0, filling with zeroes...
Thu Nov 25 16:45:21 done allocating datafile /data/db/asangadb.0, size: 64MB, took 0.123 secs
Thu Nov 25 16:45:21 [conn1] building new index on { _id: 1 } for asangadb.asangadb
Thu Nov 25 16:45:21 allocating new datafile /data/db/asangadb.1, filling with zeroes...
bin/mongoThat's it, now the DB is ready to receive connections.
MongoDB shell version: 1.6.4
connecting to: test
2. Creating & Connecting
In MongoDB databases aren't created explicitly. A database is lazily created when it is used for the very first time.
Download the drivers for Java from the MongoDB site.
To connect to the DB specify the host name (ip) and port. (There are other ways to connect as well).
Mongo m = new Mongo("192.168.0.76", 27017);If required it is also possible to secure the connection with password.
DB db = m.getDB( "pradeepdb" );
This case a database called "pradeepdb" will be created if it is not present, if it's present then a connect will be made to it. Whenever a new database is created following could be seen in the starter daemon (mongod) output
Thu Nov 25 17:10:30 [initandlisten] connection accepted from 192.168.0.29:52274 #11In the command shell switch between databases using
Thu Nov 25 17:10:30 allocating new datafile /data/db/pradeepdb.ns, filling with zeroes...
Thu Nov 25 17:10:30 done allocating datafile /data/db/pradeepdb.ns, size: 16MB, took 0.03 secs
Thu Nov 25 17:10:30 allocating new datafile /data/db/pradeepdb.0, filling with zeroes...
Thu Nov 25 17:10:30 done allocating datafile /data/db/pradeepdb.0, size: 64MB, took 0.121 secs
Thu Nov 25 17:10:30 allocating new datafile /data/db/pradeepdb.1, filling with zeroes...
Thu Nov 25 17:10:30 [conn11] building new index on { _id: 1 } for pradeepdb.pradeepdb
Thu Nov 25 17:10:30 [conn11] done for 0 records 0.012secs
use dbname
3. Inserting By default a a collection is created with the same name as the database name. If required a separate collection could be created. Again not explicitly, if one doesn't exists it will be created on first use.
To create or get the relevant collection use the db reference created earlier
DBCollection col = db.getCollection("pradeep2");To insert a key-value into this collection
DBObject obj = new BasicDBObject();To insert values in the command shell
obj.put("pradeep", 200);
col.insert(obj);
db.pradeep2.insert({"pradeep":500});4.Reading
To read the first object in a collection
DBObject obj = col.findOne();To read all the objects in a collection
DBCursor cur = col.find();To read one specific object in a collection, first create the object with desired properties and then query the collection using that object.
while(cur.hasNext()) {
System.out.println(cur.next());
}
BasicDBObject query = new BasicDBObject();On the command shell following could be used to get the first value and all the values
query.put("asanga", 100);
DBCursor cur = col.find(query);
while(cur.hasNext()) {
System.out.println(cur.next());
}
db.pradeep2.findOne();5. Updating
db.pradeep2.find();
To update create two objects that represents past image and new image. Past image will be updated by the new image.
DBObject oldObj = new BasicDBObject();On the command prompt use
oldObj.put("asanga", 100);
DBObject newObj = new BasicDBObject();
newObj.put("asanga", 200);
col.update(oldObj, newObj);
db.pradeep2.update({"asanga":200},{"asanga":500});6. Deleting
Create an object to repsent the object being deleted and use the remove function to delete it from the database.
DBObject obj = new BasicDBObject();On command shell use
obj.put("asanga", 500);
col.remove(obj);
db.pradeep2.remove({"asanga":500});Follow the MongoDB Java Tutorial for more.