MongoDB Operations and Queries Guide
MongoDB Operations and Queries Guide
To find students majoring in either CSE or ECE, use the `$in` logical operator, which checks for any matching value in the specified array. The query would be `db.students.find({ department: { $in: ['CSE', 'ECE'] } })`. This query filters documents where the `department` field value matches either 'CSE' or 'ECE' .
The `$near` operator in MongoDB is used to find documents with geospatial data close to a specified location. To use `$near`, we first ensure the geospatial data has been indexed using the `2dsphere` index. An example query is `db.places.find({ location: { $near: { $geometry: { type: 'Point', coordinates: [77.5946, 12.9716] }, $maxDistance: 1000 } } })`, which identifies places within 1000 meters of a given coordinate. Using `$near` helps in applications like finding nearby amenities or services based on user location .
In MongoDB, insert operations are performed using `insertOne()` to add a single document, such as `db.student.insertOne({ name: 'Rithvik Madugula', age: 21, course: 'Computer Science', grade: 'A' })`, and `insertMany()` to add multiple documents simultaneously. For updates, `updateOne()` and `updateMany()` are used to modify specific documents based on query conditions, exemplified by `db.student.updateOne({ name: 'Rahul Verma' }, { $set: { grade: 'A+' } })`, which updates one student's grade. Deletion operations employ `deleteOne()` and `deleteMany()` methods, such as `db.student.deleteOne({ name: 'Sneha Rao' })` to remove individual documents, and `deleteMany()` for batch deletions .
In MongoDB, projection allows the inclusion or exclusion of specific fields in the query results, without returning the entire document. Projection is specified in the `find()` method as a second parameter. For example, `db.student.find({}, { name: 1, course: 1, _id: 0 })` selects only the `name` and `course` fields, excluding the `_id`. Similarly, `db.student.find({}, { age: 0, grade: 0 })` returns all fields except `age` and `grade`. This is useful for obtaining only relevant data, minimizing data transfer and processing costs .
MongoDB geospatial queries use spatial data to find documents based on their location. Documents must include fields with geometric data types. For example, the query `db.places.find({ location: { $geoWithin: { $geometry: { type: 'Polygon', coordinates: [[[77.5930, 12.9700], [77.5960, 12.9700], [77.5960, 12.9730], [77.5930, 12.9730], [77.5930, 12.9700]]] } } } })` finds documents within a specified polygon area. Another example with `$near` finds locations close to a given point: `db.places.find({ location: { $near: { $geometry: { type: 'Point', coordinates: [77.5946, 12.9716] }, $maxDistance: 1000 } } })`, identifying documents within 1000 meters of a specified coordinate .
The `$set` operator in MongoDB is used to update fields of a document. When applied, it sets the value of a field to a specified value, or adds the field if it doesn't exist. For example, `db.student.updateOne( { name: 'Rahul Verma' }, { $set: { grade: 'A+' } } )` upgrades Rahul Verma's grade to 'A+'. To update a field across multiple documents, such as changing all CSE students' courses to AI&DS, use `updateMany()`: `db.student.updateMany( { course: 'CSE' }, { $set: { course: 'AI&DS' } } )`. This command applies the update to every document where the condition is met .
The `$geoWithin` operator in MongoDB identifies documents that exist within a specified geographical area. Used with `$geometry`, it checks if an object's location lies within a defined shape, such as a polygon. For example, `db.places.find({ location: { $geoWithin: { $geometry: { type: 'Polygon', coordinates: [[[77.5930, 12.9700], [77.5960, 12.9700], [77.5960, 12.9730], [77.5930, 12.9730], [77.5930, 12.9700]]] } } } })` fetches places within the specified polygon. This operation is applicable in mapping applications to find points of interest within a user-defined area, such as finding all stores within a city block .
Bitwise selectors in MongoDB allow filtering of documents based on the binary representation of field values. The `$bitsAllSet` operator checks if all specified bits are set in a field; for example, `db.devices.find({ status: { $bitsAllSet: 2 } })` matches documents where the binary representation of `status` has bit 1 set. The `$bitsAnySet` operator returns documents where any of the bits specified are set, as seen in `db.devices.find({ status: { $bitsAnySet: 6 } })`, which matches any document with either bit 1 or 2 set. Conversely, `$bitsAllClear` returns documents where none of the specified bits are set .
Logical query selectors such as `$and` and `$or` in MongoDB enable the formation of complex query conditions by combining multiple criteria. The `$and` operator is used to specify conditions that all must be met; for instance, to find CSE students with a grade greater than 80, use `db.students.find({ $and: [{ department: 'CSE' }, { grade: { $gt: 80 } }] })`. The `$or` operator allows any of the criteria to be satisfied, such as `db.students.find({ $or: [{ department: 'IT' }, { grade: { $lt: 70 } }] })`, which finds students either in the IT department or with a grade less than 70 .
In MongoDB, you can limit the number of documents returned by using the `.limit()` method chain after a `find()` query. To apply projection and limit the results simultaneously, include a projection specification within the `find()` method, and then apply `.limit(n)`. For example, to return only the first 5 documents showing the `name` and `course` fields, the query would be `db.student.find({}, { name: 1, course: 1, _id: 0 }).limit(5)` .