Assingment Group B17
Assingment Group B17
Description
Builds the index in the background so that building
an
backgrou
index does not block other database activities.
Boolean Specify true
nd
to build in the background. The default value is
false.
Creates a unique index so that the collection will not
accept
unique
Boolean insertion of documents where the index key or keys
match
an existing value in the index. Specify true to create
a
unique index. The default value is false.
>db.mycol.ensureIndex({"title":1})
In ensureIndex() method you can pass multiple fields, to create
index on multiple fields.
>db.mycol.ensureIndex({"title":1,"description":-1})
MongoDB Commands:
1. use Command:
MongoDB use DATABASE_NAME is used to create database. The
command will create a new database, if it doesn't exist otherwise it will
return the existing database.
Syntax:
use DATABASE_NAME
Example:
If you want to create a database with name <mydb>, then use
DATABASE statement would be as follows:
>use mydb
switched to db mydb
To check your currently selected database use the command db
>db
my
db
If you want to check your databases list, then use the command
show dbs.
>show dbs
local 0.78125GB
test 0.23012GB
Your created database (mydb) is not present in list. To display
database you need to insert atleast one document into it.
>db.movie.insert({"name":"MandarM"
})
>show dbs
0.78125G
local B
0.23012
mydb
GB
test 0.23012GB
In mongodb default database is test. If you didn't create any
database then collections will be stored in test database.
2. The dropDatabase() Method:
MongoDB db.dropDatabase() command is used to drop a existing
database.
Syntax:
db.dropDatabase()
This will delete the selected database. If you have not selected any
database,
then it will delete default 'test' database
Example:
First, check the list available databases by using the command
show dbs
>show dbs
local
0.78125GB
mydb
test
0.23012GB
0.23012GB
>
Paramet
er
Type
Description
Name
String
Options
Examples:
Basic syntax of createCollection() method without options is as
follows
>use test
switched to
db test
>db.createCollection("mycollection")
{ "ok" : 1 }
>
You can check the created collection by using the command show
collections
>show
collections
mycollection
In mongodb you don't need to create collection. MongoDB creates
collection automatically, when you insert some document.
>db.
mandar.insert({"name":"MandarMokash
i "}) >show collections
mycol
mycollection
system.index
es Mandar
>
>use mydb
switched to db
mydb >show
collections mycol
mycollection
system.index
es
MandarMoka
shi
>
Now drop the collection with the name
mycollection >db.mycollection.drop()
true
>
Again check the list of collections into database
>show
collections
mycol
system.indexes
MandarMokashi
>
5. The insert() Method:
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({
_id:
ObjectId(7df78ad8902c),
title: 'MongoDB
Overview',
'http://www.dypic.in',
tags:
{
title: 'MongoDB Overview',
description: 'MongoDB is no sql
database', by: MandarMokashi,
url:
'http://www.dypic.in',
tags:
{
title: 'NoSQL Database',
description: 'NoSQL database doesn't have
tables', by: MandarMokashi,
url:
'http://www.dypic.in',
tags:
Syntax:
>db.mycol.find().pretty()
Example
>db.mycol.find().pretty()
{
"_id": ObjectId(7df78ad8902c),
"title": "MongoDB Overview",
"description": "MongoDB is no sql
database", "by": "MandarKorade",
"url": "http://www.dypic.in", "tags":
["mongodb", "database", "NoSQL"],
"likes": "100"
}
>
8. OR in
MongoDB
Syntax:
To query documents based on the OR condition, you need to use
$or keyword.
>db.mycol.find(
{
$or: [
{key1: value1}, {key2:value2}
]
}
).pretty()
Example
Below given example will show all the tutorials written by
MandarMokashi or whose title is 'MongoDB Overview'
>db.mycol.find({$or:[{"by":"MandarKorade"},{"title": "MongoDB
Overview"}]}).pretty()
{
"_id": ObjectId(7df78ad8902c),
"title": "MongoDB Overview",
"description": "MongoDB is no sql
database", "by": "MandarKorade",
"url": "http://www.dypic.in", "tags":
["mongodb", "database", "NoSQL"],
"likes": "100"
}
>
"by": "MandarKorade",
"url": "http://www.dypic.in", "tags":
["mongodb", "database", "NoSQL"],
"likes": "100"
}
>
"_id"
ObjectId(5983548781331adf45ec6),
"title":"NoSQL Overview"}
{
"_id"
:
"title":"MandarKoradeOverview"}
ObjectId(5983548781331adf45ec7),
Following example will set the new title 'New MongoDB Tutorial' of
the documents whose title is 'MongoDB Overview'
>db.mycol.update({'title':'MongoDB Overview'},{$set:{'title':'New
MongoDB Tutorial'}})
>db.mycol.find()
{
"_id"
ObjectId(5983548781331adf45ec5),
"title":"New
ObjectId(5983548781331adf45ec7),
>
By default mongodb will update only single document, to update
multiple you need to set a paramter 'multi' to true.
>db.mycol.update({'title':'MongoDB
{'title':'NewMongoDB
Overview'},{$set:
Tutorial'}},{multi:true})
11.
criteria
"_id"
ObjectId(5983548781331adf45ec6),
"title":"NoSQL Overview"}
{
"_id"
:
"title":"MandarKoradeOverview"}
ObjectId(5983548781331adf45ec7),
ObjectId(5983548781331adf45ec7),
>
If there are multiple records and you want to delete only first record,
then set justOne parameter in remove() method
>db.COLLECTION_NAME.remove(DELETION_CRITERIA,1)
Remove All documents
If you don't specify deletion criteria, then mongodb will delete whole
documents from the collection. This is equivalent of SQL's truncate
command.
>db.mycol.remove()
>db.mycol.find()
>
"_id"
ObjectId(5983548781331adf45ec6),
"title":"NoSQL Overview"}
"_id"
"title":"MandarKoradeOverview"}
ObjectId(5983548781331adf45ec7),
"_id"
ObjectId(5983548781331adf45ec6),
"title":"NoSQL Overview"}
{
"_id"
:
"title":"MandarKoradeOverview"}
ObjectId(5983548781331adf45ec7),
{"title":"MongoDB Overview"}
>
Please note if you don't specify the sorting preference, then sort()
method will display documents in ascending order.
14. The Limit() Method
To limit the records in MongoDB, you need to use limit() method.
limit() method accepts one number type argument, which is number of
documents that you want to displayed.
Syntax:
>db.COLLECTION_NAME.find().limit(NUMBER)
Example
Consider the collection myycol has the following data
{ "_id" : ObjectId(5983548781331adf45ec5), "title":"MongoDB
Overview"}
"_id"
ObjectId(5983548781331adf45ec6),
"title":"NoSQL Overview"}
{
"_id"
:
"title":"MandarKoradeOverview"}
ObjectId(5983548781331adf45ec7),
>
Conclusion:
Here we performed Indexing and querying with MongoDB using
suitable example.