MongoDBの中に辞書のキーに対するフィルタリングのドキュメント

StackOverflow https://stackoverflow.com/questions/2495932

  •  21-09-2019
  •  | 
  •  

質問

私は、次のような構造を持っているの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