Mongo DB

 The query language for MongoDB is based on JavaScript and uses JSON-like documents. MongoDB's query language allows you to perform a wide range of operations, including querying, updating, and aggregating data.

Here are some key aspects of MongoDB's query language:

Basic Queries

Finding Documents

  • Find all documents in a collection:

    javascript
    db.collection.find()
  • Find documents that match a specific condition:

    javascript
  • db.collection.find({ key: value })

Examples

  • Find all documents where name is "John":

    javascript
  • db.users.find({ name: "John" })
  • Find all documents where age is greater than 25:

    javascript
    db.users.find({ age: { $gt: 25 } })

Query Operators

Comparison Operators

  • $eq: Equal to
  • $ne: Not equal to
  • $gt: Greater than
  • $gte: Greater than or equal to
  • $lt: Less than
  • $lte: Less than or equal to

Examples

  • Find documents where age is less than or equal to 30:
    javascript
    db.users.find({ age: { $lte: 30 } })

Logical Operators

  • $and: Logical AND
  • $or: Logical OR
  • $not: Logical NOT
  • $nor: Logical NOR

Examples

  • Find documents where age is greater than 25 and name is "John":
    javascript
    db.users.find({ $and: [ { age: { $gt: 25 } }, { name: "John" } ] })

Projection

  • Specify which fields to include or exclude in the result set:
    javascript
    db.users.find({}, { name: 1, age: 1 })
    This example includes only the name and age fields in the result.

Update Operations

Update a Single Document

  • Update the first document that matches the condition:
    javascript
    db.collection.updateOne({ key: value }, { $set: { key: newValue } })

Update Multiple Documents

  • Update all documents that match the condition:
    javascript
    db.collection.updateMany({ key: value }, { $set: { key: newValue } })

Delete Operations

Delete a Single Document

  • Delete the first document that matches the condition:
    javascript
    db.collection.deleteOne({ key: value })

Delete Multiple Documents

  • Delete all documents that match the condition:
    javascript
    db.collection.deleteMany({ key: value })

Aggregation

  • Perform complex data aggregation:
    javascript
    db.collection.aggregate([ { $match: { key: value } }, { $group: { _id: "$groupKey", total: { $sum: "$value" } } }, { $sort: { total: -1 } } ])

Example Aggregation Pipeline

  • Find the total sales per product and sort them in descending order:
    javascript
    db.sales.aggregate([ { $group: { _id: "$product", totalSales: { $sum: "$amount" } } }, { $sort: { totalSales: -1 } } ])

Indexing

  • Create an index on a field:
    javascript
    db.collection.createIndex({ key: 1 }) // 1 for ascending, -1 for descending

MongoDB's query language is powerful and flexible, allowing developers to perform a wide range of operations on their data. For more complex queries and operations, MongoDB provides detailed documentation and various tools to help developers effectively manage their database

Comments

Popular posts from this blog

SAP Archiving - Ariba documents

Community is growing ? Ideas for building a classified ad community

Detailed List of CO Tables in SAP