Learn MongoDB by Example

Use RoboMongo UI to copy paste and use these commands easily.

———————————————————————
db.collection1.insert({“_id”:1,”name” : “Bhavani”, “marks”:100,”status”:”tbd”})
db.collection1.insert({“_id”:2,”name” : “Polimetla”, “marks”:90,”status”:”tbd”})
db.collection1.insert({“_id”:3,”name” : “Ramu”, “marks”:80,”status”:”tbd”})
db.collection1.insert({“_id”:4,”name” : “John”, “marks”:70,”status”:”tbd”})
db.collection1.insert({“_id”:5,”name” : “Thomas”, “marks”:60,”status”:”tbd”})
db.collection1.insert({“_id”:6,”name” : “Edward”, “marks”:50,”status”:”tbd”})

show dbs

show collections
——————————————–
db.collection1.find()

db.collection1.find().pretty()

db.collection1.find({“name” : “Polimetla”})

db.collection1.find({“marks” : {$gt:80}})

db.collection1.find({“marks” : {$gt:80}}).count()

db.collection1.find({ $or: [{“marks” : {$gt:90}},{“marks” : {$lt:70}}]})
————————————-
db.collection1.find({“_id” : 5})
db.collection1.update(
{‘_id’:5}, { $set: {‘marks’:50}})
db.collection1.find({“_id” : 5})
————————————-
–Update multiple records

db.collection1.find({“marks” : {$gt:80}})
db.collection1.update({“marks” : {$gt:80}},{$set: {“status”:”A”}},{multi:true})
db.collection1.find({“marks” : {$gt:80}})
————————————-
db.collection1.find({“_id” : 5})
db.collection1.remove({“_id” : 5})
db.collection1.find({“_id” : 5})

–Deletes all records. Equal to truncate
db.collection1.remove()
— Go back to first command and insert data again to continue.
————————————-
–Projection: Display only selected columns.

db.collection1.find({},{“name”:1,”marks”:1})
————————————-
–LIMIT: select only first 2 records

db.collection1.find().limit(2)
————————————-

db.collection1.find().sort({“marks”:1})

db.collection1.find().sort({“marks”:-1})

————————————-

db.collection1.aggregate([{$group : {_id : “$by_user”, num_tutorial : {$sum : “$marks”}}}])

db.collection1.aggregate([{$group : {_id : “$by_user”, num_tutorial : {$avg : “$marks”}}}])

db.collection1.aggregate([{$group : {_id : “$by_user”, num_tutorial : {$min : “$marks”}}}])

db.collection1.aggregate([{$group : {_id : “$by_user”, num_tutorial : {$max : “$marks”}}}])

————————————-

db.collection1.drop()

————————————-

Reference: https://www.tutorialspoint.com/mongodb/index.htm

Element Query Operators: $exists, $types
https://docs.mongodb.com/manual/reference/operator/query-element/

Geospatial Query Operators
https://docs.mongodb.com/manual/reference/operator/query-geospatial/

Logical Operators
https://docs.mongodb.com/manual/reference/operator/query-logical/

Regex operations
https://docs.mongodb.com/manual/reference/operator/query/regex/

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s