From the hp: “MongoDB (from “humongous”) is a scalable, high-performance, open source NoSQL database. Written in C++, features”
This page aims to be a MongoDB “hello world” example, for more info read
MongoDB does not support transactions. Atomic operations are supported.
sudo apt-get install mongodb
will pull in all dependencies, install and start the database.
If required, adjust settings in /etc/mongodb.conf (like IP binding or port)
sudo service mongodb stop sudo service mongodb start
An easy way is the mongo interactive shell. In a terminal, type
mongo
This connects to database “test”.
The information below comes from the MongoDB manual
To use a specific database, say “accounts”, login to mongo and issue
>use accounts
If database “accounts” does not exist yet, it will be created.
List all existing databases:
>show dbs
MongoDB collections can be compared to RDBS tables. Collections are named groupings of documents.
Login to a specific database and list existing collections:
>use accounts >show collections
As with databases, the first time you access a collection, it will be automatically be created for you.
As mentioned before, MongoDB is document-oriented. Whereas you think of rows in RDBMS, you think of documents in MongoDB. More specifically, MongoDB documents are in JSON format.
Insert one document
> db.transactions.save({"name":"alice","type":"withdrawal", "amount": 30})
or bulk insert (supported in shell since v2.2)
>db.transactions.save([{"name":"alice","type":"deposit", "amount": 500}, {"name":"alice","type":"withdrawal", "amount": 50}, {"name":"bob","type":"withdrawal", "amount": 20}, {"name":"bob","type":"withdrawal", "amount": 60}])
Change transaction type to “deposit” where name=bob and amount 60
> db.transactions.update({"name":"bob", "amount":60}, {$set : {"type":"deposit"}})
db.transactions.drop() //drop the entire transactions collection db.transactions.remove() //remove all objects from the collection db.transactions.remove( { name : "sara" } ) //remove objects from the collection where name is sara
Get all documents
> db.transactions.find()
Alice's money transactions:
> db.transactions.find({"name" : "alice"})
Show only type and amount fields
> db.transactions.find({}, {"type":true, "amount":true, "_id":false})
> db.transactions.find({}, {}).limit(2)
sort by name ascending, amount descending
> db.transactions.find( {},{}).sort({"name":1,"amount":-1})
Run ensureIndex(), which builds the index ONLY if it does not already exist.
db.transactions.ensureIndex({"name":true});
View existing indizes
db.transactions.getIndexes()
The manual has more details.
> db.transactions.count() // or with a WHERE clause > db.transactions.count({"name":"alice"})
How much did all users withdraw?
> db.transactions.group( { "key": {"name":true} , "cond": {"type":"withdrawal"}, "reduce": function (obj,prev) { prev.csum += obj.amount; }, "initial": { csum: 0 } } )
> db.transactions.distinct("name")
Use explain() to get statistics about a query, e.g. check “millis” for the query runtime in milliseconds.
NOTE: during the execution of explain(), the actual query is being run! So if the query takes lots of time, explain() will take a long time, too.
> db.transactions.find({},{"name":true, "amount":"true"}).explain() { "cursor" : "BasicCursor", "isMultiKey" : false, "n" : 6, "nscannedObjects" : 6, "nscanned" : 6, "nscannedObjectsAllPlans" : 6, "nscannedAllPlans" : 6, "scanAndOrder" : false, "indexOnly" : false, "nYields" : 0, "nChunkSkips" : 0, "millis" : 0, "indexBounds" : { }, "server" : "ankh:27017" }