Components All New MacOS Windows Linux iOS
Examples Mac & Win Server Client Guides Statistic FMM Blog Deprecated Old

MongoDB.UpdateOne

Updates at most one document in collection that matches selector.

Component Version macOS Windows Linux Server iOS SDK
MongoDB 12.3 ✅ Yes ✅ Yes ✅ Yes ✅ Yes ✅ Yes
MBS( "MongoDB.UpdateOne"; MongoDBRef; selectorJSON; updateJSON { ; OptionsJSON } )   More

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().
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" ) ]
Set Variable [ $r ; Value: MBS( "MongoDB.SetURI"; $Mongo; "mongodb://localhost/" ) ]
Set Variable [ $r ; Value: MBS( "MongoDB.Connect"; $Mongo) ]
Set Variable [ $r ; Value: MBS( "MongoDB.OpenDatabase"; $Mongo; "local" ) ]
Set Variable [ $r ; Value: MBS( "MongoDB.OpenCollection"; $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"; $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

Example Databases

Blog Entries

This function checks for a license.

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


MongoDB.UpdateMany - MongoDB.WatchClient