Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Filtering documents against a dictionary key in MongoDB

I have a collection of articles in MongoDB that has the following structure:

{
    '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"
}

If I wanted to write a query that looked for all articles that had at least 1 geography tag, how would I do that? I have tried writing db.articles.find( {'tags': 'geography'} ), but that doesn't appear to work. I've also thought about changing the search parameter to 'tags.geography', but am having a devil of a time figuring out what the search predicate would be.

like image 779
Thomas Avatar asked Mar 22 '10 21:03

Thomas


People also ask

How do I query a nested document in MongoDB?

Accessing embedded/nested documents – In MongoDB, you can access the fields of nested/embedded documents of the collection using dot notation and when you are using dot notation, then the field and the nested field must be inside the quotation marks.

What is the preferred method of querying embedded documents in MongoDB?

Use the $elemMatch operator to query embedded documents. Use conditional operators to query embedded documents. Use Visual Query Builder to query embedded documents.

How does MongoDB filter work?

MongoDB filter is used to filter the data from an array using the specified condition which was we have used in our query. If the document array which was contains the empty value then the result using the filter operator will return the empty array.


1 Answers

If the "geography" field doesn't exist when there aren't any tags in it (i.e., it's created when you add a location), you could do:

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

If it does exists and is empty (i.e., "geography" : []) you should add a geography_size field or something and do:

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

There's more info on queries at http://www.mongodb.org/display/DOCS/Advanced+Queries

like image 159
kristina Avatar answered Oct 23 '22 13:10

kristina