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.
- Check last ‘N’ entries
> collection.find().skip(db.collection.count() – N) - Check the field and type in the collections
> collection.find() - 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 } } ] } ) - Remove entry from mongodb (a document)
collectionname.remove({“_id”: ObjectId(“Object-ID-here”)} - Update the field value in the database
collection.update( { “_id”: ObjectId(“Object-ID-of-field-to-be-updated “)}, { $set: {“messageText”: “field-with-updated-format”}}) - 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
- Check which port is configured for MongoDB (mongoDB listening port)
# lsof -iTCP -sTCP:LISTEN | grep mongo - 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 - Restore the complete mongoDB
# mongorestore /path/of/the/dump