SJ Lovatt
SJ Lovatt

Reputation: 11

MongoDB queries return no results

I'm having a problem with querying a MongoDB dataset ("On Street Crime in Camden" from data.gov.uk)

The database name is Crime_Data_in_Camden and the collection name is Street_Crime_Camden. The query to find all records, db.Street_Crime_Camden.find(), works fine but anything else returns nothing at all. Here is a portion of the metadata:

{     
    "id" : 509935, 
    "name" : "Ward Name",
    "dataTypeName" : "text",
    "fieldName" : "ward_name",
    "position" : 13,
    "renderTypeName" : "text",
    "tableColumnId" : 258836,
    "width" : 100,
    "cachedContents" : {
        "largest" : "West Hampstead",
        "non_null" : 79813,
        "null" : 0,
        "top" : [ {
            "item" : "Regent's Park",
            "count" : 20
        }, {
            "item" : "Swiss Cottage",
            "count" : 19
        }, {
            "item" : "Holborn and Covent Garden",
            "count" : 18
        }
    }
}

I've tried 3 attempts at a basic query:

  1. db.Street_Crime_Camden.find({"ward_name":"West Hampstead"});
  2. db.Street_Crime_Camden.find({'meta.ward_name':'West Hampstead'});
  3. db.Street_Crime_Camden.find({meta:{ward_name:"West Hampstead"} });

According to any documentation or tutorial that I've seen any of these approaches should be valid. And I know that there are hundreds of rows (or documents) that match those terms, so why are these queries returning nothing? Advice would be appreciated.

Upvotes: 1

Views: 1731

Answers (1)

glytching
glytching

Reputation: 47905

The common theme in the three aproaches you tried is some form of ward_name = West Hampstead but there is no attribute named ward_name in the document you shared with us.

Based on the document you show in your question the only way of addressing an attribute with the value West Hampstead is:

db.Street_Crime_Camden.find({"cachedContents.largest": "West Hampstead"});

For background; you address attributes in your documents by using dot notation so the document you included in your question could be found by any of the following find commands:

  • db.Street_Crime_Camden.find({"name": "Ward Name"});
  • db.Street_Crime_Camden.find({"position": 13});
  • db.Street_Crime_Camden.find({"cachedContents.top.item": "Swiss Cottage"});
  • db.Street_Crime_Camden.find({"cachedContents.top.1.count": 20});
  • ... etc

These examples might help you to understand how to form find criteria. The MongoDB docs are also useful.

Upvotes: 1

Related Questions