I'm working on developing a JavaScript application to be executed locally with Node.js. The goal is to allow users to input commands that will manipulate data in a MongoDb connection.
The challenge lies in keeping the MongoDb connection open while Node.js runs, ensuring that user commands are executed as intended.
I've tried using multiple connections but encountered issues with the promise stack. For instance, when a user inserts an object and then tries to delete it afterward, nothing happens.
const MongoClient = require('mongodb').MongoClient;
const uri = require('./mongoConnection.json'); // contains connection info
const mongoClient = new MongoClient(uri.connectionString, { useNewUrlParser: true });
mongoClient.connect(function(err, db){
var dbo = db.db("TestBotDb").collection("Test");
dbo.insertOne({ "data1" : "new data", "data2" : "new data2" });
var result = dbo.findOne({ "data2" : "new data2" });
dbo.deleteOne({ "_id" : result._id});
});
Due to delay in insertions, I'm unsure how to structure this into functions for future use. Waiting for an insertion before attempting deletion while the application runs isn't ideal since I can't predict when a user might choose to insert or delete.