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 Answer 1

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');
});
Sign up to request clarification or add additional context in comments.

2 Comments

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. How can an ORM do this?
@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 promisejs.org , they have a very good set of explanation

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.