Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pymongo.errors.OperationFailure saying ns does not exist

Tags:

mapreduce

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)
like image 518
Roy Avatar asked May 15 '26 06:05

Roy


1 Answers

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.

like image 126
Dongwook Kim Avatar answered May 19 '26 02:05

Dongwook Kim