Skip to main content
Version: 8.1

system.mongodb.findOne

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

Returns a single PyDictionary that matches the criteria specified on the filter parameter.

Client Permission Restrictions

This scripting function has no Client Permission restrictions.

Syntax

system.mongodb.findOne(connector, collection, filter, [projection])

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.
PyDictionaryprojectionA PyDictionary for including or omitting specific key value pairs in the query result. See MongoDB documentation for all valid project values. [optional]

Returns

PyDictionary result - A single PyDictionary as a result.

Scope

Gateway, Perspective Session

Importing Classes

You can import classes from system.mongodb.types like you would other Python classes:

Example

from system.mongodb.types import ObjectId

newObjectId = ObjectId.get()

You can also iterate those system.mongodb.types packages to see all available classes:

Example

for d in dir(system.mongodb.types):
print d

API Docs

MongoDB-specific data types come from the org.bson.types package of the Mongo Java Driver API. The library of classes available for import can be accessed at this link: Mongo Java Driver 3.6.0 API Docs for Package org.bson.types.

Types References

The following values can be referenced for the types parameter at system.mongodb.types:

Binary
Code
CodeWScope
CodeWithScope
Decimal128
INSTANCE
MaxKey
MinKey
ObjectId
Symbol
Timestamp

Code Examples

Example #1
# Retrieve a both a connector name and collection name to query from.
connector = system.mongodb.listConnectorInfo()[0]['name']
collection = system.mongodb.listCollectionNames(connector)[0]


# Specify unique field values to locate specific document.
filter = {"qualityControlProcess":"V020"}
project = {"_id":1, "position":1,"elevation":1,"type":1}

# Apply parameters for function call.
document = system.mongodb.findOne(connector, collection, filter, project)

# Print the result. Here it is expected that all key value pairs specified in the project parameter will be available.
print "Document ID: " + str(document["_id"])
print "Type: " + str(document["type"])
print "Position: " + str(document["position"])
print "Elevation: " + str(document["elevation"])

Keywords

system mongodb findOne, mongodb.findOne