2.2 KiB

show dbs list all the databases which is having any collection(table)

use <database-name> switched to database with name . If there is no database with given name, creates one and switched to it.

db outputs the currently used database

db.createCollection(<collection-name>) creates a collection

show collections list the collections in current selected database

db.<collection_name>.insert({<js_object>}) inserts the object in collection

db.<collection_name>.insertMany([{<js_objects>}]) inserts the objects in collection

db.<collection_name>.find() lists the object in collection

db.<collection_name>.find({ key: value }) lists the object in collection which matches given key value pair(words(key-value pair) are case sensitive)

db.<collection_name>.find().pretty() lists the objects in collection in more readable format

db.<collection_name>.find().sort({ key: <value>}) lists the objects in collection in sorted form. value{ 1: asc, -1: desc}

db.<collection_name>.find().count() show the count of objects in collection

db.<collection_name>.find().limit(<quantity>) show the list of objects in collection by given quantity

db.<collection_name>.find().forEach(<function>) list of objects in collection based on given object

db.<collection_name>.findOne({<key-value pair condition>}) show the first object in collection; condition is optional

db.<collection_name>.distinct('<property_name>) list all the distinct values present for given property

db.<collection_name>.update() update the object.

  • .update({<mathcing_condition>}, {<updated_value>}, {upsert: true/false}) If the upsert value is true then the new object is created if there is no object with given matching condition otherwise ignores
    • <update_value> If the object is given directly, then it will override the content of previous object
    • $set: <update_value> The object will be updated with prevoious values intact
    • $inc: {key: value} the object will be updated with incrementing the value of key by
    • #rename: { old_key: new_key } the old key will be renamed to new key

db.<collection_name>.remove({<matching_condition>}) remove the object from collection