MongoDB.UpdateOne
-----------------

Updates at most one document in collection that matches selector.

| Component | Version | macOS | Windows | Linux | Server | iOS SDK |
|---|---|---|---|---|---|---|
| [MongoDB](component_MongoDB.md) | [12.3](newinversion123.md) | ✅ Yes | ✅ Yes | ✅ Yes | ✅ Yes | ✅ Yes |

MBS( "MongoDB.UpdateOne"; MongoDBRef; selectorJSON; updateJSON { ; OptionsJSON } ) 

**MBS**( **"MongoDB.UpdateOne";** /\* Updates at most one document in collection that matches selector. \*/  
**$MongoDBRef**; /\* The reference number for the mongo connection.e.g. $MongoDB \*/   
**$selectorJSON**; /\* A JSON containing the query to match the document for updating. \*/   
**$updateJSON**; /\* A JSON containing the update to perform. If updating with a pipeline, a JSON array. \*/   
**$OptionsJSON**) /\* Optional; Options to use. \*/ 

### Parameters

| Parameter | Description | Example | Flags |
|---|---|---|---|
| MongoDBRef | The reference number for the mongo connection. | $MongoDB |  |
| selectorJSON | A JSON containing the query to match the document for updating. |  |  |
| updateJSON | A JSON containing the update to perform. If updating with a pipeline, a JSON array. |  |  |
| OptionsJSON | Options to use. |  | Optional |

### Result

Returns JSON or error.

### Description

Updates at most one document in collection that matches selector.  
To update multiple documents see [MongoDB.UpdateMany](MongoDBUpdateMany.md)().  
Return is a JSON filled out with fields matchedCount, modifiedCount, and optionally upsertedId if applicable. If there is a server error then reply contains either a "writeErrors" array with one subdocument or a "writeConcernErrors" array.   
### Examples

Update one record:

 Set Variable \[ $Mongo ; Value: MBS ( "[MongoDB.New](MongoDBNew.md)" ) \]   
Set Variable \[ $r ; Value: MBS ( "[MongoDB.SetURI](MongoDBSetURI.md)"; $Mongo ; "mongodb://localhost/" ) \]   
Set Variable \[ $r ; Value: MBS ( "[MongoDB.Connect](MongoDBConnect.md)"; $Mongo ) \]   
Set Variable \[ $r ; Value: MBS ( "[MongoDB.OpenDatabase](MongoDBOpenDatabase.md)"; $Mongo ; "local" ) \]   
Set Variable \[ $r ; Value: MBS ( "[MongoDB.OpenCollection](MongoDBOpenCollection.md)"; $Mongo ; "test" ) \]   
\# update one record by setting a new field   
Set Variable \[ $r ; Value: MBS ( "MongoDB.UpdateOne"; $Mongo ; "{\\"Hello\\": \\"World\\"}"; "{\\"$set \\": {\\"OtherField\\": 123}}") \]   
Show Custom Dialog \[ "Result" ; $r \]   
Set Variable \[ $r ; Value: MBS ( "[MongoDB.Release](MongoDBRelease.md)"; $Mongo ) \]  
Update or Insert:

 MBS ( "MongoDB.UpdateOne"; $mongo ;   
// we select record with ID field  
JSONSetElement ( "{}" ; "ID" ; Assets::PrimaryKey ; JSONString );  
// and here pass whole record including all fields we like to move  
JSONSetElement ( "{}" ; "$set " ; Assets::JSON ; JSONObject );  
// and here allow to insert, if record is new  
JSONSetElement ( "{}" ; "upsert" ; True ; JSONBoolean ))  
### See also

- [MongoDB.Connect](MongoDBConnect.md)
- [MongoDB.DeleteOne](MongoDBDeleteOne.md)
- [MongoDB.New](MongoDBNew.md)
- [MongoDB.OpenCollection](MongoDBOpenCollection.md)
- [MongoDB.OpenDatabase](MongoDBOpenDatabase.md)
- [MongoDB.Release](MongoDBRelease.md)
- [MongoDB.ReplaceOne](MongoDBReplaceOne.md)
- [MongoDB.SetURI](MongoDBSetURI.md)
- [MongoDB.UpdateMany](MongoDBUpdateMany.md)

### Example Databases

- [MongoDB/MongoDB Blog](https://www.mbsplugins.eu/MBS-FileMaker-Plugin-Examples/MongoDB/MongoDB%20Blog.shtml#25ScriptAnchor_)
- [MongoDB/MongoDB Test](https://www.mbsplugins.eu/MBS-FileMaker-Plugin-Examples/MongoDB/MongoDB%20Test.shtml#5ScriptAnchor_)

### Blog Entries

- [FileMaker and MongoDB](https://www.mbsplugins.de/archive/2023-03-20/FileMaker_and_MongoDB/monkeybreadsoftware_blog_filemaker)
- [New MongoDB Example](https://www.mbsplugins.de/archive/2022-07-25/New_MongoDB_Example/monkeybreadsoftware_blog_filemaker)

### FileMaker Magazin

- [Ausgabe 1/2025, Seite 25](https://filemaker-magazin.de/neuigkeit/4282-Appetithappen-FMM_202501)

This function checks for a license.

Created 22nd May 2022 , last changed 29th July 2022

  
[MongoDB.UpdateMany](MongoDBUpdateMany.md) - [MongoDB.WatchClient](MongoDBWatchClient.md)

[HTML Version](MongoDBUpdateOne.shtml)