Commands
db.collection.find()
Returns all documents in the collection
db.collection.find({<field>:<value>})
Returns all documents where the field matches the specified value
db.collection.find({$and:[{<field1>:<value1>},{<field2>:<value2>}]})
Returns all documents where both the fields match their respective values
db.collection.find({$or:[{<field1>:<value1>},{<field2>:<value2>}]})
Returns all documents where either field matches its respective value
db.collection.find().sort({<field>:1})
Returns all documents sorted by the specified field in ascending order
db.collection.find().sort({<field>:-1})
Returns all documents sorted by the specified field in descending order
db.collection.find().limit(<num>)
Returns only the first <num>
documents
db.collection.find().skip(<num>)
Skips the first <num>
documents and returns the rest
db.collection.insertOne({<field>:<value>})
Inserts a new document into the collection
db.collection.insertMany([{<field1>:<value1>},{<field2>:<value2>}])
Inserts multiple documents into the collection
db.collection.updateOne({<field>:<value>},{$set:{<field>:<new_value>}})
Updates the first document that matches the query with the new field value
db.collection.updateMany({<field>:<value>},{$set:{<field>:<new_value>}})
Updates all documents that match the query with the new field value
db.collection.deleteOne({<field>:<value>})
Deletes the first document that matches the query
db.collection.deleteMany({<field>:<value>})
Deletes all documents that match the query
db.collection.aggregate([{<stage>},{<stage>}])
Performs a multi-stage aggregation operation on the collection
Note: Replace <field>
, <value>
, <new_value>
, <num>
, <stage>
, and collection
with the actual field, value, new value, number, stage, and collection names, respectively.
Last updated
Was this helpful?