1

I'm trying to create an npm command that drops a database from my mongo instance.

When I paste this line of code directly into my shell, it works, and gives the output below:

> echo 'db.dropDatabase()' | mongo musicappdb

MongoDB shell version v3.4.10
connecting to: mongodb://127.0.0.1:27017/musicappdb
MongoDB server version: 3.4.10
{ "ok" : 1 }
bye

However when I run it from my npm script, which is defined like this

scripts: {
     ...
     "drop-db": "echo 'db.dropDatabase()' | mongo musicappdb",
     ...
  },

I get

> echo "db.dropDatabase()" | mongo musicappdb

MongoDB shell version v3.4.10
connecting to: mongodb://127.0.0.1:27017/musicappdb
MongoDB server version: 3.4.10
db.dropDatabase()
bye

And the database is NOT getting dropped.

Why is the command not behaving properly when I issue through npm?

3
  • hey friend see this article maybe helps : antrikshy.com/blog/run-mongodb-automatically-nodejs-project Commented Nov 27, 2017 at 17:35
  • echo 'db.dropDatabase()' | mongo musicappdb is this command run in termial or mongoshell? Commented Nov 27, 2017 at 17:45
  • cmd terminal (Windows 8) Commented Nov 27, 2017 at 17:46

4 Answers 4

1

This worked for me:

"drop-db": "echo db.dropDatabase() | mongo musicappdb"

Without the single quotes.

It didn't work with either double or single quote. This is on Windows 10.

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

Comments

0

hey see this article maybe helps :

http://antrikshy.com/blog/run-mongodb-automatically-nodejs-project

just test :

'start mongo admin --eval "db.dropDatebase()" '

3 Comments

Thanks. Just tried with mongo musicappdb --eval 'db.dropDatabase()'" but still no luck from npm. Yet it works if I run directly in my terminal
maybe you must be admin to drop your databases
Tried as admin, didn't work. Also it works without me being admin when doing in terminal directly
0
"drop-db": "echo 'db.dropDatabase()' | mongo myDB"

This worked for me

Output was

MongoDB shell version v4.0.9
connecting to: mongodb://127.0.0.1:27017/myDB?gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("44d9deb8-6de1-49ff-8532-24cf2ae5247b") }
MongoDB server version: 4.0.9
{ "ok" : 1 }
bye

Comments

0

If someone is using Docker container:

docker exec -it mongodb_container sh -c "mongo db_name --eval 'db.dropDatabase()'"

Comments

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.