pymongo-如何匹配查找? [英] pymongo - how to match on lookup?
问题描述
我有两个集合,一个模型和一个论文集合.我需要能够匹配他们两个的字段.它们有一个共同的字段,称为引用,其中包含一个标识符.
I have two collections, a model and a papers collection. I need to be able to match fields from both of them. They have a field in common called reference which contains an identifier.
我要匹配具有以下内容的文档
I want to match documents that have the following
'作者':论文集中的'Migliore M' 'celltypes':模型集合中的'海马CA3锥体细胞'
'authors' : 'Migliore M' from the papers collection 'celltypes' : 'Hippocampus CA3 pyramidal cell' from the models collection
这是我的代码的样子:
pipeline = [{'$lookup':
{'from' : 'models',
'localField' : 'references',
'foreignField' : 'references',
'as' : 'cellmodels'}},
{'$match':
{'authors' : 'Migliore M', 'cellmodels.celltypes' : 'Hippocampus CA3 pyramidal cell'}},
]
for doc in (papers.aggregate(pipeline)):
pprint (doc)
我没有结果.
我注意到,如果不在match参数中调用cellmodels.celltypes,它将找到与Migliore M匹配的论文.我如何才能使其也与细胞类型匹配:模型中的海马CA3锥体细胞"该查询中的收藏吗?
I notice that if I do not call on the cellmodels.celltypes in the match parameter it will find the papers that match Migliore M. How can I get it to also match the celltype:'Hippocampus CA3 pyramidal cell' from the models collection in this query?
推荐答案
此方法有效:
pipeline = [{'$lookup':
{'from' : 'models',
'localField' : '_id',
'foreignField' : 'references',
'as' : 'cellmodels'}},
{'$unwind': '$cellmodels'},
{'$match':
{'authors' : 'Migliore M', 'cellmodels.celltypes' : 'Hippocampus CA3 pyramidal cell'}},
{'$project':
{'authors':1, 'cellmodels.celltypes':1}}
]
for doc in (papers.aggregate(pipeline)):
pprint (doc)
这篇关于pymongo-如何匹配查找?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!