MongoDB/pymongo:在任意长度集合中检查一个未知键的字符串
我想在集合中的一系列任意长度和键中搜索特定值。一个示例集合:
{
'myJsonObject' : {
'key1' : 'value1',
'key2' : 'foobar',
'key3' : 'value3'
}
}
我想运行一个查询,该查询在 true 时才仅在任意数量的键/值对中找到字符串'foobar'
。如果使用python dict,我会写一些类似的内容:
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))
在这种情况下,函数将返回true
,因为foobar
是其中之一。 是否有一种类似的方法来检查MongoDB或Pymongo中的任意长度和此类键的JSON对象的每个值?有人告诉我, $ elemmatch 操作员可能会有所帮助。 感谢您的任何帮助或指导。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论