interfering with mongo.exe with console

Viewed 76
ProcessStartInfo psi = new ProcessStartInfo();
psi.FileName = "cmd.exe";
psi.Arguments = @"/C cd C:\Program Files\MongoDB\Server\4.2\bin & dir & pause";
using (Process p = Process.Start(psi))
{
p.WaitForExit();
}

With this code, I first open cmd and then run mongo.exe. But after switching to mongo shell, I can't send any commands (with Code). My goal is to automate Mongo ReplicaSet. For this, I need to get input and output by interfering with mongoshell. Thank you in advance for your help.

1 Answers

There is an easier way of doing this.

  1. Start the MongoDB instance and put it in background.
  2. Open a new mongo shell and run your commands.
    • When you like to use the mongo shell then you should use native JavaScript commands, i.e. start shell with javascript file name (see Core Options)
    • If you prefer to do all in C# then you should use the MongoDB C#/.NET Driver
  3. As last command in your shell script put db.getSiblingDB("admin").shutdownServer() - by this the first window will terminate and you don't need any WaitForExit()
Related