1.

Some other Handy commands

Answer»
  • use admin
db.createUser({"user": "major", "pwd": passwordPrompt(), "roles": ["major"]})
db.dropUser("major")
db.auth( "user", passwordPrompt() )
  • use test
db.getSiblingDB("dbname")
db.currentOp()
db.killOp(345) // opid
  • Change Streams
watchCursor = db.docx.watch( [ { $match : {"operationType" : "insert" } } ] )
while (!watchCursor.isExhausted()){
if (watchCursor.hasNext()){
print(tojson(watchCursor.next()));
}
}
  • Search in a MongoDb Database: 

db.comments.find({lang:'Python'})

  • Showing All Collections in a Database: 

db.getCollectionNames()

  • Listing a Collection’s Records: 

db.collectionname.find()

  • Listing Records with Matching Values of Specific Fields: 

db. collectionname.find({"field2": "secondmatching value"})

  • Multiple Matching Values:

db. collectionname.find({"field2": "second matching value", "field3": "thirdmatchingvalue"})

  • Finding a Single Record:

db. collectionname.findOne({"field2": "content"})

Conclusion

MongoDB is one of the world’s most popular document databases. It has some powerful capabilities like full-text search, data aggregation etc. One should have a solid knowledge of what MongoDB is. In this document, we’ve covered the basics of MongoDB, its features, and some of the important cheat sheets. We’ve also explored the common database operations of MongoDB. Now, it’s time for you to head out and try what we’ve covered here and more.

Useful Resources

  • Technical Interview Questions

  • Coding Interview Questions

  • Interview Resources

  • DSA- Programming

  • Mock Interview

  • MongoDB Vs MySQL




Discussion

No Comment Found