Mongodb - 查询包含 $all 和 $nin 的标签列表中的元素

Mongodb - query for elements in a list of tags combining $all and $nin

我有一个集合,其中的记录看起来像这样:

{
"_id" : ObjectId("55024bb1f36088d8ce7a79e8"),
"date_taken" : ISODate("2015-01-13T13:50:31.000Z"),
"db_filename" : "2015-01-13 13.50.31.jpg",
"db_original_height" : 3264,
"db_latitude" : -44.66694444444444,
"db_tags" : [ 
    "January", 
    "2015", 
    "Tuesday", 
    "GT-I9300", 
    "SAMSUNG", 
    "New Zealand/Aotearoa", 
    "Southland", 
    "Afternoon", 
    "Medium file"
],
"db_original_path" : "/Users/hingem/my_image_store/imgs/2015-01-13 13.50.31.jpg",
"db_date_taken" : ISODate("2015-01-13T13:50:31.000Z"),
"db_ImageUniqueID" : "ZDFI02\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000",
"db_thumb_path" : "/Users/hingem/thumbs/imgs/2015-01-13 13.50.31_tm.jpg",
"db_flash_fired" : 0,
"db_has_exif" : true,
"db_size" : 3224612,
"db_model" : "GT-I9300",
"db_location" : true,
"db_large_path" : "/Users/hingem/thumbs/imgs/2015-01-13 13.50.31_lg.jpg",
"db_longitude" : 167.9216666666667,
"db_make" : "SAMSUNG",
"db_medium_path" : "/Users/hingem/thumbs/imgs/2015-01-13 13.50.31_md.jpg",
"db_original_width" : 2448,
"db_orientation" : 1,
"db_address" : "Milford Foreshore Walk, Milford Sound, Southland, New Zealand/Aotearoa",
"db_country" : "New Zealand/Aotearoa",
"db_state" : "Southland",
"db_road" : null

}

重要的一点是 db_tags 字段,它是一个标签列表。我想查询包含或排除某些标签的所有记录。我工作很瘦:

db.images.find({'db_tags': 
{'$all': ['Medium file', '2015']}
})

其中 returns 包含这两个标签的所有记录。如何向该查询添加 $nin 子句?所以在伪代码中:

给我所有 db_tag 包含 'Medium file' 和 '2015' 但 db_tags 不包含 'January'

的记录

您可以组合多个查询运算符,使用','分隔它们相当于"and"。

db.images.find( {'db_tags': {'$all': ['Medium file', '2015'],$nin:["January"]} })