MCPcopy Create free account
hub / github.com/StackStorm/st2 / construct_mongo_shell_query

Function construct_mongo_shell_query

st2common/st2common/models/utils/profiling.py:103–150  ·  view source on GitHub ↗
(
    mongo_query, collection_name, ordering, limit, only_fields=None
)

Source from the content-addressed store, hash-verified

101
102
103def construct_mongo_shell_query(
104 mongo_query, collection_name, ordering, limit, only_fields=None
105):
106 result = []
107
108 # Select collection
109 part = "db.{collection}".format(collection=collection_name)
110 result.append(part)
111
112 # Include filters (if any)
113 if mongo_query:
114 filter_predicate = mongo_query
115 else:
116 filter_predicate = ""
117
118 part = "find({filter_predicate})".format(filter_predicate=filter_predicate)
119
120 # Include only fields (projection)
121 if only_fields:
122 projection_items = ["'%s': 1" % (field) for field in only_fields]
123 projection = ", ".join(projection_items)
124 part = "find({filter_predicate}, {{{projection}}})".format(
125 filter_predicate=filter_predicate, projection=projection
126 )
127 else:
128 part = "find({filter_predicate})".format(filter_predicate=filter_predicate)
129
130 result.append(part)
131
132 # Include ordering info (if any)
133 if ordering:
134 sort_predicate = []
135 for field_name, direction in ordering:
136 sort_predicate.append(
137 "{name}: {direction}".format(name=field_name, direction=direction)
138 )
139
140 sort_predicate = ", ".join(sort_predicate)
141 part = "sort({{{sort_predicate}}})".format(sort_predicate=sort_predicate)
142 result.append(part)
143
144 # Include limit info (if any)
145 if limit is not None:
146 part = "limit({limit})".format(limit=limit)
147 result.append(part)
148
149 result = ".".join(result) + ";"
150 return result

Calls 1

formatMethod · 0.45

Tested by

no test coverage detected