1

I am looking for a way to create a new database and a collection using only the terminal, without using Mongo shell or Mongo Compass GUI.

The idea is to create the db and collection from the terminal before using mongoimport, like:

# Create database `company-example` and collection `employees` from the terminal
# ...
# ...
    
# Import the data to the collection
mongoimport --db=company-example --collection=employees --file=employees.json --jsonArray

I could create a npm package and run it with with Node from the terminal, but I am wondering if there is a simpler way?

4
  • 1
    You can execute a JS file with database and collection create commands without physically logging into mongo shell: Write Scripts for mongo Shell. Commented Feb 3, 2021 at 12:44
  • "The idea is to use it before importing data..." - use it in what way? Commented Feb 3, 2021 at 13:14
  • Thank you for pointing out, that was a typo... The idea is to create the db and collection before using mongoimport.... I updated the question Commented Feb 3, 2021 at 13:57
  • 1
    The import will create the database and the collection (which were not existing before) _ and_ load the data from the JSON. Why do you need to create a database and an empty collection before running the import? Commented Feb 4, 2021 at 9:50

2 Answers 2

1

You can "clone" a database and an empty collection from a predefined database and collection (for the purpose of creating a new database with an empty collection). This is based upon another post by the author: clone collection in mongoDB in the same db.

First, create a database called as a dummyDB, and an empty collection called testColl. This is used to create/clone a database with one collection that is empty.

This command from terminal will create a clone:

mongodump --archive --db=dummyDB --collection=testColl | mongorestore --archive --nsFrom="dummyDB.*" --nsTo="examples.*"

The cloned database is examples and the collection with no documents testColl.

This process is useful in case you are creating the clones again and again.

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

2 Comments

Thanks for taking time to share this prasad_!
I am looking for a solution that my students can run during their first exercise with Mongo, meaning that they don't have any predefined db or collection, but rather need to run a code snippet that will create it for them, "fuss free".
1

I assume that for some reason you would like to have DB and collection created prior to import.

  1. create a script for example employees.js, with content:

db.createCollection("employees", {collation:{locale:"en", strength:2}});

  1. start command from the terminal:

    mongo company-example employees.js

Please notice the first parameter of mongo command is DB name and the second is your script. If DB does not exist it will be created automatically.

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.