system.mongodb.find
This function is used in Python Scripting.
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​
Type | Parameter | Description |
---|---|---|
String | connector | The name of connector (case-insensitive). |
String | collection | The name of collection (case-sensitive). |
PyDictionary | filter | A PyDictionary for specifying matching key value pair criteria when querying a collection. |
PyDictionary | projection | A PyDictionary for including or omitting specific key value pairs in the query result. See MongoDB documentation for all valid project values. [optional] |
PyDictionary | sort | A PyDictionary of specified items to sort returned results. See MongoDB documentation for all valid sort values. [optional] |
PyDictionary | collation | A PyDictionary of items to specify language-specific rules. See MongoDB documentation for all valid collation values. [optional] |
Int | limit | The maximum number of PyDictionaries that will be returned. [optional] |
Int | skip | The 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​
# 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.
# 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 "==="