0

I'm trying to write a mongo shell script that will delete some entries in an existing DB, and then reload them (depending on some conditions), and I'm having a lot of trouble.

One thing I can't understand/figure out is this: To run the script file I'm making (let's call it script.js), I'm reading (from mongo docs and other places) that I need to use a command like this:

mongo mongodb://{{mycreds}}@{{address of existing mongo}}:27017/{{DB I want}}?authSource=admin script.js

However, since I'm connecting to that same DB in the script itself, I'm also seeing that I need to make that connection in the script.js:

db = connect(
  "mongodb://{{mycreds}}@{{address where the existing mongo is}}:27017/{{DB I want}}?authSource=admin"
);

Why do I have to specify the DB connection when running the script? It's in the script?

Either way, I'm not able to make the connection in the script. I don't know if I'm running the script incorrectly, or making the connection to the DB in the script incorrectly. Or just something else.

In the docs, it says I can run the script in the mongo shell as well. But don't I have to have a mongo instance running at 27017 to start the script? And do I need a mongo running on my machine (since there's already one that exists that I need to connect to)?

I would really appreciate some clarity in this. All I'm trying to figure out is how to run a mongo script that connects to an existing DB, and I'm getting really tangled in all these docs.

Edit:

After receiving a comment (thanks Joe), I removed the connection in the script file and was able to make the connection. I guess having both was messing it up. I'd still like to be able to have the connection in the script, but not when I run the script. I want to do this so that others can run the script without entering the long connection address.

If anyone knows a way to do this, I'd appreciate the help. Thanks.

2
  • 1
    You shouldn't need to connect again inside the script. Have you tried without that? Commented Apr 23, 2020 at 0:46
  • I tried removing that from the script, and it worked. I guess having both was messing it up. I'd like to be able to have the connection in the script, but not when I run the script. However, I suppose this way works. Thanks. Commented Apr 23, 2020 at 13:25

1 Answer 1

1

you always need a credential for any DB you want to connect even if it's SQL. if you want to make it easier for you, you need to create a file.js in vs code and put a credential in a file when you need it you can just call it that's it, and use roboMongo to make your life easier with MongoDB.

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

1 Comment

That credentials file is a good idea, but this isn't a credentials issue. Turns out having a connection in the script, while also making the connection when running the script lead to issues. So, I've removed the connection in the script, and it's working now. I would still like to be able to have the connection in the script, but not when I run the script. However, I suppose this way works. Thanks

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.