且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用Node.js在Mongo DB中存储JS函数的问题

更新时间:2023-02-14 12:30:26

Mongo无法存储函数(代码),只能存储数据。获取您获得的原始数据并将其传递给构造函数,或者考虑像mongoose这样的ODM。

Mongo cannot store functions (code), only data. Take the raw data you get back and pass it to a constructor function, or consider an ODM like mongoose.

有一组非常具体且有限的条件,您可以以字符串形式发送mongo函数代码并在服务器上进行评估。这些内容记录在 MongoDB手册中。但是,在mongo中运行的代码的目的是支持复杂的数据查询操作,而不是为应用程序逻辑建模,这就是这个问题所要求的。应用程序逻辑位于node.js模块 .js 文件中。数据在数据库中。

There are a very specific and limited set of conditions wherein you can send mongo function code in string form and have it evaluated on the server. These are documented in the MongoDB manual. However, the purpose of that code running inside mongo is to support sophisticated data query operations, not to model application logic, which is what this question is asking about. Application logic goes in your node.js module .js files. Data goes in the database.