如何从以下mongo对象中过滤出数据库

时间:2019-07-04 14:19:21

标签: python mongodb

我有一个数据库,其中列出了以下对象

{'documents': [{'tags': ['Procedures', 'Cardiac', 'ECG', None],
   '_id': ObjectId('5cf602f74beb6e2a391b7d95'),
   'key': 'images/333213095:2019-06-04T05:34:46.862Z.jpg',
   'reportedAt': datetime.datetime(2019, 6, 4, 4, 30),
   'updatedAt': datetime.datetime(2019, 6, 4, 5, 34, 47, 907000),
   'createdAt': datetime.datetime(2019, 6, 4, 5, 34, 47, 907000)},
  {'tags': ['Documents', 'Admission notes', None, None],
   '_id': ObjectId('5cf60fa84beb6e46bc1b7dcc'),
   'key': 'images/2851467532:2019-06-04T06:28:55.398Z.jpg',
   'reportedAt': datetime.datetime(2019, 6, 4, 4, 30),
   'updatedAt': datetime.datetime(2019, 6, 4, 6, 28, 56, 34000),
   'createdAt': datetime.datetime(2019, 6, 4, 6, 28, 56, 34000)},
  {'tags': ['Documents', 'Admission notes', None, None],
   '_id': ObjectId('5cf60fb74beb6eac251b7dcf'),
   'key': 'images/3976429268:2019-06-04T06:29:11.269Z.jpg',
   'reportedAt': datetime.datetime(2019, 6, 4, 4, 30),
   'updatedAt': datetime.datetime(2019, 6, 4, 6, 29, 11, 724000),
   'createdAt': datetime.datetime(2019, 6, 4, 6, 29, 11, 724000)},
  {'tags': ['Documents', 'Admission notes', None, None],
   '_id': ObjectId('5cf60fd44beb6e27081b7dd2'),
   'key': 'images/1426882813:2019-06-04T06:29:40.078Z.jpg',
   'reportedAt': datetime.datetime(2019, 6, 4, 4, 30),
   'updatedAt': datetime.datetime(2019, 6, 4, 6, 29, 40, 447000),
   'createdAt': datetime.datetime(2019, 6, 4, 6, 29, 40, 447000)},
  {'tags': ['Blood', 'Hematology', 'CBC', None],
   '_id': ObjectId('5cf6add44beb6e1c001b99fb'),
   'key': '',
   'attributes': {'Hb': '12.8',
    'Platelet': '106000',
    'MCV': '92.6',
    'Neutrophils': '82',
    'Eosinophil': '02',
    'Lymphocytes': '13',
    'Atypical': '00',
    'Total count': '10330',
    'Basophils': '00'},
   'reportedAt': datetime.datetime(2019, 6, 4, 17, 43, 35, 960000),
   'updatedAt': datetime.datetime(2019, 6, 4, 17, 43, 48, 306000),
   'createdAt': datetime.datetime(2019, 6, 4, 17, 43, 48, 306000)}

我需要过滤出所有带有标签

的记录
'tags': ['Blood', 'Hematology', 'CBC', None]

我尝试了以下代码

list(db2.patients.find({'CPMRN':mrn1, 'documents.tags':['Blood', 'Hematology', 'CBC', None]}, {('documents'):1, '_id':0}))

但是它不起作用(没有显示错误,但是过滤后的结果不是我想要的)。

当我尝试这个

list(db2.patients.find({'CPMRN':mrn1, 'documents.tags':'[Blood, Hematology, CBC, None]'}, {('documents'):1, '_id':0}))

结果为空列表

当它是一个字符串列表时,应该如何对查询进行措辞?

非常感谢您

1 个答案:

答案 0 :(得分:0)

我认为问题出在查找查询中。

您将“文档”括在括号()中,但您应在不带括号的情况下传递它。

尝试一下:

db2.patients.find({
    'CPMRN':mrn1, 
    'documents.tags':['Blood', 'Hematology', 'CBC', None]
}, {
    'documents':1, 
    '_id':0
})

您也可以尝试使用$all

db2.patients.find({
    'CPMRN':mrn1, 
    'documents.tags':{$all : ['Blood', 'Hematology', 'CBC', None]}
}, {
    'documents':1, 
    '_id':0
})