Saturday, May 2, 2015

Introduction to mongoDB database

In MongoDB

Collection    :   Table
Document    :   Row
Field            :   Column

1. Find all documents of items collection
1. db.items.find();
view raw gistfile1.js hosted with ❤ by GitHub

2. Find documents of items collection whose price is 10
db.items.find({ price : 10 });
view raw gistfile1.js hosted with ❤ by GitHub

3. Find documents of items collection whose price is greater than 20
db.items.find( { price : { $gt : 20 } });
view raw gistfile1.js hosted with ❤ by GitHub

4. Find documents of items collection whose price is greater than 20 and less than 100
db.items.find({ price : { $gt : 20, $lt : 100 } });
view raw gistfile1.js hosted with ❤ by GitHub

5. Loop through all documents of items collection and print JSON
var myCursor = db.items.find();
myCursor.forEach(printjson);
view raw gistfile1.js hosted with ❤ by GitHub

6. Find first two document of items collection
db.items.find().limit(2);
view raw gistfile1.js hosted with ❤ by GitHub

7. Skip first two document of items collection and display others
db.items.find().skip(2);
view raw gistfile1.js hosted with ❤ by GitHub

8. Insert a document in items collection
db.items.insert({
name : 'daal makhni',
itemCode : 'a1',
price : "120",
sellingPrice : "140",
restaurant : "5540ca56d1f925f156a8c33d"
});
view raw gistfile1.js hosted with ❤ by GitHub

9. Find first document of items collection
db.items.findOne(); is same as db.items.find().limit(1);
db.items.findOne();
view raw gistfile1.js hosted with ❤ by GitHub

10. Find all document of items collection where name is ‘daal’ or ‘daal makhni’
db.items.find({ name : { $in : ['daal', 'daal makhni'] } });
view raw gistfile1.js hosted with ❤ by GitHub

11. And condition
db.items.find({ name : 'daal', price : 10});
db.items.find({ $and : [{ name : 'daal', price : 10}] });
db.items.find({ $and : [ {name : 'daal'}, {price : 10} ] });
view raw gistfile1.js hosted with ❤ by GitHub

12. Or condition
db.items.find({ $or : [{name : 'daal'}, {price : 20}] });
view raw gistfile1.js hosted with ❤ by GitHub

13. $and and $or both condition
db.items.find({ type : 'veg', $or : [ { name : 'daal'}, {name : 'rice'} ] });
view raw gistfile1.js hosted with ❤ by GitHub

14. Start mongoDB shell
      This will connect you to running mongod instance
mongo (Enter this in terminal)
view raw gistfile1.js hosted with ❤ by GitHub

15. Show the list of databases
show dbs
view raw gistfile1.js hosted with ❤ by GitHub

16. Show current db
db
view raw gistfile1.js hosted with ❤ by GitHub

17. If database nite-foodie-internal is exist then this command switch to nite-foodie-internal database. If database nite-foodie-internal is not exist then this command create database named nite-foodie-internal
use nite-foodie-internal
view raw gistfile1.js hosted with ❤ by GitHub

18. Deleted all collection of current database
db.dropDatabase();
view raw gistfile1.js hosted with ❤ by GitHub

19. Show all collections of current database
show collections
view raw gistfile1.js hosted with ❤ by GitHub

20. Create collection items in current database
db.createCollection(‘items’);
view raw gistfile1.js hosted with ❤ by GitHub

21. Delete collection items of current database
db.items.drop();
view raw gistfile1.js hosted with ❤ by GitHub

22. Update query
      Update the title
db.COLLECTION_NAME.update(SELECTIOIN_CRITERIA, UPDATED_DATA);
db.items.update({ title : 'first title'}, { $set : { 'title' : 'new title' } });
view raw gistfile1.js hosted with ❤ by GitHub

23. Delete documents where title equal to ‘new title’ of items collection
db.items.remove({title : 'new title'});
view raw gistfile1.js hosted with ❤ by GitHub

24. Delete all documents of items collection
db.items.remove({});
view raw gistfile1.js hosted with ❤ by GitHub

25. Find all documents of items collection and display only name price and type of each document.
In MongoDB when you execute find() method, then it displays all fields of a document. To limit this you need to set list of fields with value 1 or 0. 1 is used to show the field while 0 is used to hide the field.
db.items.find({}, {name:1, price:1,type:1});
view raw gistfile1.js hosted with ❤ by GitHub

26. Sort is used to sort data
      1 is used for ascending order while -1 is used for descending order
db.items.find({}).sort({ 'name' : 1});
db.items.find({}).sort({ 'name' : -1});
view raw gistfile1.js hosted with ❤ by GitHub

27. This query will loop through all documents of collection items and sum all price of same type
{ "_id" : "egg", "total_price" : 30 }
{ "_id" : "non-veg", "total_price" : 20 }
{ "_id" : "veg", "total_price" : 161 }
db.items.aggregate([ { $group : { _id : "$type", total_price : { $sum :"$price" } } } ]);
view raw gistfile1.js hosted with ❤ by GitHub

28. Create backup
mongodump --collection COLLECTION_NAME --db DB_NAME
mongodump --db nite-foodie-internal
// create backup of nite-foodie-internal database
mongodump --collection items --db nite-foodie-internal
// create backup of items collection of nite-foodie-internal database
view raw gistfile1.js hosted with ❤ by GitHub

29. Restore database nite-foodie-internal to mongoDB
mongorestore dump/nite-foodie-internal
view raw gistfile1.js hosted with ❤ by GitHub

30. Returns the count of documents that would match a find() query
db.items.find({}).count();
view raw gistfile1.js hosted with ❤ by GitHub

31. Apply regex in mongoDB query
db.items.find({ cuisine : new RegExp('^Indian$', 'i') });
view raw gistfile1.js hosted with ❤ by GitHub

32. How to export MongoDB database in CSV format
mongoexport --host localhost --db databaseName --collection collectionName --csv --out fileName.csv --fields commaSeperatedFieldsName
mongoexport --host localhost --db nite-foodie-pro --collection customers --csv --out text.csv --fields number
view raw MongoDB.sh hosted with ❤ by GitHub

No comments:

Post a Comment