当前位置:  开发笔记 > 编程语言 > 正文

根据MongoDB中的字典键过滤文档

如何解决《根据MongoDB中的字典键过滤文档》经验,为你挑选了1个好方法。

我在MongoDB中有一系列文章,它们具有以下结构:

{
    'category': 'Legislature', 
    'updated': datetime.datetime(2010, 3, 19, 15, 32, 22, 107000), 
    'byline': None, 
    'tags': {
        'party': ['Peter Hoekstra', 'Virg Bernero', 'Alma Smith', 'Mike Bouchard', 'Tom George', 'Rick Snyder'], 
        'geography': ['Michigan', 'United States', 'North America']
    }, 
    'headline': '2 Mich. gubernatorial candidates speak to students', 
    'text': [
        'BEVERLY HILLS, Mich. (AP) \u2014 Two Democratic and Republican gubernatorial candidates found common ground while speaking to private school students in suburban Detroit', 
        "Democratic House Speaker state Rep. Andy Dillon and Republican U.S. Rep. Pete Hoekstra said Friday a more business-friendly government can help reduce Michigan's nation-leading unemployment rate.", 
        "The candidates were invited to Detroit Country Day Upper School in Beverly Hills to offer ideas for Michigan's future.", 
        'Besides Dillon, the Democratic field includes Lansing Mayor Virg Bernero and state Rep. Alma Wheeler Smith. Other Republicans running are Oakland County Sheriff Mike Bouchard, Attorney General Mike Cox, state Sen. Tom George and Ann Arbor business leader Rick Snyder.', 
        'Former Republican U.S. Rep. Joe Schwarz is considering running as an independent.'
    ], 
    'dateline': 'BEVERLY HILLS, Mich.', 
    'published': datetime.datetime(2010, 3, 19, 8, 0, 31), 
    'keywords': "Governor's Race", 
    '_id': ObjectId('4ba39721e0e16cb25fadbb40'), 
    'article_id': 'urn:publicid:ap.org:0611e36fb084458aa620c0187999db7e', 
    'slug': "BC-MI--Governor's Race,2nd Ld-Writethr"
}

如果我想编写一个查询所有至少有1个地理标记的文章的查询,我该怎么做?我曾尝试编写db.articles.find({'tags':'geography'}),但这似乎不起作用.我也考虑过将搜索参数更改为'tags.geography',但是我有一段时间来搞清楚搜索谓词是什么.



1> kristina..:

如果"geography"字段在其中没有任何标记时不存在(即,在添加位置时创建它),则可以执行以下操作:

db.articles.find({tags.geography : {$exists : true}})

如果它确实存在且为空(即"geography" : []),则应添加geography_size字段或其他内容并执行:

db.articles.find({tags.geography_size : {$gte : 1}})

有关查询的更多信息,请访问http://www.mongodb.org/display/DOCS/Advanced+Queries

推荐阅读
家具销售_903
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有