I have stucked with the db.eval() function with C# driver
I want to execute a javascript function from middle tier i.e from C# driver
here is my snippet:--
var strs = @"function(x) {return db.users.find();}";
var bson = new BsonJavaScript(strs);
var obj = new EvalArgs();
obj.Code = bson;
var result = db.Eval(obj);
It would great If you guys can figure out what I am doing wrong here.
You don't say what is not working for you... but my guess is that since find() returns a cursor (as opposed to the actual results) what you are returning from the server is not what you expect.
Can you please suggest me ...
Try using:
var result = database.Eval(new EvalArgs
{
Code = @"function() { return db.test.find().toArray(); }"
});
The main difference being adding toArray() after the call to find() to force execution of the query and iteration over the results.
Also, the use of Eval is generally discouraged. There is considerable overhead involved, and when using authentication only users with the highest possible permissions are allowed to use Eval.
Thanks Robert for you reply really appreciating it
I am getting same exception using the given code by you
The returned value is a BsonValue. Some of the properties of BsonValue are only appropriate when the BsonValue is of a certain type (for example, AsBoolean throws an InvalidCastException when the BsonValue is not a BsonBoolean).
The actual type of your return BsonValue should be BsonArray.
You can use AsBsonArray to cast the returned BsonValue to a BsonArray, as in:
foreach (BsonDocument document in result.AsBsonArray)
{
// process document
}
You can also use ToJson if you just want to visualize the result as a JSON string, as in:
Console.WriteLine(result. ToJson());
Thanks for helping Buddy
Perfect solution now got result.
Happy to hear that.
Any other way you do it will be better than using db.eval.
Here is my Code snippet
var json = @" { $match :{$and:[{mapl_checkin_time:{$ gte:{$date:ISO(\'2014-11- 09T06:25:59.273Z\')}}},{mapl_ checkin_flag:true}, {Location : {$within : { $center : [" +
"[" + Convert.ToDouble(placeid. Place_location[0]) + "," + Convert.ToDouble(placeid. Place_location[1]) + "],100" +
@"]
}
}
}
]}
}";
var group = new BsonDocument
{
{
"$group", new BsonDocument
{
{
"_id",
new BsonDocument
{
{"mapl_user_id", "$mapl_user_id"}
}
},
{
"Location", new BsonDocument
{
{
"$last", "$mapl_map_currentlocation"
}
}
}
}
}
};
var match = BsonSerializer.Deserialize< BsonDocument>(json);
var pipeline = new[] { group, match };
var response= dbMaplocation.Collection. Aggregate(pipeline);
And now that you have it working... you should probably find a way to NOT use Eval. We generally discourage use of Eval.
I would also like to know why you are using db.Eval? It's generally a bad idea, and it seems completely unnecessary here - why wouldn't you just run "db.users.find()" directly from C# code using the C# driver? What is the reason to use Eval here?
Thanks for concern. I want to run javscript script code on mongo shell that will perform scheduled bulk operation for inserting ,updating and then fetching records in one go.
If there is any other best possible way to do please let me know. I would like to implement it.Any other way you do it will be better than using db.eval.
I'm not sure what you mean by "in one go" - if you mean atomically, all-or-nothing then you have chosen the wrong database for the job.
Scenarios is like I have to get 1000 records and then update those record. I want to do it with single operation from middletier. So I decided to use Eval function, just passing the jsScript from the middle tier and it will execute in sinlge command.
I woluld like to hear from you if there is any other best possible way to do it...
I have stucked with Datetime query
I have date and time field in my collection named mapl_checkin_time.I am running simple greater then query
I have tried passing
ISO(\'2014-11-09T06:25:59. 273Z\') ,
ISODate(\'2014-11-09T06:25:59. 273Z\')
new Date(2010, 3, 1)
ISO(\'2014-11-09T06:25:59.
ISODate(\'2014-11-09T06:25:59.
new Date(2010, 3, 1)
Nothing works really got stucked
Here is my Code snippet
var json = @" { $match :{$and:[{mapl_checkin_time:{$
@"]
}
}
}
]}
}";
var group = new BsonDocument
{
{
"$group", new BsonDocument
}
};
var match = BsonSerializer.Deserialize<
var pipeline = new[] { group, match };
var response= dbMaplocation.Collection.
Here is error I am getting
You cannot do it "in a single operation" - that's not how MongoDB works. It will execute as many operations as there are - updating 1000 records is 1,000 updates on the server, it doesn't matter how you send them to the server.
If it's important that those updates happen all at once, all or nothing, then you cannot use MongoDB for the job. It's that simple.
댓글 없음:
댓글 쓰기