0

I need to run some havy read & write operations on mongodb. I thought of doing this using a js script on a remote mongodb server, something similar to what's asked in here: How to run mongo db script on remote server?

where the answer says:

mongo -u user -p password mongodb01d.mydomain.com:27017/mydb yourFile.js

but instead, I need to run this from nodejs code, because I am using Azure Functions and I don't have an environment for running mongo client binary.

So I would like to now if doing something like this it is somehow possible:

MongoClient.connect(someUrl, function (err, client) {
    client.load("some script") // <-- Of course client.load doesn't exist.
});

I tried db.eval, which is similar, but it is blocking. I want to run the task in background and don't block my nodejs script, as "load(...)" does when it is run in shell.

Thanks!

1 Answers1

0

You can easily connect to the mongo using an ORM in node like mongoose

var mongoose = require('mongoose');
mongoose.set('debug', true);
mongoose.connect('mongodb://{YOUR_MONGO_URL}:{PORT}/{DB_NAME}', { useNewUrlParser: true }, (err) => {
    if (!err)
        console.log('mongo started');
});
Vaibhav
  • 1,481
  • 13
  • 17
  • As the post says, I need to run long and heavy processes on MongoDB, that means an asyc process that's run on the server side, like when using load from mongo shell: [link](https://docs.mongodb.com/manual/reference/method/load/). How can an ORM do this? – Luis Pablo Linietsky Jul 17 '18 at 20:39
  • @LuisPabloLinietsky - So , the way nodejs works will help you run things in non-blocking way and using mongoose will help you running the operations without managing the mongo server by you manually .Chain everything in promises i.e.., all the mongoose calls for CRUD operations ,hence it'll run in non-blocking manner .Try visiting https://www.promisejs.org/ , they have a very good set of explanation – Vaibhav Jul 18 '18 at 05:45