Sunday, September 16, 2012

code 13629 error : have undefined in a query expression

Hi,

First see the example :


> db.activity.find();
{ "_id" : ObjectId("505223f63136983d1e072341"), "a" : 1, "b" : 2, "c" : 3 }
{ "_id" : ObjectId("505224123136983d1e072342"), "a" : 11, "b" : 12, "c" : 13 }
{ "_id" : ObjectId("5052241b3136983d1e072343"), "a" : 11, "b" : 12, "c" : 13, "d" : 14 }
{ "_id" : ObjectId("505224213136983d1e072344"), "a" : 11, "b" : 12, "c" : 13, "d" : 15 }
{ "_id" : ObjectId("505224243136983d1e072345"), "a" : 11, "b" : 12, "c" : 13, "d" : 16 }
{ "_id" : ObjectId("505224343136983d1e072346"), "a" : 112, "b" : 22 }

> db.activityTotalPage.find();
{ "_id" : ObjectId("50553c1120c52c10c63c510e"), "a" : 1, "totalPages" : 10 }
{ "_id" : ObjectId("50553c1c20c52c10c63c510f"), "b" : 2, "totalPages" : 11 }
{ "_id" : ObjectId("50553c2620c52c10c63c5110"), "c" : 2, "totalPages" : 12 }
{ "_id" : ObjectId("50553c2c20c52c10c63c5111"), "d" : 2, "totalPages" : 13 }


// getData.js
var x = new Mongo('127.0.0.1:27017');
var db = x.getDB('dbname');

    // i want the record against b

var data = db.activity.find({b:{$ne :null}});
while(data.hasNext())
{
var t = data.next();
var cVal = t.c;
var cc = getCValueAgain(cVal); print(cc);

}


function getCValueAgain(cVal) {
var findc = db.activityTotalPage.find({c:cVal}, {totalPage:1});
return findc.hasNext()  ? findc.next() : '';
}


$  mongo 127.0.0.1:27017 --quiet /path/getData.js
$  uncaught exception: error: { "$err" : "can't have undefined in a query expression", "code" : 13629 }
failed to load: /path/getData.js

This error response because of the c is null in last record of activity collection .
When we call the function getCValueAgain(cVal which is null), then it show this error .

Ok, so correction is here only :
var data = db.activity.find({b:{$ne :null}, c:{$ne :null}});


Conclusion is that if you are querying on null value then is show the undefined error.

Thanks


No comments:

Post a Comment