I'm trying to count how many records I have located in each zip code.
In my MongoDB, zip code is embedded; using dot notation, it's located at a.res.z (a for address, res for residential, z for zip). For example, this works just fine:
db.NY.count({'a.res.z' : '14120'})
But when I try the map function (in python, because I'm using PyMongo):
map = Code("function () {"
" emit(this.a.res.z, 1);"
"}")
I get this error when I call mapreduce:
pymongo.errors.OperationFailure: db assertion failure, assertion: 'map invoke failed: JS Error: TypeError: this.a has no properties nofile_b:0', assertionCode: 9014
Dot notation works at the top level - e.g. one dot - but I can't get it to work on embeddeds. What's the secret?
This error means that at least one object you're map-reducing is missing the
res
field of itsa
. See:You can use a
query
argument to map-reduce to operate only on those that have the fields that you want:To use the
query
argument from PyMongo, you can set it as a keyword argument tomap_reduce(...)