# MongoDB Write

### MongoDBWrite <a href="#mongodbwrite" id="mongodbwrite"></a>

**MongoDBWrite** performs write operations on a Mongo database.

Mongo is a [NoSql database](https://en.wikipedia.org/wiki/NoSQL), which provides a mechanism for storage and retrieval of data that is different to the tabular relations used in relational databases like SQL Server, MySql and Oracle.

***

### Properties <a href="#properties" id="properties"></a>

#### Connection string <a href="#connection-string" id="connection-string"></a>

Configuration values required by the data provider to connect to the data source. [learn more](http://docs.mongodb.org/manual/reference/connection-string/)

#### Collection <a href="#collection" id="collection"></a>

The collection to write to.

#### Operation <a href="#operation" id="operation"></a>

The operation to perform.

Options:

1. *Insert*\
   Inserts a single object (specified in the [data](#properties) property) into the database.
2. *Update*\
   Applies an [update operation](#update) to any documents in the database that matches the [criteria](#update) property.
3. *Replace*\
   Replaces a single document in the database with the object in the[ data](#replace) property. The document to be replaced can either be specified by the[ criteria](#replace) property, or by the [ID attribute](#what-are-id-attributes) of the replacement document.
4. *Delete*\
   Deletes one or more documents in the database which match the [criteria](#delete) property.
5. *Delete All*\
   [Deletes all](#deleteall) records in the collection.

The selection of an Operation will determine which specific properties will display for you to complete.

### Insert

**Data**

The object to be written into the database. The object can be specified either as a JSON formatted string, or as an instance of a *Type*.

### Update

**Criteria**

A MongoDb [query document](http://docs.mongodb.org/manual/tutorial/query-documents/) that specifies the selection criteria to be matched by a document in order for it to have the write operation applied.

For example, if we select from collection of documents with attributes "quantity" and "price", the following criteria will match all documents in the collection with fields "quantity" greater than 100 or "price" less than 9.95.:

`{ $or: [ { quantity: { $gt: 100 } }, { price: { $lt: 9.95 } } ] }`

Click on ... to acess the Criteria Editor to add database fields and variables to the criteria.

**Insert If Not Found**

Controls the action of[ replace](#properties) and [update ](#properties)operations, when no documents are found matching the [criteria ](#replace)property.

* *Checked* - A new document will be created.
* *Unchecked* - No action will be taken.

**Update Operation**

JSON object describing the changes to be made to each database document.

Click on ... to acess the Update Operation Editor, where you can formulate your update operations.

The following example will set an *updated* field to 'true' and increment a *count* field for all matched documents.:

`{ $set: { updated: true }, $inc: { count: 1 } }`

### Replace

**Criteria**

A MongoDb [query document](http://docs.mongodb.org/manual/tutorial/query-documents/) that specifies the selection criteria to be matched by a document in order for it to have the write operation applied.

For example, if we select from collection of documents with attributes "quantity" and "price", the following criteria will match all documents in the collection with fields "quantity" greater than 100 or "price" less than 9.95.:

`{ $or: [ { quantity: { $gt: 100 } }, { price: { $lt: 9.95 } } ] }`

Click on ... to acess the Criteria Editor to add database fields and variables to the criteria.

**Data**

The object to be written into the database. The object can be specified either as a JSON formatted string, or as an instance of a *Type*.

**Insert If Not Found**

Controls the action of [replace ](#properties)and[ update](#properties) operations, when no documents are found matching the [criteria ](#replace)property.

* *Checked* - A new document will be created.
* *Unchecked* - No action will be taken.

### Delete

**Criteria**

A MongoDb [query document](http://docs.mongodb.org/manual/tutorial/query-documents) that specifies the selection criteria to be matched by a document in order for it to have the write operation applied.

For example, if we select from collection of documents with attributes "quantity" and "price", the following criteria will match all documents in the collection with fields "quantity" greater than 100 or "price" less than 9.95.:

`{ $or: [ { quantity: { $gt: 100 } }, { price: { $lt: 9.95 } } ] }`

Click on ... to acess the Criteria Editor to add database fields and variables to the criteria.

### DeleteAll

No specific additional properties.

### What are ID Attributes? <a href="#what-are-id-attributes" id="what-are-id-attributes"></a>

Each document in a MongoDb collection is uniquely identified by an [Object ID](http://docs.mongodb.org/manual/reference/object-id/) field called "\_id". This field is automatically created whenever a new document is inserted into the database, and cannot be changed by [update](#update) or [replace ](#replace)operations.

When using the object id as an element in the criteria field, the string value will need to be converted back to a Mongo object id. For example, a document with id "525bc19331eec126ecdcf199" can be matched by using the [criteria](#update):

`{ _id: ObjectId("525bc19331eec126ecdcf199") }`

When running a[ replace](#replace) operation without specifying any criteria, the function will attempt to replace a document in the database whose id matches a [string](/reference/plugins/linx/content/string.md) field named "id" or "\_id" in the replacement object. This allows objects to be read from the database using the MongoDBRead function, altered and written back to the database without requiring a replacement [criteria](#replace).

***

### Links <a href="#links" id="links"></a>

[Connection string format](http://docs.mongodb.org/manual/reference/connection-string/)

[Aggregation pipeline](http://docs.mongodb.org/manual/core/aggregation-pipeline/)

[Aggregation operators](http://docs.mongodb.org/manual/reference/operator/aggregation/#aggregation-pipeline-operator-reference)

[Query documents](http://docs.mongodb.org/manual/tutorial/query-documents/)

[Update Operators](http://docs.mongodb.org/manual/reference/operator/update/#id1)

[Mongo Object Ids](http://docs.mongodb.org/manual/reference/object-id/)


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.linx.software/reference/plugins/database/content/mongodb-write.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
