CRUD Operations In MongoDB-Part 1
TOPICS:
- CRUD
- Create
- Read
- Comparison Operators
- Logical Operators
- Element Operators
- Array Operators
Definition:
CRUD operations in MongoDB refer to the four basic functions used to manage data in a database: Create, Read, Update, and Delete.
- C: Create – Insert new data
- R: Read – Retrieve data
- U: Update – Modify existing data
- D: Delete – Remove data
Create (Inserting Data)
The Create operation adds new documents to a collection.
- Methods:
- insertOne(): Inserts a single document.
- insertMany(): Inserts multiple documents at once.
SYNTAX:
// Insert a single document
db.collection.insertOne({
key1: value1,
key2: value2
});
// Insert multiple documents
db.collection.insertMany([
{ key1: value1 },
{ key2: value2 }
]);
db.users.insertOne({
name: "Alice",
age: 25,
email: "alice@example.com"
});
db.users.insertMany([
{ name: "Bob", age: 30, email: "bob@example.com" },
{ name: "Charlie", age: 35, email: "charlie@example.com" }
]);

Read (Querying Data)
The Read operation retrieves documents from a collection based on a query.
- Methods:
- findOne(): Returns the first document matching the query.
- find(): Returns a cursor to all matching documents.
SYNTAX:
// Find one document
db.collection.findOne({ query });
// Find multiple documents
db.collection.find({ query });

// Find a single user by name
db.users.findOne({ name: "Banu" });
// Find all users greater than grade 9
db.users.find({grade:{$gt:9}})
// Find all users greater than equal to grade 9
db.users.find({grade:{$gte:9}})
// Pretty-print results
db.users.find({ age: { $gt: 30 } }).pretty();
In MongoDB, query operators are used to perform specific operations in queries. Below is a comparison and explanation of the operators you listed, grouped by their categories: Comparison, Logical, Element, and Array operators.
1. Comparison Operators
These operators are used to compare values in documents against a specified value.
Operator | Description | Example |
---|---|---|
$eq | Matches values that are equal to the specified value. | { age: { $eq: 25 } } → Matches documents where age is exactly 25. |
$ne | Matches values that are not equal to the specified value. | { age: { $ne: 25 } } → Matches documents where age is not 25. |
$gt | Matches values that are greater than the specified value. | { age: { $gt: 25 } } → Matches documents where age is greater than 25. |
$gte | Matches values that are greater than or equal to the specified value. | { age: { $gte: 25 } } → Matches documents where age is 25 or greater. |
$lt | Matches values that are less than the specified value. | { age: { $lt: 25 } } → Matches documents where age is less than 25. |
$lte | Matches values that are less than or equal to the specified value. | { age: { $lte: 25 } } → Matches documents where age is 25 or less. |
$in | Matches documents where the value of a field is in a specified array of values. | { age: { $in: [25, 30, 35] } } → Matches documents where age is 25, 30, or 35. |
$nin | Matches documents where the value of a field is not in a specified array of values. | { age: { $nin: [25, 30, 35] } } → Matches documents where age is not 25, 30, or 35. |

Dot (.
) is not an operator here — it's part of the string that tells MongoDB to go one level deeper into the document.
$nin
means: “not in the given array”

2. Logical Operators
These operators combine multiple query conditions logically.
Operator | Description | Example |
---|---|---|
$and | Matches documents that satisfy all specified conditions. | { $and: [ { age: { $gt: 18 } }, { age: { $lt: 30 } } ] } → Matches documents where age is between 18 and 30 (exclusive). |
$or | Matches documents that satisfy at least one of the specified conditions. | { $or: [ { age: 25 }, { age: 30 } ] } → Matches documents where age is 25 or 30. |
$not | Matches documents that do not satisfy the specified condition. | { age: { $not: { $gt: 25 } } } → Matches documents where age is not greater than 25 (i.e., ≤ 25). |
$nor | Matches documents that do not satisfy any of the specified conditions. | { $nor: [ { age: 25 }, { age: 30 } ] } → Matches documents where age is neither 25 nor 30. |
Note: $and is often implicit in MongoDB queries (e.g., { age: 25, status: "active" } implies $and). Explicit $and is used when combining complex conditions.
db.users.find({$and:[{grade:{$gt:8}},{grade:{$lt:10}}]})
db.users.find({$nor:[{grade:{$gt:8}},{grade:{$lt:10}}]})
db.users.find({$or:[{grade:{$gt:8}},{grade:{$lt:10}}]})
db.users.find({grade:{$not:{$gte:9}}})

3. Element Operators
These operators check for the existence or type of a field in a document.
Operator | Description | Example |
---|---|---|
$exists | Matches documents where a field exists (or does not exist). | { name: { $exists: true } } → Matches documents that have a name field. { name: { $exists: false } } → Matches documents that do not have a name field. |
$type | Matches documents where a field is of a specified BSON type. | { age: { $type: "number" } } → Matches documents where age is a number (e.g., integer or double). { age: { $type: ["string", "null"] } } → Matches documents where age is a string or null. |
Common BSON Types: "double", "string", "object", "array", "binData", "objectId", "boolean", "date", "null", "regex", "int", "timestamp", "long", etc.
Example:
db.users.find({name:{$exists:true}})
db.users.find({name:{$exists:false}})
db.users.find({grade:{$type:["number","null"]}})
db.users.find({grade:{$type:"number"}})

4. Array Operators
These operators are used to query fields that contain arrays.
Operator | Description | Example |
---|---|---|
$all | Matches documents where an array field contains all specified elements (in any order). | { tags: { $all: ["red", "blue"] } } → Matches documents where tags includes both "red" and "blue". |
$elemMatch | Matches documents where at least one element in an array satisfies all conditions specified in the $elemMatch query. | { scores: { $elemMatch: { $gt: 80, $lt: 90 } } } → Matches documents where the scores array has at least one element between 80 and 90. |
$size | Matches documents where an array field has a specific number of elements. | { tags: { $size: 3 } } → Matches documents where the tags array has exactly 3 elements. |
Notes:
- $all is useful for ensuring multiple values are present in an array, regardless of order.
- $elemMatch is necessary when applying multiple conditions to a single array element (e.g., a single object in an array of objects).
- $size cannot be combined with other operators like $gt directly; use aggregation for more complex array size queries.
db.users.find({language:{$size:3}})
db.users.find({language:{$all:["Telugu","Hindi"]}})

Comments
Post a Comment