Javatpoint Logo
Javatpoint Logo

MongoDB Shell Collection Methods

Following are the MongoDB collection methods that are used in different scenarios.

#1: db.collection.aggregate(pipeline, option)

The aggregate method calculates mass values for the data in a collection/table or in a view.

Pipeline: It is an array of mass data operations or stages. It can accept the pipeline as a separate argument, not as an element in an array. If the pipeline is not specified as an array, then the second parameter will not be specified.

Option: A document that passes the aggregate command. It will be available only when you specify the pipeline as an array.

Command Fields:

Field Type Description
explain boolean The explain field specifies to return the information on the processing of the pipeline.
allowDiskUse boolean The allow disk use field enables you to write to temporary files.
cursor document The initial batch size for the cursor is specified with this field. The value inside this field is the document with the batchSize field.
maxTimeMS non-negative integer Use the time limit for the processing operations on a cursor is specified using this field.
bypassDocument Validation boolean The $out or $merge aggregation stages can be specified using this field. It allows the aggregate collection method to bypass the document validation during the operation.
readConcern document You can specify the read concern level using this field.
collation document The collation field specifies the language specific rules for string comparison.

Example

The examples use the collection library that contains the following documents:

Calculating the sum

Output:

MongoDB Shell Collection Methods

Specifying the collation

#2 db.collection.bulkWrite()

The bulkWrite() method performs multiple write operations with the order of execution control. Array of write operations are executed by this operation. Operations are executed in a specific order by default.

Syntax:

Output:

MongoDB Shell Collection Methods

Execution of Operations

insertOne: It inserts only one document into the collection.


MongoDB Shell Collection Methods

Update one: It updates only one document that matches the filter in the collection.

Output:

MongoDB Shell Collection Methods

Update Many: It updates all the filter matched documents in the collection.

replaceOne: It replaces a single document in the collection that matches the filter.

# 3. db.collection.count(query, option)

The count() method return the number of documents that would match a find method query for the collection or view.

Example:

We will count all the document in the javaTpoint collection using the following operation:

Now, we will count all the documents that Match a Query in the javaTpoint collection with the field tut_dt greater than new Date ('01/01/2015')

Output:

MongoDB Shell Collection Methods

#4. Db.collection.countDocuments(query, options)

The countDocument() method return the number of documents that match the query for a collection or view. it does not use the metadata to return the count.

Syntax:

Examples:

Below example will count the number of all the documents in the javaTpoint collection.

Now, we will count all the documents that Match a Query in the javaTpoint collection with the field tut_dt greater than new Date ('01/01/2015')

#5. db.collection.estimatedDocumentCount()

The estimateddocumentCount() method counts all documents in a collection or view. This method wraps the count command.

Syntax:

Example

The following example will retrieve the count of all the documents in the javaTpoint collection:

#6. db.collection.createIndex()

It can create the indexes on collections

Syntax:

Keys: For an ascending index on a field we need to specify a value of 1 and for the descending index we need to specify a value of -1.

Example

The example below creates an ascending index on the field tut_Date.

The following example shows a compound index created on the tut_Date field and the tut_code field.

The example below will create an index named as category_tutorial. The example creates the index with the collation that specifies the locale fr and comparison strength.

#7. db.collection.createIndexes()

The createIndexes() method creates one or more indexes on a collection.

Syntax:

Keypatterns: It is an array that contains the index specific documents. All the document have field-value pairs. For an ascending index on a field we need to specify a value of 1 and for the descending index we need to specify a value of -1

Example

In the example below we considered a employee collection containing documents that resemble the following:

Output:

MongoDB Shell Collection Methods

Now, the example below creates two indexes on the products collection:

  • Index on the manufacturer field in ascending order.
  • Index on the category field in ascending order.

The above indexes use a collation which specifies the basic fr and comparison strength  as 2.

#8. db.collection.dataSize()

The data size method have a cover around the output of the collStats (i.e. db.collection.stats() ) command.

#9. db.collection.deleteOne()

The deleteOne() method deletes one document from the collection. It replaces the first document that is similar to the filter. You need to use a field that is related to a unique index such as id for perfect deletions.

Syntax:

Example

The orders collection has documents with the following structure:

The following operation deletes the order with _id: objectId ("563237a41a4d6858 2da"):

Output:

MongoDB Shell Collection Methods





Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA