MongoDB watch() 聚合匹配字段值
MongoDB watch() aggregation match by field value
当我在 collection 上使用 watch()
函数时,我正在传递一个聚合来过滤通过的内容。我能够让 operationType
正常工作,但我也只想包含 city
字段等于 Vancouver
的文档。我使用的当前语法不起作用:
change_stream = client.mydb.mycollection.watch([
{
'$match': {
'operationType': { '$in': ['replace', 'insert'] },
'fullDocument': {'city': {'$eq': 'Vancouver'} }
}
}
])
作为参考,这是我正在汇总的字典的样子:
{'_id': {'_data': '825F...E0004'},
'clusterTime': Timestamp(1595565179, 2),
'documentKey': {'_id': ObjectId('70fc7871...')},
'fullDocument': {'_id': ObjectId('70fc7871...'),
'city': 'Vancouver',
'ns': {'coll': 'notification', 'db': 'pipeline'},
'operationType': 'replace'}
我发现我只需要使用一个点来访问嵌套字典:
change_stream = client.mydb.mycollection.watch([
{
'$match': {
'operationType': { '$in': ['replace', 'insert'] },
'fullDocument.city': 'Vancouver' }
}
}
])
当我在 collection 上使用 watch()
函数时,我正在传递一个聚合来过滤通过的内容。我能够让 operationType
正常工作,但我也只想包含 city
字段等于 Vancouver
的文档。我使用的当前语法不起作用:
change_stream = client.mydb.mycollection.watch([
{
'$match': {
'operationType': { '$in': ['replace', 'insert'] },
'fullDocument': {'city': {'$eq': 'Vancouver'} }
}
}
])
作为参考,这是我正在汇总的字典的样子:
{'_id': {'_data': '825F...E0004'},
'clusterTime': Timestamp(1595565179, 2),
'documentKey': {'_id': ObjectId('70fc7871...')},
'fullDocument': {'_id': ObjectId('70fc7871...'),
'city': 'Vancouver',
'ns': {'coll': 'notification', 'db': 'pipeline'},
'operationType': 'replace'}
我发现我只需要使用一个点来访问嵌套字典:
change_stream = client.mydb.mycollection.watch([
{
'$match': {
'operationType': { '$in': ['replace', 'insert'] },
'fullDocument.city': 'Vancouver' }
}
}
])