0

Can someone explain this to me? All the documentation I'm reading says you just issue a use [database] command and it creates a db in mongo.

But take a look at this:

22:13 $ mongo
MongoDB shell version v3.6.0
connecting to: mongodb://127.0.0.1:27017
MongoDB server version: 3.6.0
Server has startup warnings: 
2017-12-19T08:36:34.901-0600 I CONTROL  [initandlisten] 
2017-12-19T08:36:34.901-0600 I CONTROL  [initandlisten] ** WARNING: Access control is not enabled for the database.
2017-12-19T08:36:34.901-0600 I CONTROL  [initandlisten] **          Read and write access to data and configuration is unrestricted.
2017-12-19T08:36:34.901-0600 I CONTROL  [initandlisten] 
> use spiTest
switched to db spiTest
> show dbs
admin   0.000GB
config  0.000GB
local   0.000GB
> db
spiTest
> 

Also when I run a test against the database using mongodb-core it's throwing an error saying "Database spiTest doesn't exist"

How do I create it?

1
  • 1
    insert atleast one document in any collection inside db . "use spiTest" then "db.sipTestColl.insert({b:1})" Commented Dec 21, 2017 at 4:29

1 Answer 1

0

According to this I need to create a record first? https://www.mkyong.com/mongodb/how-to-create-database-or-collection-in-mongodb/

> db.users.save( {username:"mkyong"} )

If this is correct it's pretty silly.

Leaving this here hoping there's a better answer.

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

2 Comments

The use <db> syntax in the mongo shell selects a database namespace to use and does not explicitly create any data files on disk. As per the MongoDB documentation: "If a database does not exist, MongoDB creates the database when you first store data for that database". This includes inserting documents, creating indexes, creating collections, or any other activities that store data. For example, you could use db.createCollection('foo') to create a new empty collection (and by extension, database).
Whether this is suitable behaviour is a matter of opinion, but this approach means that data files are only created when there is data that needs to be persisted. In the SQL world the actions of "using" and "creating" a database are also separate. MongoDB (as at 3.6) does not have the equivalent of a SQL CREATE DATABASE command and performs this action implicitly, as required.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.