Title: "MongoDB. A Guide to Database Operations": Part 02

Title: "MongoDB. A Guide to Database Operations": Part 02

Read Operations

The db.collectionName.find({ key: value }) syntax in MongoDB is used to retrieve documents from a collection that match a specified condition.

db.collectionName: Specifies the collection where the search will be performed. Replace "collectionName" with the actual name of your collection.

.find({ key: value }): The find() method is used to query documents in the collection. Inside the find() method, you provide a condition expressed as a key-value pair. This condition is used to filter documents based on the specified field and its corresponding value.

db.student.find({'grade':'A'})
[
  {
    _id: ObjectId('658ad314011e6a5d02559e27'),
    name: 'John Doe',
    age: '20',
    gender: 'Male',
    grade: 'A', // it filtered due to to this field
    subjects: [ 'Math', 'English', 'Science' ],
    address: { street: '123 Main St', city: 'Anytown', country: 'USA' }
  }
]

 db.student.findOne({'gender':'male'})

it will only give the first document that contain field gender:male and skip all remain.

Arithmetics operators in shell

$eq => Equal to

The $eq is a query operator used in the context of querying documents based on equality. It stands for "equals" and is used to match documents where a specified field is equal to a certain value.

Here's a basic example of how you might use $eq in a MongoDB query:

db.collection.find({ field: { $eq: "value" } })

You can also use $eq for numeric comparisons. For instance, if you want to find students with a specific age:

db.students.find({ age: { $eq: 20 } })

This query would retrieve documents from the "students" collection where the "age" field is equal to 20.

[
  {
    _id: ObjectId('658ae5266058972bff93a800'),
    name: 'John Doe',
    age: 20,
    gender: 'Male',
    grade: 'A',
    subjects: [ 'Math', 'English', 'Science' ],
    address: { street: '123 Main St', city: 'Anytown', country: 'USA' }
  },
  {
    _id: ObjectId('658ae5266058972bff93a802'),
    name: 'Grace Miller',
    age: 20,
    gender: 'Female',
    grade: 'A',
    subjects: [ 'Chemistry', 'Art History', 'Math' ],
    address: { street: '555 Cedar Ave', city: 'Artsville', country: 'USA' }
  }
]

$ne => Not equal to

The $ne (not equal) operator is used to find documents where the specified field is not equal to a particular value. Here's how you can use $ne in the context of the "students" collection:

db.students.find({ grade: { $ne: "A" } })

In this example, the query will return documents from the "students" collection where the "grade" field is not equal to "A". You can replace "grade" and "A" with your specific field and value.

$lt => Less than and $gt Greater than

the $lt operator is used to find documents where the value of a specified field is less than (<) a given value. Here's an example using the "students" collection:

db.students.find({ age: { $lt: 25 } })

$gt (greater than) operator is used to find documents where the value of a specified field is greater than a given value. Here's an example using the "students" collection:

db.students.find({ age: { $gt: 20 } })

$gte and $lte

$gte (greater than or equal to) and $lte (less than or equal to) operators are used for range queries. Here are examples using the "students" collection:

Greater Than or Equal To ($gte):

db.students.find({ age: { $gte: 20 } })

Less Than or Equal To ($lte):

db.students.find({ score: { $lte: 60 } })

This query will retrieve documents from the "students" collection where the "score" field is less than or equal to 60.

$in and $nin

the $in and $nin operators are used for matching values within and outside a specified set, respectively.

$in Operator:

The $in operator selects documents where the value of a field equals any value in the specified array. Here's an example:

db.students.find({ grade: { $in: ["A", "B", "C"] } })

This query will return documents from the "students" collection where the "grade" field is equal to "A", "B", or "C".

$nin Operator: The $nin operator selects documents where the value of a field does not equal any value in the specified array. Here's an example:

db.students.find({ grade: { $nin: ["D", "F"] } })

This query will return documents from the "students" collection where the "grade" field is not equal to "D" or "F".

Cursor Methods

cursor methods like count(), limit(), skip(), and sort() are commonly used to control and customize the retrieval of documents from a collection.

count() Method:

The count() method is used to count the number of documents that match a query. Here's an example:

db.students.find({ grade: "A" }).count()

This query will return the count of documents in the "students" collection where the "grade" field is equal to "A".

limit() Method:

The limit() method is used to specify the maximum number of documents to be returned. For example:

db.students.find().limit(5)

This query will return at most 5 documents from the "students" collection.

skip() Method:

The skip() method is used to skip a specified number of documents in the result set. For example:

db.students.find().skip(5)

This query will skip the first 5 documents and return the rest.

sort() Method:

The sort() method is used to sort the result set based on one or more fields. For example:

db.students.find().sort({ age: 1 })

This query will return documents from the "students" collection sorted in ascending order based on the "age" field. The value 1 represents ascending order, and -1 represents descending order.

These cursor methods provide flexibility in querying and retrieving data from MongoDB collections.

Element Operators

$exists Operator:

The $exists operator checks for the existence of a field in a document. It can be used to find documents where a particular field exists or does not exist. Here's an example:

# Find documents where the "address" field exists
db.students.find({ address: { $exists: true } })

This query will return documents from the "students" collection where the "address" field exists.

$type Operator:

The $type operator is used to query documents based on the BSON type of a field. For example:

# Find documents where the "age" field is of type double (BSON type 1)
db.students.find({ age: { $type: 1 } })

This query will return documents where the "age" field is of type double.

$size Operator:

The $size operator is used to query arrays based on their size. For example:

# Find documents where the "grades" array has a size of 3
db.students.find({ grades: { $size: 3 } })

This query will return documents where the "grades" array has a size of 3.

Remember to replace "students," "address," "age," "grades," and other field names with the actual field names from your collection.

In this comprehensive guide, we delved into the various find operations in MongoDB, providing a thorough understanding of how to query and retrieve documents from collections.

Your quest for mastering MongoDB continues. Get ready for an even deeper dive into the world of MongoDB find operations in our upcoming blogs. Happy querying, and see you in Part 3!

Bye!