Skip to content

Instantly share code, notes, and snippets.

@lionrajkumar
Last active September 25, 2022 23:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lionrajkumar/4c1fbf952995c4697263d8118a6a4191 to your computer and use it in GitHub Desktop.
Save lionrajkumar/4c1fbf952995c4697263d8118a6a4191 to your computer and use it in GitHub Desktop.

MongoDB Tuts

DB: MongoDB Community Server

GUI: Compass

mongo
show dbs
use myfirstdb
db.createCollection("user")
db.createCollection("profile")
show collections
db.profile.drop()
db.dropDatabase()

db.profile.insert({"name":"abc",age:10})

db.profile.find()
db.profile.insert({_id:2,"name":"sdf",age:20})

db.profile.find().pretty()

db.profile.find().count()

db.profile.insertOne({"name":"ert",city:"FL"})
db.profile.insertMany([{"name":"ghj",age:25},{"name":"yui",city:"IL"},{"name":"sdf",age:15}])


db.profile.find({name:"abc"}).pretty()

db.profile.find({age:{$gt:25}}).pretty()
db.profile.find({age:{$gte:25}}).pretty()
db.profile.find({age:{$lt:25}}).pretty()
db.profile.find({age:{$lte:25}}).pretty()
db.profile.find({age:{$eq:25}}).pretty()
db.profile.find({$and:[name:"abc",{age:25}]}).pretty()
db.profile.find({$or:[name:"abc",{age:25}]}).pretty()
db.profile.find({name:{$in:["abc","sdf"]}}).pretty()
db.profile.find({name:{$nin:["abc","sdf"]}}).pretty()
db.profile.find({age:{$not:{$lt:25}}}).pretty()


db.profile.update({name:"abc"},{$set:{city:"TX"}}) //update existing
db.profile.update({name:"abc"},{city:"TX"}) //delete existing insert new


db.profile.find().sort({"age":1}).pretty() //asc:1 Desc: -1


projection
db.profile.find({name:"abc"},{city:1,"_id":0}).pretty() select only city field hide Id field


db.profile.deleteOne({name:"abc"})
db.profile.remove({name:"abc"})
db.profile.remove({name:"abc"},1)

mongodump --db myfirstdb
mongorestore --db myfirstdb
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment