Saturday, May 2, 2015

Introduction to mongoDB database

In MongoDB

Collection    :   Table
Document    :   Row
Field            :   Column

1. Find all documents of items collection

2. Find documents of items collection whose price is 10

3. Find documents of items collection whose price is greater than 20

4. Find documents of items collection whose price is greater than 20 and less than 100

5. Loop through all documents of items collection and print JSON

6. Find first two document of items collection

7. Skip first two document of items collection and display others

8. Insert a document in items collection

9. Find first document of items collection
db.items.findOne(); is same as db.items.find().limit(1);

10. Find all document of items collection where name is ‘daal’ or ‘daal makhni’

11. And condition

12. Or condition

13. $and and $or both condition

14. Start mongoDB shell
      This will connect you to running mongod instance

15. Show the list of databases

16. Show current db

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

18. Deleted all collection of current database

19. Show all collections of current database

20. Create collection items in current database

21. Delete collection items of current database

22. Update query
      Update the title

23. Delete documents where title equal to ‘new title’ of items collection

24. Delete all documents of items collection

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.

26. Sort is used to sort data
      1 is used for ascending order while -1 is used for descending order

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 }

28. Create backup

29. Restore database nite-foodie-internal to mongoDB

30. Returns the count of documents that would match a find() query

31. Apply regex in mongoDB query

32. How to export MongoDB database in CSV format

No comments:

Post a Comment