- A Non-relational database.
- Document oriented database.
Mongo Data Model (from http://www.mongodb.org/display/DOCS/Introduction)
- A Mongo system holds a set of databases
- A database holds a set of collections
- A collection holds a set of documents
- A document is a set of fields
- A field is a key-value pair
- A key is a name (string)
- A value is a
- basic type like string, integer, float, timestamp, binary, etc.,
- a document, or
- an array of values
How to Install ?
- Download from http://www.mongodb.org/downloads
- Extract it to c:/mongodb
- Go to c:/mongodb/bin, there you can see mongo.exe (client) and mongod.exe (database server)
- Create the folders to store the database. Default is c:/data/db
- After doing these steps, go to command prompt and execute mongod first
- Then execute mongo.exe.
- Then you can start playing with commands, try db.version()
Playing with Mongo DB Commands
- db.help() - gets all the command
- db.stats() - gets database details such as DB name, no of collections, data size, indexes & etc.
- db.test (test is the collection name) - Create to a collection
- db.getCollectionNames() - the names of the collections made.
- use test - switches to the specified collection
Now you are in the "test" collection let's do some CURD operations !!!!!!!
Reading data
db.test.find()
SELECT * FROM tbl_name
db.test.find({gender: f})
SELECT * FROM tbl_nameWHERE geneder ='f'
db.test.find({gender: {$ne: 'f'}, weight: {$gte: 70}})
SELECT * FROM tbl_name WHERE gender !='f' AND weight >= 70
db.test.find({gender: 'f', $or: [{name: 'jen'}, {name: 'ann'}, {weight: {$lt: 50}}]})
SELECT * FROM name WHERE gender='f' AND name='jen' OR name='ann' AND weight < 50
Insert Data
db.test.insert({name: 'ann', dob: new Date(1985,2,13), city: 'london'});
Remove Data
db.test.remove()
DELETE FROM tbl_name;
db.test.remove({city:'london'})
DELETE FROM tbl_name WHERE city='london';
Update Data
db.test.update({id: 1202}, {$set: {name: 'ann mary' }})
UPDATE tbl_name SET name='ann mary' WHERE id=1202
db.test.update({name: 'ann'}, {$inc: {count: +2 }})
Count is increased by 2 in order to decrease use minus(-)
db.test.update({name: 'ann'}, {$push: {age: 23 }})
New field called age is appended
What is an UPSERT ??????
It UPDATES if the document is already there if not it inserts a document but in order to do that third parameter has to be specified.
db.test.update({page: 'unicorns'}, {$inc: {hits: 1}}, true);
Multiple Updates !!
Only the first matching record is updated unless it is told to do multiple updates. It is done by specifying the fourth parameter.
db.test.update({}, {$set: {activate: true }}, false, true);
No comments:
Post a Comment