0% found this document useful (0 votes)
18 views8 pages

01 Mongodb 10

m

Uploaded by

saroj1979
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as ODT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views8 pages

01 Mongodb 10

m

Uploaded by

saroj1979
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as ODT, PDF, TXT or read online on Scribd
You are on page 1/ 8

Assignment 10:

Design and Develop MongoDB Queries using CRUD operations. (Use CRUD operations,
SAVE method, logical operators)

Theory:

Inserting multi values in MongoDB

The "insert" command can also be used to insert multiple documents into a collection at one time.
The below code example can be used to insert multiple documents at a time.

The following example shows how this can be done,

var myEmployee=
[

{
"Employeeid" : 1,
"EmployeeName" : "Smith"
},
{
"Employeeid" : 2,
"EmployeeName" : "Mohan"
},
{
"Employeeid" : 3,
"EmployeeName" : "Joe"
},

];

db.Employee.insert(myEmployee);

Saving a particular Document in MongoDB

MongoDB stores data records as BSON documents. BSON is a binary representation of JSON
documents, though it contains more data types than JSON.

MongoDB documents are composed of field-and-value pairs and have the following structure:

{
field1: value1,
field2: value2,
field3: value3,
...
fieldN: valueN
}

The value of a field can be any of the BSON data types, including other documents, arrays, and
arrays of documents. For example, the following document contains values of varying types:
var mydoc = {
_id: ObjectId("5099803df3f4948bd2f98391"),
name: { first: "Alan", last: "Turing" },
birth: new Date('Jun 23, 1912'),
death: new Date('Jun 07, 1954'),
contribs: [ "Turing machine", "Turing test", "Turingery" ],
views : NumberLong(1250000)
}

The above fields have the following data types:

_id holds an ObjectId.


name holds an embedded document that contains the fields first and last.
birth and death hold values of the Date type. contribs holds an array of strings. views holds a
value of the NumberLong type.

Field names are strings.

Documents have the following restrictions on field names:

The field name _id is reserved for use as a primary key; its value must be unique in the
collection, is immutable, and may be of any type other than an array.
The field names cannot start with the dollar sign ($) character.
The field names cannot contain the dot (.) character.
The field names cannot contain the null character.

BSON documents may have more than one field with the same name. Most MongoDB interfaces,
however, represent MongoDB with a structure (e.g. a hash table) that does not support duplicate
field names. If you need to manipulate documents that have more than one field with the same
name, see the driver documentation for your driver.

Some documents created by internal MongoDB processes may have duplicate fields, but no
MongoDB process will ever add duplicate fields to an existing user document.
Field Value Limit

For indexed collections, the values for the indexed fields have a Maximum Index Key Length limit.
See Maximum Index Key Length for details.
Dot Notation

MongoDB uses the dot notation to access the elements of an array and to access the fields of an
embedded document.
Arrays

To specify or access an element of an array by the zero-based index position, concatenate the array
name with the dot (.) and zero-based index position, and enclose in quotes:

"<array>.<index>"

For example, given the following field in a document:

{
...
contribs: [ "Turing machine", "Turing test", "Turingery" ],
...
}

To specify the third element in the contribs array, use the dot notation "contribs.2".

Definition

db.collection.save()

Updates an existing document or inserts a new document, depending on its document parameter.

The save() method has the following form:

db.collection.save(
<document>,
{
writeConcern: <document>
}
)

Parameter Type Description


document document A document to save to the
collection.
writeConcern document Optional. A document
expressing the write concern.
Omit to use the default write
concern. See Write Concern.

Do not explicitly set the write concern for the operation if run in a transaction. To use write concern
with transactions, see Transaction Options (Read Concern/Write Concern).

The save() method uses either the insert or the update command, which use the default write
concern. To specify a different write concern, include the write concern in the options parameter.
Insert

If the document does not contain an _id field, then the save() method calls the insert() method.
During the operation, the mongo shell will create an ObjectId and assign it to the _id field.

Insert

If the document does not contain an _id field, then the save() method calls the insert() method.
During the operation, the mongo shell will create an ObjectId and assign it to the _id field
Update

If the document contains an _id field, then the save() method is equivalent to an update with the
upsert option set to true and the query predicate on the _id field.

Using Regular Expression to find a record in Mongodb

Select All Documents in a Collection


db.inventory.find( {} )
OR
db.inventory.find()
 Specify Equality Condition
 use the query document { <field>: <value> }
 Examples:
db.stud.find( name: “Jiya" } )
db.stud.find( { _id: 5 } )

Comparison Operators

Operator Description
$eq Matches values that are equal to a specified value
$gt Matches values that are greater than a specified value.
$gte values that are greater than or equal to a specified value.
$lt Matches values that are less than a specified value.
$lte Matches values that are less than or equal to a specified value
$ne Matches all values that are not equal to a specified value
$in Matches any of the values specified in an array.
$nin Matches none of the values specified in an array.

Logical Operators:

Operator Description
$and Joins query clauses with a logical AND returns all documents that match the
conditions of both clauses.
$not Inverts the effect of a query expression and returns documents that do not match
the query expression.
$nor Joins query clauses with a logical NOR returns all documents that fail to match
both clauses.
$or Joins query clauses with a logical OR returns all documents that match the
conditions of either clause.

Element

Operator Description
$exists Matches documents that have the specified field.
$type Selects documents if a field is of the specified type.

Evaluation

Operator Description
$expr Allows use of aggregation expressions within the query language.
$json Validate documents against the given JSON Schema.
$mod Performs a modulo operation on the value of a field and selects
documents with a specified result.
$regex Selects documents where values match a specified regular expression.

Removing from Database

Remove All Documents


db.inventory.remove({})
Remove Documents that Match a Condition
db.inventory.remove( { type : "food" } )
Remove a Single Document that Matches a
Condition
db.inventory.remove( { type : "food" }, 1 )

The drop() Method

MongoDB's db.collection.drop() is used to drop a collection from the database.


Syntax

Basic syntax of drop() command is as follows −

db.COLLECTION_NAME.drop()
We Will insert the Following table using Insert command

Roll No Name Branch Marks


201 Pallavi Comp 960
302 Kavita Civil 850
405 Pooja Entc 1002
108 Abhijit Mech 904

> var Stud=


... [
...
... {
... "Roll" : 201,
... "Name" : "Pallavi",
... "Branch" : "Comp",
... "Marks" : 960
... },
...
... {
... "Roll" : 302,
... "Name" : "Kavita",
... "Branch" : "Civil",
... "Marks" : 850
... },
...
... {
... "Roll" : 405,
... "Name" : "Pooja",
... "Branch" : "Entc",
... "Marks" :1002
... },
... {
... "Roll" : 108,
... "Name" : "Abhijit",
... "Branch" : "Mech",
... "Marks" : 904
... },
... ];

> db.Student.insert(Stud)
BulkWriteResult({
"writeErrors" : [ ],
"writeConcernErrors" : [ ],
"nInserted" : 4,
"nUpserted" : 0,
"nMatched" : 0,
"nModified" : 0,
"nRemoved" : 0,
"upserted" : [ ]
})

> db.Student.find().pretty()
{
"_id" : ObjectId("5b936e207ee924f248ecc1bb"),
"Roll" : 201,
"Name" : "Pallavi",
"Branch" : "Comp",
"Marks" : 960
}
{
"_id" : ObjectId("5b936e207ee924f248ecc1bc"),
"Roll" : 302,
"Name" : "Kavita",
"Branch" : "Civil",
"Marks" : 850
}
{
"_id" : ObjectId("5b936e207ee924f248ecc1bd"),
"Roll" : 405,
"Name" : "Pooja",
"Branch" : "Entc",
"Marks" : 1002
}
{
"_id" : ObjectId("5b936e207ee924f248ecc1be"),
"Roll" : 108,
"Name" : "Abhijit",
"Branch" : "Mech",
"Marks" : 904
}
> db.Student.find({"Roll":{$gt:300}})
{ "_id" : ObjectId("5b936e207ee924f248ecc1bc"), "Roll" : 302, "Name" : "Kavita", "Branch" :
"Civil", "Marks" : 850 }
{ "_id" : ObjectId("5b936e207ee924f248ecc1bd"), "Roll" : 405, "Name" : "Pooja", "Branch" :
"Entc", "Marks" : 1002 }

> db.Student.find({"Marks":{$gt:800}})
{ "_id" : ObjectId("5b936e207ee924f248ecc1bb"), "Roll" : 201, "Name" : "Pallavi", "Branch" :
"Comp", "Marks" : 960 }
{ "_id" : ObjectId("5b936e207ee924f248ecc1bc"), "Roll" : 302, "Name" : "Kavita", "Branch" :
"Civil", "Marks" : 850 }
{ "_id" : ObjectId("5b936e207ee924f248ecc1bd"), "Roll" : 405, "Name" : "Pooja", "Branch" :
"Entc", "Marks" : 1002 }
{ "_id" : ObjectId("5b936e207ee924f248ecc1be"), "Roll" : 108, "Name" : "Abhijit", "Branch" :
"Mech", "Marks" : 904 }

> db.Student.find({"Marks":{$lt:900}})
{ "_id" : ObjectId("5b936e207ee924f248ecc1bc"), "Roll" : 302, "Name" : "Kavita", "Branch" :
"Civil", "Marks" : 850 }
> db.Student.find({$or:[{Roll:302},{Name:"Abhijit"}]})
{ "_id" : ObjectId("5b936e207ee924f248ecc1bc"), "Roll" : 302, "Name" : "Kavita", "Branch" :
"Civil", "Marks" : 850 }
{ "_id" : ObjectId("5b936e207ee924f248ecc1be"), "Roll" : 108, "Name" : "Abhijit", "Branch" :
"Mech", "Marks" : 904 }

db.Student.find({"Roll":{"$exists":true}})
{ "_id" : ObjectId("5b936e207ee924f248ecc1bb"), "Roll" : 201, "Name" : "Pallavi", "Branch" :
"Comp", "Marks" : 960 }
{ "_id" : ObjectId("5b936e207ee924f248ecc1bc"), "Roll" : 302, "Name" : "Kavita", "Branch" :
"Civil", "Marks" : 850 }
{ "_id" : ObjectId("5b936e207ee924f248ecc1bd"), "Roll" : 405, "Name" : "Pooja", "Branch" :
"Entc", "Marks" : 1002 }
{ "_id" : ObjectId("5b936e207ee924f248ecc1be"), "Roll" : 108, "Name" : "Abhijit", "Branch" :
"Mech", "Marks" : 904 }

>db.Student.find({"Dept":{"$exists":true}})

> db.Student.drop()
true

You might also like