Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can you debug stored javascript functions in MongoDB?

I'm thinking of moving some workflow logic from C# code to stored JS in MongoDB (for example, wen a user sends a message, a bunch or records is to be created in different collections, which right now I do in C#), but I'm concerned whether I would be able to debug that JS code if things don't work correctly.

like image 855
Andrey Avatar asked Jun 23 '11 17:06

Andrey


People also ask

Can we store function in MongoDB?

Do not store application logic in the database. There are performance limitations to running JavaScript inside of MongoDB.

Does MongoDB support JavaScript?

MongoDB supports JavaScript through the official Node. js driver. You can connect your Node. js applications to MongoDB and work with your data.


2 Answers

There isn't a particular facility for that. One thing you could do is run some of that code in the mongo shell, which can execute exactly the same javascript as the server. The shell doesn't have a debugger but with its interactive prompt it would be much easier to try things, inspect variables, etc.

Personally I would not necessarily recommend moving code into the server. Note it is possible to send several write operations (such as inserts) in a row and then after sending several ask for a single acknowledgement. Thus that scenario is not necessarily slow even if there is some nontrivial network latency.

Alternatively you could run C# code on the same server as the mongod process and thereby get extremely low latency on turnarounds of requests. One way to do that would be to make a web server that is written in C# and encapsulates the logic suggested above.

like image 106
dm. Avatar answered Oct 13 '22 23:10

dm.


I guess you can write some debug information into separate collection and see how things are going, but it seems to me that actual debugging is not possible.

like image 41
Andrew Orsich Avatar answered Oct 14 '22 00:10

Andrew Orsich