Query and Write Operation Commands

MongoDB Insert command

It inserts one or multiple documents in the collection and also returns a document containing the status of all inputs. The insert method uses the insert command internally, which is provided by MongoDB.

Syntax:

Argument Fields

FieldTypeDescription
insertstringIt is the name of the collection where we want to insert the element.
documentsarrayIt is an array of files that we want to insert into the collection
orderedbooleanIf it sets to true then it returns the result without inserting any remaining documents listed in the insert array when an insert operation fails, and vice versa
writeConcerndocumentIt is a document that defines the write concern of insert command.
bypass
Document
Validation
booleanWe can insert the document that does not meet the validation requirement using this field.

Example:

Let's insert a document into the books collection:

MongoDB Delete Command

We can remove any document from the collection using the delete command. There is multiple delete specification in a single delete command. We cannot use it on the capped collection. The delete command is internally used by the removal method, which is provided by the MongoDB.

Syntax:

Argument Fields

FieldTypeDescription
deletestringIt is the name of the target collection where we want to delete the element.
deletesarrayIt is an array of delete statements on which we want to perform the delete operation.
orderedbooleanIf it sets to true then it returns the result without inserting any remaining documents listed in the insert array when an insert operation fails, and vice versa
writeConcerndocumentIt is a document that defines the write concern of the delete command.
qdocumentIt is the query that matches to delete.
limitintegerWe can limit the matching document to delete using this field. Specify 0 to delete all matching documents and vice versa.
collationdocumentIt is an optional field and used to define the collation used for the operation.
Syntax:
collation: {
   locale: <string>,
   caseLevel: <boolean>,
   caseFirst: <string>,
   strength: <int>,
   numericOrdering: <boolean>,
   alternate: <string>,
   maxVariable: <string>,
   backwards: <boolean>
}

Examples:

The following example deletes a document from the book collection that has the status equal to A by specifying the limit 2.

MongoDB Update command

The update command makes changes to the document in a collection. It contains multiple update statements. It is used by the update method provided by the MongoDB drivers.

Syntax:

Command fields:

FieldTypeDescription
updatestringIt is the name of the target collection where we want to update the array.
updatesarrayIt is the array of update statements to perform the update operation on the given collection.
orderedbooleanIt is an optional field if it is set to true. It will return the result without performing the remaining update operation and vice versa.
writeConcerndocumentIt is a document that expresses the write concern of the update command. It describes the level of acknowledgment requested from MongoDB for write operations to a standalone MongoDB.
bypass
Document
Validation
boolean It enables the update operation to bypass document validation.
qdocumentIt is the query that matches the documents we want to update.
udocumentIt is the document where the update operator expressions are stored.
upsertbooleanIf this field is set to true, then it performs an insert operation if no document matches the query.
multibooleanIt this field is set to true; it will update all the documents that meet the query criteria.
collationdocumentIt specifies language-specific rules for string comparison.
Syntax:
collation: {
   locale: <string>,
   caseLevel: <boolean>,
   caseFirst: <string>,
   strength: <int>,
   numericOrdering: <boolean>,
   alternate: <string>,
   maxVariable: <string>,
   backwards: <boolean>
}
arrayfiltersarrayIt is an array of documents that describe which array elements we want to modify.
hintstring/
document
It is a document that specifies the index to use to support the query predicate.

Example:

Let's create a student's collection

The run command uses the $set and $inc operators to update the status of a document where the member equals "john."

MongoDB find command

The find command is used to execute a query and returns the first group of results and the id of the cursor from which we can construct a cursor.

Syntax:

Command Fields:

FieldTypeDescription
findstringIn this field, we can define the name of the collection.
filterdocumentIt filters the query.
sortdocumentIt is a document that contains the sorting details of the query.
projectiondocumentIt is the document that contains the projection specification to determine which fields to include in the returned documents.
hintstringIt is a document that specifies the index name as the string or the index key pattern.
skippositive integerThis filed contains the number of documents to be skipped.
limitNon-negative integerWe can set the maximum number of documents we want to return.
batchSizeNon-negative integerIt contains the number of documents we want to return in the first batch.
singleBatchbooleanIt contains the details of whether to close the cursor after the first batch of the result.
maxTimeMS+ve integerWe can set the time limit for the processing operation on the cursor.
readConcerndocumentIt specifies the read concern level.
ReadConcern: { level: <value> }
maxdocumentIt contains the upper bound for the given index.
minbooleanIt contains the lower bound for the given index.
returnKeybooleanIf it is true, return only the index keys in the resulting documents.
showRecordIDbooleanIt is used to return the record identifier for each document.
tailablebooleanIt returns a tailable cursor for a capped collection.
awaitDatabooleanIt is used to temporarily block the getMore command on the cursor.
oplogReplaybooleanIt is a command used for replaying a replica set's oplog. For example -
{ find: "data", oplogReplay: true, filter: 
		{ ts: { $gte: new Timestamp(1514764800, 0) } } }
noCursorTimeoutbooleanThis filed to prevent the server from timing out idle cursors.
allowPartialResultsbooleanThis field prevents throwing an error if some shards are unavailable.
collationdocumentIt specifies the collation for the operations
Syntax:
collation: {
   locale: <string>,
   caseLevel: <boolean>,
   caseFirst: <string>,
   strength: <int>,
   numericOrdering: <boolean>,
   alternate: <string>,
   maxVariable: <string>,
   backwards: <boolean>
}

Example:

In the example below, the command sorts the documents in the result set by the name field and limit the result set by six documents.

MongoDB findAndModify command

It modifies and returns a single document at a time. The returned document does not include the modification made on the update by default. We need to use the new option to return the modified document.

Syntax:

Command Fields

document
FieldTypeDescription
querydocumentThe query field contains the same query selectors as used in the db.collection.find() method.
sortdocumentIt defines the sort order of the document.
removebooleanThis field removes the document specified in the query field.
updatedocument/ arrayIt will update the specified document.
newbooleanIf it sets to true, it will return the modified document rather than the original one.
fieldsdocumentIt is a subset of the fields to return. It specifies an inclusion of field with value 1.
fields: { <field1>: 1, <field2>: 1, ... }
upsertbooleanIt is used with the updated field. If it is true, it creates a new document and updates a single document that matches the query. The default value of this filed is false.
bypass
Document
Validation
booleanIt enables findAndModify to bypass document validation during the process.
writeConcerndocumentIt is a document that expresses the write concern for the command.
maxTimeMSintegerIt declares the time limit for the operation.
FindAndModifyStringThis field contains the collection against which we have to run the command.
collationThe collation field allows users to specify language-specific rules for string comparison.
Syntax:
collation: {
   locale: <string>,
   caseLevel: <boolean>,
   caseFirst: <string>,
   strength: <int>,
   numericOrdering: <boolean>,
   alternate: <string>,
   maxVariable: <string>,
   backwards: <boolean>
}
arrayFiltersarrayIt is the array of filter documents that determine that defines which array elements will be modified for the update operation.

Example:






Latest Courses