How to call the MongoDB function from NodeJS?

Viewed 24

I have created a function in the mongodb using the following command : -
db.system.js.save({
    _id: "sp_GetCustomerInfo",
    value: function(){
        return db.customer.find()
    }
})

I am using Mongoose Library in the nodeJS side to perform the operations of mongodb. Now, I want to call this function from NodeJS side. How can I call this?

Please Help
1 Answers

You probably want something like code snippet below. Nevertheless this is not a good approach to query for data from a mongoDB. A best practice would be to just use find() method from mongoose directly.

const conn = mongoose.connect(PATH_TO_DB, err => {
    mongoose.connection.db.eval('sp_GetCustomerInfo()', (err, result) => {
         result;
    })
});
Related