mongoDB - 2 LIMIT and SELECTORS
mongoDB - 2 LIMIT and SELECTORS
mongoDB - 2 LIMIT and SELECTORS
What is MongoDB?
MongoDB is a source-available, cross-platform, document-oriented
database program.
Classified as a NoSQL database product, MongoDB utilizes JSON-like
documents with optional schemas .
What is database?
Structured Data:
show dbs:
use db:
To create a database.
show collections:
db.collection.insert():
Inserts a document or documents into a collection.
db.collection.find():
Selects documents in a collection or view and returns a cursor to the
selected documents.
Document:
Collections:
Database:
A good rule of thumb is to store all data for a single application in the
same database.
Datatype:
{
"name: Nanditha Naveen”,
“address”:{
“street”:”Jai Maruthi Nagar”,
“city”:”Chikmagalur”,
“state”:”Karnataka”
}}
WHERE
OUTPUT
OR
The $or operator is used to specify a compound query with multiple
conditions, where at least one condition must be satisfied for a document to
match.
db.students.find({$or:[{home_city:”City
4”},{gpa:{$gt:3.0}}]});
OUTPUT
AND
The $and operator allows you to specify multiple conditions that documents
must satisfy to match the query.
OUTPUT
CRUD
• C - Create / Insert
• R - Remove
• U - update
• D - Delete
This is applicable for a Collection (Table) or a Document (Row)
INSERT
insertOne()
const studentData = {
“age”: 24,
“gpa”:4.5,
“home_city”:”New York”,
“blood_group”:”O+”,
“is_hotel_resident”:true
}:
UPDATE
UpdateOne()
OUTPUT:
UpdateMany()
• The updateMany() method updates all the documents in
MongoDB collections that match the given query. When you
update your document, the value of the _id field remains
unchanged.
• You can also use this method inside multi-document transactions.
OUTPUT:
DeleteMany()
PROJECTION
only the necessary data rather than selecting the whole set of data
EXAMPLE 1:
db.students.find({}, {_id: 1, gpa: 1});
OUTPUT
EXAMPLE 2 :
OUTPUT
MongoDB Projection Operators
• $slice
• $elemMatch
• $meta
1. $slice operator :
The $slice operator bounds the number of elements that should
be returned as the output of a MongoDB projection query.
element and will not return any other item, especially from
MongoDB 4.4.
MongoDB views.
EXAMPLE:
OUTPUT:
2. $elemMatch :
The $elemMatch operator also limits the contents of an array to
the first element that fits the given constraint. Though, there is a minor
expressions.
EXAMPLE :
db.candidates.find({courses:
{$elemMatch:{$eq:"English"}}},{age:1,"courses.$":1})
;
OUTPUT:
BENEFITS OF PROJECTION:
• We can use this method after the find() method and find() will give
you all the records or documents in the collection. You can also
use some conditions inside the find to give you the result that you
want.
Syntax:
db.collectionName.find(<query>).limit(<number>)
EXAMPLE 1:
db.candidates.find({}, {_id:0}).limit(3);
OUTPUT:
EXAMPLE 2:
Use of sort():
EXAMPLE 3:
db.candidates.find({}, { _id:1}).sort({_id:-1}).limit(4);
OUTPUT
SELECTORS
Comparison gt lt:
OUTPUT
$bitsAllClear :
It matches documents where all the locations of bits specified in
the query are clear—0.
$bitsAllSet :
It matches documents where all the locations of bits specified in
the query are set—1.
$bitsAnyClear :
$bitsAnySet :
• The MongoDB Atlas UI. To learn more, see Query Documents with
MongoDB Atlas.
• MongoDB Compass.
GEOSPATIAL QUERY :
based on proximity.
EXAMPLE :
$geoWithin :
Selects documents with geospatial data that exists entirely within a
specified shape.
$near :
Specifies a point for which a geospatial query returns the
documents from nearest to farthest. The $near operator can specify
either a GeoJSON point or legacy coordinate point.
$nearSphere :
Specifies a point for which a geospatial query returns the
documents from nearest to farthest. $nearSphere requires a geospatial
index.