我的物品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”,但我有一个时间的魔鬼找出搜索谓词会是什么。

有帮助吗?

解决方案

如果当没有在它(即,当你添加一个位置它的创建)的任何标签的“地理”字段不存在,你可以这样做:

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

如果它确实存在,并且是空的(即"geography" : []),你应该添加一个geography_size场或东西,做的:

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

有在 http://www.mongodb.org/display/查询的详细信息DOCS /高级+查询

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top