01 Mongodb 10
01 Mongodb 10
Design and Develop MongoDB Queries using CRUD operations. (Use CRUD operations,
SAVE method, logical operators)
Theory:
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.
var myEmployee=
[
{
"Employeeid" : 1,
"EmployeeName" : "Smith"
},
{
"Employeeid" : 2,
"EmployeeName" : "Mohan"
},
{
"Employeeid" : 3,
"EmployeeName" : "Joe"
},
];
db.Employee.insert(myEmployee);
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 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>"
{
...
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.
db.collection.save(
<document>,
{
writeConcern: <document>
}
)
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.
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.
db.COLLECTION_NAME.drop()
We Will insert the Following table using Insert command
> 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