MongoDB is a popular open source Database application. While it is way easier than other databases, it also supports salient features like Master Slave replication, ad hoc queries, indexing and much more. Let us know few basic commands of MongoDB. You can login to MongoDB console with ‘mongo’ command.

  1. Check last ‘N’ entries
    > collection.find().skip(db.collection.count() – N)
  2. Check the field and type in the collections
    > collection.find()
  3. Find entry containing specific value in the field
    For only one entry – > collection.findOne({“messageText” : /.vaule./});
    For mulitple entries – > db. collection.find({messageText:/ .vaule./})
       For date specific entries > db. collection.find
    ( { $and: [ { messageText: { value } }, { messageText: { date-format-you have-entered } } ] } )
  4. Remove entry from mongodb (a document)
    collectionname.remove({“_id”: ObjectId(“Object-ID-here”)}
  5. Update the field value in the database
    collection.update( { “_id”: ObjectId(“Object-ID-of-field-to-be-updated “)}, { $set: {“messageText”: “field-with-updated-format”}})
  6. Find text in the collection/DB with pre-defined identifier
    General command – > collection.find( {}, { “identifier-value”: “text-in-the-field” } )
    Restricting entries upto ‘N’ number – > db. collection.find( {}, { “identifier-value”: “text-in-the-field” } )
    .skip(db.collection.count() – N)
  • Commands to be executed outside MongoDB console i.e. Linux terminal
  1. Check which port is configured for MongoDB (mongoDB listening port)
    # lsof -iTCP -sTCP:LISTEN | grep mongo
  2. Take complete backup of the MongoDB
    Complete backup – # mongodump -o /path/to/take/dump
      Database specific backup – # mongodump –db newdb –out /path/to/take/dump/Name-of-backup
  3. Restore the complete mongoDB
    # mongorestore /path/of/the/dump