mujaffars
mujaffars

Reputation: 1479

Running mongodb commands through cmd on windows

I have installed mongodb on windows system,

I have run mongod on command prompt

Now in another command prompt I am running mongo command which connected to test database. But when I try to execute show collections command nothing is happening.

I want to know how to trigger show collection, create collection commands from cmd

Mongo command prompt

Upvotes: 4

Views: 14530

Answers (2)

chridam
chridam

Reputation: 103305

To show all the databases present in MongoDB, you need to issue the "show dbs" command at the prompt:

MongoDB shell version: 2.6.5
connecting to: test
> show dbs
admin   0.078GB
local   0.078GB

Only two databases exist i.e. the system dbs 'admin' and 'local'

Next, issue the command "use new-db" to switch from the default database to the defined database "new-db". At this juncture, MongoDB will not automatically create any databases or collections yet until you manually create a collection or save a document inside. So run the following commands

> use new-db
switched to db new-db
> show dbs
admin   0.078GB
local   0.078GB

"new-db" does not show up after the "show dbs" command because it does not have any collections yet, so you need to create a test collection named "people", and insert a document inside. There are a couple of ways you can go about creating a collection, you can either issue the db.createCollection() command:

The following command simply creates a collection named people:

> db.createCollection("people")

Or you can just call the collection's insert() or save() command, which will automatically create the collection and the new-db database:

> db.people.save({"name": "mujaffars"})
WriteResult({ "nInserted" : 1 })
> db.people.find()
{ "_id" : ObjectId("5627430e0899b9f16b9bd781"), "name" : "mujaffars" }

> show dbs
admin  0.078GB
local  0.078GB
new-db  0.078GB

Upvotes: 4

Fariz Aghayev
Fariz Aghayev

Reputation: 651

Just,

> use databaseName; // first you need create own db
> db.createCollection("nameCollection");  // ... own collection show
> collection; // find exists collections  
> db.nameCollection.find(); // find all documents in collection ...

Upvotes: 0

Related Questions