Skip to main content
Version: 8.1

system.mongodb.replaceOne

New in 8.1.28

This function is used in Python Scripting.

note

Project Library scripts will not provide hints for MongoDB system functions unless the Script Hint Scope is set to Gateway. See the Scripting in Ignition page for more details on scripting hints.

Description​

Replaces a document in the collection that matches the filter.

Client Permission Restrictions​

This scripting function has no Client Permission restrictions.

Syntax​

system.mongodb.replaceOne(connector, collection, filter, replacement, [options])

Parameters​

TypeParameterDescription
StringconnectorThe name of connector (case-insensitive).
StringcollectionThe name of collection (case-sensitive).
PyDictionaryfilterA PyDictionary for specifying matching key value pair criteria when querying a collection.
PyDictionaryreplacementNew values to apply for all non-immutable document fields.
PyDictionaryoptionsA PyDictionary for including additional replace configurations. [optional]

Returns​

PyDictionary result - Result of replace action formatted as a PyDictionary with keys ‘acknowledged’, ‘modifiedCount’, ‘matchedCount’, and ‘upsertedId’.

Scope​

Gateway, Perspective Session

Code Examples​

Example #1
# Import required BSON types.
from system.mongodb.types import ObjectId

# Specify unique field values to locate specific document. Here we are specifying the known _id of the document.
filter = {"_id": ObjectId("63fe3941519feb58abb3cda1")}

# Specify required fields in a PyDictionary format for BSON document.
replacement = {
"username": "jdoe2",
"name": "Jane Doe II",
"address": '321 Second Street, City CA 56789',
"birthdate": system.date.getDate(2004, 5, 19),
"email": "jdoe2@email.com",
"active": True,
"accounts": [],
"tier_and_details": {}
}


# Apply parameters for function call.
print system.mongodb.replaceOne("MongoDB", "customers", filter, replacement)

Keywords​

system mongodb replaceOne, mongodb.replaceOne