Program3_WM
Program3_WM
a. Execute query selectors (comparison selectors, logical selectors) and list out the results
on any collection
b. Execute query selectors (Geospatial selectors, Bitwise selectors) and list out the results
on any collection
Comparison Selectors: Comparison selectors are used to compare fields against specific
values or other fields. Here are some common comparison selectors:
$ne - Matches all values that are not equal to a specified value.
$gte - Matches values that are greater than or equal to a specified value.
$lte - Matches values that are less than or equal to a specified value.
Logical Selectors: Logical selectors are used to combine multiple conditions in a query. Here
are some common logical selectors:
$and - Joins query clauses with a logical AND and requires that all conditions be true.
$or - Joins query clauses with a logical OR and requires that at least one condition be true.
$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 and requires that none of the conditions be
true.
In MongoDB Shell:
>use Store
> db.customers.insertMany([ { _id: 1, name: "Alice", age: 30, city: "New York" },
{ _id: 2, name: "Bob", age: 25, city: "San Francisco" },
{ _id: 3, name: "Charlie", age: 35, city: "Los Angeles" },
{ _id: 4, name: "David", age: 28, city: "Chicago" },
{_id: 5, name: "Eve", age: 32, city: "Miami" } ])
a. Execute query selectors (comparison selectors, logical selectors) and list out the
results on any collection.
Output:
>db.customers.find({$and: [
{ age: { $gte: 18 } }, // age greater than or equal to 18
{ age: { $lt: 35 } }, // age less than 35
{ city: { $in: ["New York", "Miami"] } } // city is either "New York" or "Miami"
]})
Output:
b. Execute query selectors (Geospatial selectors, Bitwise selectors) and list out the
results on any collection
Under database Store, create a collection places in Mongo DB IDE.
>db.places.insertMany([
{ id: 1, name: "Place A", location: { type: "Point", coordinates: [ -73.97, 40.77 ] } }, // New
York
{ id: 2, name: "Place B", location: { type: "Point", coordinates: [ -122.43, 37.77 ] } }, // San
Francisco
{ id: 3, name: "Place C", location: { type: "Point", coordinates: [ -118.25, 34.05 ] } }, // Los
Angeles
{ id: 4, name: "Place D", location: { type: "Point", coordinates: [ -87.63, 41.88 ] } }, //
Chicago
{ id: 5, name: "Place E", location: { type: "Point", coordinates: [ -80.19, 25.77 ] } } // Miami
])
>db.places.find({ location: {
$near: {
$geometry: {
type: "Point",
}}})
2. Bitwise Selectors
Under database Store, create a collection devices in Mongo DB IDE.
We'll use a collection devices with fields id, name, and status (where status is a
bitwise flag).
>db.devices.insertMany([
])
Bitwise AND Query (Find devices where the 2nd bit is set):
To find all devices where any of the bits at positions 0 or 3 are set (i.e., either the least significant
bit or the fourth bit is set), you can use the $bitsAnySet operator as follows:
1. $bitsAllClear: Matches documents where all of the given bit positions are
clear (i.e., 0).
2. $bitsAllSet: Matches documents where all of the given bit positions are set
(i.e., 1).
3. $bitsAnyClear: Matches documents where any of the given bit positions are
clear (i.e., 0).
4. $bitsAnySet: Matches documents where any of the given bit positions are
set (i.e., 1).
Explanation
• Geospatial Selector:
o $near: Finds documents near a specified point. Requires a 2dsphere index on the
location field.
o $geometry: Specifies the reference point as a GeoJSON object.
o $maxDistance: Limits the distance from the reference point (in meters).
• Bitwise Selector:
o $bitsAllSet: Matches documents where all of the given bit positions are 1.
o $bitsAnySet: Matches documents where any of the given bit positions are 1.
By executing these queries, you can filter documents based on geospatial proximity and bitwise
conditions.