Skip to main content
Version: 8.1

system.mongodb.find

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 list of PyDictionaries that matches the criteria specified on the filter parameter.

Client Permission Restrictions

This scripting function has no Client Permission restrictions.

Syntax

system.mongodb.find(connector, collection, filter, [projection], [sort], [collation], [limit], [skip])

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]
PyDictionarysortA PyDictionary of specified items to sort returned results. See MongoDB documentation for all valid sort values. [optional]
PyDictionarycollationA PyDictionary of items to specify language-specific rules. See MongoDB documentation for all valid collation values. [optional]
IntlimitThe maximum number of PyDictionaries that will be returned. [optional]
IntskipThe number of PyDictionaries to skip before returning results. [optional]

Returns

List[PyDictionary] result - A list of PyDictionary results.

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 documents.
filter = {"qualityControlProcess":"V020"}
project = {"_id":1, "position":1,"elevation":1,"type":1}

# Specify any additional formatting for result.
sort = {"_id":1}
collation = {"locale":"en"}

# Apply all parameters for our function call.
documents = system.mongodb.find(connector, collection, filter, project, sort, collation, limit=5, skip=0)

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

Below is an example on how to use the MongoDB scripting functions for iterating through large datasets without the use of cursors or continuationPoint since it is not provided by the MongoDB connector.

Example #2 - Iterating Through Large Datasets Without Cursors
# Amount of results to return each iteration
pageSize = 5

# Starting with a blank filter for initial query
filter = {}

# Sort must include| `_id` after all other sort items are added
sort = {"year": 1, "_id": 1}

while True:
results = system.mongodb.find("MongoDB", "movies", filter=filter, sort=sort, limit=pageSize)

for result in results:
print result

# get last returned result
lastIndex = len(results) - 1

if lastIndex < 0:
# No results returned
break

lastItem = results[lastIndex]

# filter will continue where the lastItem left off
filter = {
"$or": [
{"year": {"$gt": lastItem["year"]}}, # allow items next in sort
{"year": lastItem["year"], "_id": {"$gt": lastItem["_id"]}} # if sort item is same, get the next item by ObjectID
]
}

if len(results) < pageSize:
break
else:
print "==="

Keywords

system mongodb find, mongodb.find