Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
7 views10 pages

Oss - Mongo DB2

Mongo DB CURD operations

Uploaded by

KGS-CSE PSG CT
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views10 pages

Oss - Mongo DB2

Mongo DB CURD operations

Uploaded by

KGS-CSE PSG CT
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 10

Create Collection

• MongoDB db.createCollection(name,
options) is used to create collection.
In the command, name is name of collection to be
created. Options is a document and is used to specify
configuration of collection.

Parameter Type Description


Name String Name of the collection to be
created
Options Document (Optional) Specify options about
memory size and indexing

Options parameter is optional, so you need to specify only the


name of the collection.
Following is the list of options you can use −
Field Type Description
(Optional) If true, enables a capped collection.
Capped collection is a fixed size collection that
automatically overwrites its oldest entries
capped Boolean
when it reaches its maximum size. If you
specify true, you need to specify size
parameter also.
autoIndexI (Optional) If true, automatically create index
Boolean
d on _id field.s Default value is false.
(Optional) Specifies a maximum size in bytes
size number for a capped collection. If capped is true, then
you need to specify this field also.
(Optional) Specifies the maximum number of
max number
documents allowed in the capped collection.
Examplex

Use test
db.createCollection(“mycollection”)

show collections
syntax of createCollection() method with few options
>db.createCollection("mycol1", { capped : true,
autoIndexId : true, size : 6142800, max : 10000 } )

In MongoDB, you don't need to create collection. MongoDB


creates collection automatically, when you insert some document.

>db.testing.insert({"name" : “DBtest"})
>show collections
mycol
mycollection
system.indexes
testing
Drop Collection

• db.collection.drop() is used to drop a


collection from the database.

db.COLLECTION_NAME.drop()
db.mycollection.drop()

Show collections
DATA TYPES
• String − This is the most commonly used datatype to
store the data. String in MongoDB must be UTF-8 valid.
• Integer − This type is used to store a numerical value.
Integer can be 32 bit or 64 bit depending upon your
server.
• Boolean − This type is used to store a boolean (true/
false) value.
• Double − This type is used to store floating point values.
• Min/ Max keys − This type is used to compare a value
against the lowest and highest BSON elements.
• Arrays − This type is used to store arrays or list or
multiple values into one key.
• Timestamp − ctimestamp. This can be handy for
recording when a document has been modified
or added.
• Object − This datatype is used for embedded
documents.
• Null − This type is used to store a Null value.
• Symbol − This datatype is used identically to a
string; however, it's generally reserved for
languages that use a specific symbol type.
• Date − This datatype is used to store the current
date or time in UNIX time format. You can specify
your own date time by creating object of Date
and passing day, month, year into it.
• Object ID − This datatype is used to store the
document’s ID.
• Binary data − This datatype is used to store
binary data.
• Code − This datatype is used to store
JavaScript code into the document.
• Regular expression − This datatype is used to
store regular expression.
INSERT
• To insert data into MongoDB collection, you need to use
MongoDB's insert()or save() method.

SYNTAX:
db.COLLECTION_NAME.insert(document)

EXAMPLE:
db.mycol.insert({
title:'MongoDB Overview',
description:'MongoDB is no sql database',
by:‘MONGO DATA BASE',
url: 'http://www.mongodb.com',
tags: ['mongodb', 'database', 'NoSQL'],
likes: 100
})
db.post.insert([ {
title: 'MongoDB Overview',
description: 'MongoDB is no sql database',
by: 'tutorials point',
url: 'http://www.tutorialspoint.com',
tags: ['mongodb', 'database', 'NoSQL'],
likes: 100 },
{ title: 'NoSQL Database',
description: "NoSQL database doesn't have tables",
by: 'tutorials point',
url: 'http://www.tutorialspoint.com',
tags: ['mongodb', 'database', 'NoSQL'],
likes: 20,
comments: [ { user:'user1',
message: 'My first comment',
dateCreated: new Date(2013,11,10,2,35),
like: 0 }
] } ])

You might also like