Python pymongo SONManipulator查找填充值很少的
使用,可以插入记录。我的自定义对象有5个字段。我需要一个find_one方法,它根据自定义对象中填充的字段进行搜索。假设我创建了一个自定义对象,并且只填充了2个字段(尽管值是唯一的)。我需要使用这个对象和操纵器搜索MongoDB。我如何做到这一点?上面链接中提到的示例使用find_one(),没有任何参数 我在下面尝试过,但没有返回数据Python pymongo SONManipulator查找填充值很少的,python,pymongo,Python,Pymongo,使用,可以插入记录。我的自定义对象有5个字段。我需要一个find_one方法,它根据自定义对象中填充的字段进行搜索。假设我创建了一个自定义对象,并且只填充了2个字段(尽管值是唯一的)。我需要使用这个对象和操纵器搜索MongoDB。我如何做到这一点?上面链接中提到的示例使用find_one(),没有任何参数 我在下面尝试过,但没有返回数据 self.my_collection.find_one({'custom': custom}) #Of course I could specify the c
self.my_collection.find_one({'custom': custom})
#Of course I could specify the complete query. Below fetched the data.
self.my_collection.find_one({'custom.prop1': custom.prop1,'custom.prop2': custom.prop2})
到目前为止,我已经完成了以下工作,以实现同样的目标:
def get_one_by_specified_properties(self, custom):
query_str_map = dict()
for attr in custom_object_properties_names_as_list():
if custom.__getattribute__(attr):
query_str_map.update({'custom.' + attr: custom.__getattribute__(attr)})
document = self.my_collection.find_one(query_str_map)
return document['custom'] if document else None