Ø Find all collection names: To find all collection names in mongo
database use the below query.
db.getCollectionNames()
Return Type: Array
Ø Find all documents from collection name:
To find document from collection name use the below query.
db.<<collectionName>>.find()
Return Type: Cursor (i.e. collection of objects)
Ø Find document(s) based on filter criteria:
To find document based on filter criteria use the below query.
db.<<collectionName>>.find({
<field1>: <value1>, <field2>: <value2>, ... })
For the nested
field use the dot(.) operator with value to find the document.
Return Type: Cursor (i.e. collection of objects)
Ø Find document(s) using operators: To create condition with operator
use the below syntax:
db.<<collectionName>>.find({ <field1>: { <operator1>:
<value1> } })
Operators are: $lt,
$gt,
For AND Operator:
Use comma (,)
And
For OR Operator:
Use $OR
db.<<collectionName>>.find(
{ $or: [ {
field1: value1 }, { field2: value2 } ] } )
Return Type: Cursor (i.e. collection of objects)
Ø Sort document in collection: To sort
document in collection use the below query.
db.<<collectionName>>.find().sort({
“field1” : 1, “field2”: -1})
1 is used for
Ascending and -1 is used for Descending.
Return Type: Cursor (i.e. collection of objects)
Note: “Field
Name” is case sensitive
Ø Remove document from collection: To
remove document from collection use the below query.
db.<<collectionName>>.remove({“fieldname”:”value”})