Adbms Unit 3
Adbms Unit 3
MANAGEMENT
SYSTEM
UNIT-3
UNIT-III
Specification Description
}])
MongoDB Aggregation Frameworks & Operations
$Out-
$Out query1(group) :db.c4.aggregate([{$group:{_id:"$name",stunum:{$sum:"$number"}}},{$out:"student_details“
}])
MongoDB Aggregation Frameworks & Operations
$sort- Mongo dB sort
In MongoDB, the sort() method is used to specify the order in which the query returns matching documents from a
collection. It allows users to arrange documents in either ascending (1) or descending (-1) order based on the values
of one or more fields.
MongoDB sort()
The sort() method in MongoDB is used to specify the order in which the query returns matching documents from a
given collection. It must be applied to the cursor before retrieving any documents from the database.
It takes a document as a parameter that contains a field: value pair that defines the sort order of the result set. The
value is 1 or -1 specifying an ascending or descending sort respectively.
If a sort returns the same result every time we perform on the same data, then such type of sort is known as a stable
sort.
If a sort returns a different result every time we perform on the same data, then such type of sort is known
as unstable sort.
MongoDB generally performs a stable sort unless sorting on a field that holds duplicate values.
limit() method with the sort() method, it will return the first m documents, where m is the given limit.
MongoDB can find the result of the sort operation using indexes.
If MongoDB does not find sort order using index scanning, then it uses the top-k sort algorithm.
Syntax:
db.Collection_Name.sort({field_name:1 or -1})
MongoDB Aggregation Frameworks & Operations
$sort- Mongo dB sort- Examples(Ascending order sorting)
db.c4.aggregate([{$sort:{name:1}}]) (or) db.c4.find().sort({number:1})
MongoDB Aggregation Frameworks & Operations
$sort- Mongo dB sort- Examples (Descending Order sort)
db.c4.aggregate([{$sort:{name:-1}}]) (or) db.c4.find().sort({number:-1})
MongoDb Indexing
Indexing i.e., fetching the data in a short span of time without iterating over the whole dataset.
MongoDB is a NoSQL document type database that follows indexing.
Indexes make searching in a collection is easier with the limited number of documents.
A binary tree is the data structure used by an index. In documents, the _id field is a default
index which is automatically created by MongoDB and we are not allowed to drop this index.
Types of index
MongoDB provides different types of indexes that are used according to the data type or
queries. The indexes supported by MongoDB is are as follows:
1. Single field Index
2. Compound Index
3. Multikey Index
Mongo DB Indexing
Single field Indexing – It means index on a single field of a document. This index is helpful for
fetching data in ascending as well as descending order.
Syntax: db.collection.createIndex({“<fieldName>” : <1 or -1>});
Example: Ascending order
db.c4.createIndex({number:1})
Output:number_1
db.c4.find().sort({number:1})
Output: same as sort method.
Example: Decending order
db.c4.createIndex({number:-1})
Output:number_1
db.c4.find().sort({number:-1})
Output: same as sort method.
Mongo DB Indexing
Compound Indexing –combine multiple fields for compound indexing and that will help for
searching or filtering documents in that way. Or in other words, the compound index is an
index where a single index structure holds multiple references.
Syntax: db.<collection>.createIndex( { <field1>: <type>, <field2>: <type2>, … } )
combine the required fields in this pattern. Also the value of these fields is 1(for ascending
order) or -1(for descending order).
Note: Compound indexes may have a single hashed index field but a hashing function is
required by Hashed indexes to compute the hash of the value of the index field.
Example: Ascending order
db.c4.createIndex({age:1,number:1})Output:age_1,number_1
db.c4.find().sort({age:1,number:1}) Output: same as sort method. If the two age fields are same
number then checks the next field for sorting.
Example: Descending order
db.c4.createIndex({age:-1,number:-1})Output:age_1,number_1
db.c4.find().sort({age:-1,number:-1}) Output: same as sort method. If the two age fields are same
number then checks the next field for sorting.
-1,1 and 1 and -1 are also allowable for the field numbers.
Mongo DB Indexing
Partial Indexing –Partial indexes only index the documents in a collection that meet a
specified filter expression. By indexing a subset of the documents in a collection, partial
indexes have lower storage requirements and reduced performance costs for index creation and
maintenance.
use the db.collection.createIndex() method with the partialFilterExpression option.
The partialFilterExpression option accepts a document that specifies the filter condition using:
•$gt, $gte, $lt, $lte expressions,
•equality expressions (i.e. field: value or using the $eq operator),
•$exists: true expression,
•$type expressions,
•$and operator,
•$or operator,
•$in operator
)
Mongo DB Indexing
Partial Indexing
Example:
db.c4.createIndex( { name: 1 }, { partialFilterExpression: { age: { $gt: 26} } }