Thursday 19 February 2015

mongodb : Create collection

In mongoDB, you no need to create a collection, mongoDB creates automatically, when you insert a document.

> use sample;
switched to db sample
>
>
> db.dropDatabase();
{ "dropped" : "sample", "ok" : 1 }
>
> show dbs;
admin  (empty)
local  0.078GB
test   (empty)
>
> use sample;
switched to db sample
>
> show collections;
>
> db.employee.insert({"id":1,"firstName":"Hari Krishna","lastName":"Gurram"})
WriteResult({ "nInserted" : 1 })
>
> show collections;
employee
system.indexes
>

If you want to create a collection explicitly, you can do by using “db.createCollection(collectionName, options)” command.

Syntax 
db.createCollection(name, {capped: <boolean>, autoIndexId: <boolean>, size: <number>, max: <number>, usePowerOf2Sizes : <boolean>} )

Parameter
Type
Description
name
string
The name of the collection to create.
options
document
Optional. Configuration options for creating a capped collection or for pre allocating space in a new collection.

Prevoius                                                 Next                                                 Home

No comments:

Post a Comment