0

I have a MongoDB collection with the following documents

> db.mycol.find()
{ "_id" : ObjectId("5ec6506171ae442136aa97d2"), "uname" : "mail1", "port" : 1000, "abc" : "test1" }
{ "_id" : ObjectId("5ec659e6c0b1cc11370d8378"), "uname" : "mail2", "port" : 1001, "abc" : "test2" }

In the below code I am trying to fetch an array of document field port by making use of distinct in NodeJS.

var nextPort=0;
MongoClient.connect(url, function(err, db) {
    if (err) throw err;
    var dbo = db.db("mydb");
    dbo.collection("mycol").distinct('port', function(err, result) {
      if (err) throw err;
      console.log(result);
      db.close();
    });
  });

The above code gives an output [ 1000, 1001 ] Now, I have a variable called nextPort I want it to have the next value of already existing ports (should be unique and unused) i.e. if the last element of the array is 1001 then nextPort=1002.

How do I assign the next value (unique) from the array elements to my variable. Also is there a way to have all unique values in the document field port like we have primary key in MySQL

1 Answer 1

1

Not sure if this will help but you can try assigning the output to a variable, say "array". Then use nextPort = array[array.length-1]+1 See code sample below:

let nextPort=0; 
let arr = [1000, 1001];
nextPort = arr[arr.length-1]+1

or if you want to make nextPort an array:

let nextPort=[]; 
let arr = [1000, 1001];
nextPort.push(...arr, arr[arr.length-1]+1);

The code above assumes your Mongo code will always return a number.

Sign up to request clarification or add additional context in comments.

5 Comments

I need your help with another thing. So basically I have a synchronous function (a child process) which makes use of nextPort and since it is a synchronous method it takes precedence over the function where I define the value of nextPort. Need a work around with that. As in the cp the nextPort value is set to 0. I have updated the code. Please have a look at it
So what i want is - make the entire function synchronous MongoClient.connect(url, function(err, db)
mmm. Forgive my ignorance, but why would you want to make it synchronous as opposed to asynchronous? Is your goal to create / start multiple containers in parallel?
Yes, I plan on creating multiple containers. And the MongoDB instance which I plan on provisioning takes port number as nextPort's value. As the child process in a sych function it takes precedence over the fucntion above (nextPort=0). This is the only reason I want the above function to take priority over execSync.
I have no idea. But I am fairly certain mongoconnect is asynchronous, which may actually be a good thing, it can ensure you never overwrite a port number or anything worse. Hope you come right. There are some super clever people here who I am sure can answer without much effort

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.