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
Collection : Table
Document : Row
Field : Column
1. Find all documents of items collection
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1. db.items.find(); |
2. Find documents of items collection whose price is 10
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
db.items.find({ price : 10 }); |
3. Find documents of items collection whose price is greater than 20
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
db.items.find( { price : { $gt : 20 } }); |
4. Find documents of items collection whose price is greater than 20 and less than 100
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
db.items.find({ price : { $gt : 20, $lt : 100 } }); |
5. Loop through all documents of items collection and print JSON
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var myCursor = db.items.find(); | |
myCursor.forEach(printjson); |
6. Find first two document of items collection
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
db.items.find().limit(2); |
7. Skip first two document of items collection and display others
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
db.items.find().skip(2); |
8. Insert a document in items collection
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
db.items.insert({ | |
name : 'daal makhni', | |
itemCode : 'a1', | |
price : "120", | |
sellingPrice : "140", | |
restaurant : "5540ca56d1f925f156a8c33d" | |
}); |
9. Find first document of items collection
db.items.findOne(); is same as db.items.find().limit(1);
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
db.items.findOne(); |
10. Find all document of items collection where name is ‘daal’ or ‘daal makhni’
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
db.items.find({ name : { $in : ['daal', 'daal makhni'] } }); |
11. And condition
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
db.items.find({ name : 'daal', price : 10}); | |
db.items.find({ $and : [{ name : 'daal', price : 10}] }); | |
db.items.find({ $and : [ {name : 'daal'}, {price : 10} ] }); |
12. Or condition
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
db.items.find({ $or : [{name : 'daal'}, {price : 20}] }); |
13. $and and $or both condition
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
db.items.find({ type : 'veg', $or : [ { name : 'daal'}, {name : 'rice'} ] }); |
14. Start mongoDB shell
This will connect you to running mongod instance
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
mongo (Enter this in terminal) |
15. Show the list of databases
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
show dbs |
16. Show current db
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use nite-foodie-internal |
18. Deleted all collection of current database
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
db.dropDatabase(); |
19. Show all collections of current database
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
show collections |
20. Create collection items in current database
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
db.createCollection(‘items’); |
21. Delete collection items of current database
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
db.items.drop(); |
22. Update query
Update the title
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
db.COLLECTION_NAME.update(SELECTIOIN_CRITERIA, UPDATED_DATA); | |
db.items.update({ title : 'first title'}, { $set : { 'title' : 'new title' } }); |
23. Delete documents where title equal to ‘new title’ of items collection
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
db.items.remove({title : 'new title'}); |
24. Delete all documents of items collection
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
db.items.remove({}); |
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
db.items.find({}, {name:1, price:1,type:1}); |
26. Sort is used to sort data
1 is used for ascending order while -1 is used for descending order
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
db.items.find({}).sort({ 'name' : 1}); | |
db.items.find({}).sort({ 'name' : -1}); |
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 }
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
db.items.aggregate([ { $group : { _id : "$type", total_price : { $sum :"$price" } } } ]); |
28. Create backup
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
29. Restore database nite-foodie-internal to mongoDB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
mongorestore dump/nite-foodie-internal |
30. Returns the count of documents that would match a find() query
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
db.items.find({}).count(); |
31. Apply regex in mongoDB query
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
db.items.find({ cuisine : new RegExp('^Indian$', 'i') }); |
32. How to export MongoDB database in CSV format
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
No comments:
Post a Comment