MongoDB.live, free & fully virtual. June 9th - 10th. Register Now

eval replacement in 4.2

why eval is removed in 4.2 version and what is alternate to this in 4.2
if you have removed the eval command there is no meaning to use the scriptoperation.

Welcome to the MongoDB Community @Swapnil_Jain!

The eval server command has been deprecated since MongoDB 3.0 and is definitely not recommendable for performance or security reasons. See eval behaviour in the MongoDB 4.0 documentation for some of the caveats.

For example: server-side eval requires full permissions to your deployment, takes a global write lock while evaluating the function, and does not use indexes.

The recommended approach is to write functions using the native MongoDB query and aggregation operators. Successive major server releases have significantly added to available operators and server functionality where eval might previously have been considered.

In MongoDB 3.6+ you can use the $expr query operator to perform more advanced document manipulation using aggregation expressions. MongoDB 4.2 also added support for On-Demand Materialised Views.

If you are doing complex calculations that cannot be expressed with query operators or aggregation, this work is likely better performed in your application code. If reducing network latency is your key goal for server-side scripting, you could consider co-locating your application code with your database instances.

MongoDB Atlas (our cloud MongoDB service) also provides some alternatives with serverless functions and triggers.

Server-side eval is not analogous to stored procedures: concurrency and performance is severely limited.

If you have existing usage of eval and aren’t sure what approach to take, please provide more details on the functions you are replacing.

Regards,
Stennie