Hey Guys,
The fun continued here with mongodb next topic, creating collections(tables in RDBMS) and inserting documents(rows in table in RDBMS).
The fun continued here with mongodb next topic, creating collections(tables in RDBMS) and inserting documents(rows in table in RDBMS).
#
|
Task
|
Syntax with Example
|
1 |
Collection |
|
2 |
Creating Collection Method -1 : By using createCollection method |
Using db.createCollection(name,options) function
Syntax:
db.createCollection(<name>,<options>)
In the above
syntax <name> is the name of the collection & options
are various other parameters like capped, size, max and etc.(Refer
official documentation for this).
Example 1: Just creating a
collection Name
> db;
test
>
db.createCollection("myFirstCollection");
{
"ok" : 1 }
> show
collections;
myFirstCollection
nettuts
system.indexes
>
Example 2: Creating collection
with along with document.
>
db.mySecondCollection.save({"firstName":"sadakar","lastName":"pochampall",ID:200});
WriteResult({
"nInserted" : 1 })
>
db.mySecondCollection.save({"firstName":"dolly","lastName":"boray",ID:201});
WriteResult({
"nInserted" : 1 })
>
db.mySecondCollection.find();
{ "_id"
: ObjectId("53fdbdbe268aa77e481c4a6c"), "firstName"
: "sadakar", "lastName" : "pochampall",
"ID" : 200 }
{ "_id"
: ObjectId("53fdbde4268aa77e481c4a6d"), "firstName"
: "dolly", "lastName" : "boray",
"ID" : 201 }
>
|
3 |
Method -2: By using insert method: |
Create documents separately and insert them into collection. Example:
>
i={"firstName":"sadakar","lastName":"pochampalli",id:100};
{
"firstName" : "sadakar", "lastName"
: "pochampalli", "id" : 100 }
>
j={"firstName":"dolly","lastName":"boray",id:101};
{
"firstName" : "dolly", "lastName" :
"boray", "id" : 101 }
>
db.myFirstCollection.insert(i);
WriteResult({
"nInserted" : 1 })
>
db.myFirstCollection.insert(j);
WriteResult({
"nInserted" : 1 })
> show
collections;
myFirstCollection
nettuts
system.indexes
>
db.myFirstCollection.find();
{
"_id" : ObjectId("53fdbaad268aa77e481c4a6a"),
"firstName" : "sadakar", "lastName"
: "pochampalli", "id" : 100 }
{
"_id" : ObjectId("53fdbab2268aa77e481c4a6b"),
"firstName" : "dolly", "lastName" :
"boray", "id" : 101 }
>
|
|
References : |
http://docs.mongodb.org/manual/tutorial/getting-started/ http://docs.mongodb.org/manual/reference/method/db.createCollection/ http://docs.mongodb.org/manual/reference/method/db.collection.insert/ |
4 |
Drop Collection |
Syntax: db.<CollectionName>.drop(); Example: > db.myFirstCollection.drop(); true > |
:-)
No comments:
Post a Comment