I got an error as following : Traceback (most recent call last):
pymongo.errors.OperationFailure: command SON([('mapreduce', u'tweets'), ('map', Code('function() { emit(this.via, 1); }', {})), ('reduce', Code(' function(key,value) {\n var res = 0;\n values.forEach(function(v) {res += 1 })\n return {count: res};\n }\n ', {})), ('out', 'via_count')]) on namespace Corpus.$cmd failed: ns doesn't exist
The codes are :
from pymongo import MongoClient
from bson.code import Code
con = MongoClient()
db = con.Corpus
tweets = db.tweets
map = Code("function() { emit(this.via, 1); }")
reduce = Code(""" function(key,value) {
var res = 0;
values.forEach(function(v) {res += 1 })
return {count: res};
}
""")
result = tweets.map_reduce(map, reduce, "via_count")
for doc in db.via_count.find():
print(doc)
on namespace Corpus.$cmd failed: ns doesn't exist
It means you don't have collection named 'tweets' or 'via_count' in Corpus.
Not like a raw query in mongodb, map_reduce function doesn't make a new collection if it's not exist.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With