Query and Write Operation CommandsMongoDB Insert commandIt 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 Field | Type | Description |
---|
insert | string | It is the name of the collection where we want to insert the element. | documents | array | It is an array of files that we want to insert into the collection | ordered | boolean | If 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 | writeConcern | document | It is a document that defines the write concern of insert command. | bypass Document Validation | boolean | We 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 CommandWe 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 Field | Type | Description |
---|
delete | string | It is the name of the target collection where we want to delete the element. | deletes | array | It is an array of delete statements on which we want to perform the delete operation. | ordered | boolean | If 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 | writeConcern | document | It is a document that defines the write concern of the delete command. | q | document | It is the query that matches to delete. | limit | integer | We can limit the matching document to delete using this field. Specify 0 to delete all matching documents and vice versa. | collation | document | It 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 commandThe 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: Field | Type | Description |
---|
update | string | It is the name of the target collection where we want to update the array. | updates | array | It is the array of update statements to perform the update operation on the given collection. | ordered | boolean | It is an optional field if it is set to true. It will return the result without performing the remaining update operation and vice versa. | writeConcern | document | It 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. | q | document | It is the query that matches the documents we want to update. | u | document | It is the document where the update operator expressions are stored. | upsert | boolean | If this field is set to true, then it performs an insert operation if no document matches the query. | multi | boolean | It this field is set to true; it will update all the documents that meet the query criteria. | collation | document | It 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>
} | arrayfilters | array | It is an array of documents that describe which array elements we want to modify. | hint | string/ 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 commandThe 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: Field | Type | Description |
---|
find | string | In this field, we can define the name of the collection. | filter | document | It filters the query. | sort | document | It is a document that contains the sorting details of the query. | projection | document | It is the document that contains the projection specification to determine which fields to include in the returned documents. | hint | string | It is a document that specifies the index name as the string or the index key pattern. | skip | positive integer | This filed contains the number of documents to be skipped. | limit | Non-negative integer | We can set the maximum number of documents we want to return. | batchSize | Non-negative integer | It contains the number of documents we want to return in the first batch. | singleBatch | boolean | It contains the details of whether to close the cursor after the first batch of the result. | maxTimeMS | +ve integer | We can set the time limit for the processing operation on the cursor. | readConcern | document | It specifies the read concern level.ReadConcern: { level: <value> } | max | document | It contains the upper bound for the given index. | min | boolean | It contains the lower bound for the given index. | returnKey | boolean | If it is true, return only the index keys in the resulting documents. | showRecordID | boolean | It is used to return the record identifier for each document. | tailable | boolean | It returns a tailable cursor for a capped collection. | awaitData | boolean | It is used to temporarily block the getMore command on the cursor. | oplogReplay | boolean | It is a command used for replaying a replica set's oplog. For example -{ find: "data", oplogReplay: true, filter:
{ ts: { $gte: new Timestamp(1514764800, 0) } } } | noCursorTimeout | boolean | This filed to prevent the server from timing out idle cursors. | allowPartialResults | boolean | This field prevents throwing an error if some shards are unavailable. | collation | document | It 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 commandIt 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 documentField | Type | Description |
---|
query | document | The query field contains the same query selectors as used in the db.collection.find() method. | sort | document | It defines the sort order of the document. | remove | boolean | This field removes the document specified in the query field. | update | document/ array | It will update the specified document. | new | boolean | If it sets to true, it will return the modified document rather than the original one. | fields | document | It is a subset of the fields to return. It specifies an inclusion of field with value 1.fields: { <field1>: 1, <field2>: 1, ... } | upsert | boolean | It 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 | boolean | It enables findAndModify to bypass document validation during the process. | writeConcern | document | It is a document that expresses the write concern for the command. | maxTimeMS | integer | It declares the time limit for the operation. | FindAndModify | String | This field contains the collection against which we have to run the command. | collation | The 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>
} | arrayFilters | array | It is the array of filter documents that determine that defines which array elements will be modified for the update operation. |
Example:
|