I would like to search for a particular value in a series of key/value pairs of arbitrary length and keys in a collection. An example collection:
{
'myJsonObject' : {
'key1' : 'value1',
'key2' : 'foobar',
'key3' : 'value3'
}
}
I want to run a query which returns True
if and only if it finds the string 'foobar'
in any of an arbitrary number of key/value pairs. If working with a python dict, I would write something like:
myJsonObject = {
'key1' : 'value1',
'key2' : 'foobar',
'key3' : 'value3'
}
def check_for_foobar(aDict):
for value in aDict.values():
if 'foobar' == value:
return(True)
return(False)
print(check_for_foobar(myJsonObject))
In this case the function will return True
because foobar
is one of the values.
Is there an analogous way to check every value of a json object of arbitrary length and keys like this in mongodb or pymongo? I am told that the $elemMatch operator could be helpful.
Thank you for any help or guidance.