CRUD Operations In MongoDB-Part 1

 

TOPICS:

  1. CRUD
  2. Create
  3. 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:

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:


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.

OperatorDescriptionExample
$eqMatches values that are equal to the specified value.{ age: { $eq: 25 } } → Matches documents where age is exactly 25.
$neMatches values that are not equal to the specified value.{ age: { $ne: 25 } } → Matches documents where age is not 25.
$gtMatches values that are greater than the specified value.{ age: { $gt: 25 } } → Matches documents where age is greater than 25.
$gteMatches values that are greater than or equal to the specified value.{ age: { $gte: 25 } } → Matches documents where age is 25 or greater.
$ltMatches values that are less than the specified value.{ age: { $lt: 25 } } → Matches documents where age is less than 25.
$lteMatches values that are less than or equal to the specified value.{ age: { $lte: 25 } } → Matches documents where age is 25 or less.
$inMatches 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.
$ninMatches 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.

OperatorDescriptionExample
$andMatches documents that satisfy all specified conditions.{ $and: [ { age: { $gt: 18 } }, { age: { $lt: 30 } } ] } → Matches documents where age is between 18 and 30 (exclusive).
$orMatches documents that satisfy at least one of the specified conditions.{ $or: [ { age: 25 }, { age: 30 } ] } → Matches documents where age is 25 or 30.
$notMatches documents that do not satisfy the specified condition.{ age: { $not: { $gt: 25 } } } → Matches documents where age is not greater than 25 (i.e., ≤ 25).
$norMatches 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.

3. Element Operators

These operators check for the existence or type of a field in a document.

OperatorDescriptionExample
$existsMatches 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.
$typeMatches 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:

4. Array Operators

These operators are used to query fields that contain arrays.

OperatorDescriptionExample
$allMatches 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".
$elemMatchMatches 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.
$sizeMatches 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.

Comments

Popular posts from this blog

"Don't Believe Everything You Think"-Joseph Nguyen

HOW JAVA WORKS

How Java Works (Interview Question Answers)