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?

CodyBugstein
  • 21,984
  • 61
  • 207
  • 363

4 Answers4

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.

Mika Sundland
  • 18,120
  • 16
  • 38
  • 50
0

hey see this article maybe helps :

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

just test :

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

shahabvshahabi
  • 937
  • 2
  • 7
  • 18
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
Tegbir Singh
  • 61
  • 1
  • 1
0

If someone is using Docker container:

docker exec -it mongodb_container sh -c "mongo db_name --eval 'db.dropDatabase()'"
Matko
  • 3,386
  • 4
  • 21
  • 35