2010-03-22 76 views
3

字典鍵我有文章的集合MongoDB中具有以下結構過濾文件:對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',但是我有一段時間的魔力來搞清楚搜索謂詞是什麼。

回答

7

如果當沒有在它(即,當你添加一個位置它的創建)的任何標籤的「地理」字段不存在,你可以這樣做:

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

+0

輝煌查詢的詳細信息!謝謝克里斯蒂娜! – Thomas 2010-03-22 22:20:11