开发者

MongoDB : '$err: name has to be a string' with $where

I've been trying to use the $where functionality with a Mongo database but I always get the same error message each time...

$err: name has to be a string

It doesn't matter what type I actually use (string eval, function, etc) - I get the same message each time. I've even gone as far as to try the examples listed on their website and still have the same error message.

Other queries work fine - it is just the $where function th开发者_运维问答at has this error.

Running: Ubuntu on VirtualBox with latest MongoDB stable release

To clarify, I'm using both the shell and drivers with the same error. I'm using examples similar to those found on the website.

(From the Mongo website)

db.myCollection.find( { $where: "this.a > 3" });
db.myCollection.find( "this.a > 3" );
db.myCollection.find( { $where: function() { return this.a > 3;}});


This can be attributed to a malformed entry in the system.js collection for the given database. The case I've seen specifically in system.js is a record without a "string" name, like this:

{ "_id" : ObjectId( "4dee4ee586da7d1330b33d87" ), "value" : function (foo) ...

Remove that record from system.js and your $where functions should work again.

The LONG answer, or how to reproduce this:

Create a new db:

> use TempTest 

Create a test collection:

db.createCollection("myTestCollection")

{ "ok" : 1 }

Populate the test collection with some data:

db.myTestCollection.save( { a : 1 } )

Query using $where:

db.myTestCollection.find( { $where : "this.a == 1" } )

We get the expected response: { "_id" : ObjectId("4dee98e26fcbba3e6d0ba2c9"), "a" : 1 }

Now create the system.js collection, which stores js functions for server-side execution:

db.createCollection("system.js")

{ "ok" : 1 }

Insert a record into system.js, without specifying a value for _id:

db.system.js.save( { value : "foo" } )

Re-run the last query, which now results in the infamous error:

db.myTestCollection.find( { $where : "this.a == 1" } )

error: { "$err" : "name has to be a string", "code" : 10209 }

Now remove that rogue row from system.js:

db.system.js.remove()

Run the query again, and see it return the expected response again.

db.myTestCollection.find( { $where : "this.a == 1" } )

{ "_id" : ObjectId("4dee98e26fcbba3e6d0ba2c9"), "a" : 1 }

Hope this helps someone - it drove me crazy today!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜