LTCMiner cloud mining
Posts

Program 7

PROGRAM -7

7. Install an Open Source NoSQL Data base MongoDB & perform basic CRUD(Create, Read, Update & Delete) operations. Execute MongoDB basic Queries using CRUD operations.


Performing Basic CRUD Operations:


1. Create Operation:

mysql> use basictask;

2. Insert Operation:

● Insert single documents:

mysql> db.users.insertOne({
    name: "Braham Kumar",
    age: 25,
    email: "braham@gmail.com",
    status: "inactive"
});

● Insert multiple documents:

mysql> db.users.insertMany([
    {
        name: "Braham Kumar",
        age: 25,
        email: "braham@gmail.com",
        status: "inactive"
    },
    {
        name: "Shubham Kumar",
        age: 35,
        email: "shubham@gmail.com",
        status: "active"
    },
    {
        name: "Bikash Singh",
        age: 28,
        email: "bikash@gmail.com",
        status: "active"
    },
    {
        name: "Shoaib Akhtar",
        age: 28,
        email: "shoaib@gmail.com",
        status: "active"
    }
]);

3. Read Query:

● Find all documents:

mysql> db.users.find();

OUTPUT:

[
    {
        _id: ObjectId('666c78f13c52fc36f3cdcdf6'),
        name: 'Braham Kumar',
        age: 25,
        email: 'braham@gmail.com',
        status: 'inactive'
    },
    {
        _id: ObjectId('666c78f13c52fc36f3cdcdf7'),
        name: 'Shubham Kumar',
        age: 35,
        email: 'shubham@gmail.com',
        status: 'active'
    },
    {
        _id: ObjectId('666c78f13c52fc36f3cdcdf8'),
        name: 'Bikash Singh',
        age: 28,
        email: 'bikash@gmail.com',
        status: 'active'
    }
]

● Find documents with specific criteria (e.g., find all users with age greater than 25):

mysql> db.users.find({ age: { $gt: 25 } });

OUTPUT:

[
    {
        _id: ObjectId('666c78f13c52fc36f3cdcdf6'),
        name: 'Braham Kumar',
        age: 25,
        email: 'braham@gmail.com',
        status: 'inactive'
    },
    {
        _id: ObjectId('666c78f13c52fc36f3cdcdf7'),
        name: 'Shubham Kumar',
        age: 35,
        email: 'shubham@gmail.com',
        status: 'active'
    },
    {
        _id: ObjectId('666c78f13c52fc36f3cdcdf8'),
        name: 'Bikash Singh',
        age: 28,
        email: 'bikash@gmail.com',
        status: 'active'
    }
]

4. Update Query:

● Update a single document:

mysql> db.users.updateOne(
    { name: "Bikash Singh" },
    { $set: { age: 31 } }
);
mysql> db.users.find({ age: { $gt: 31 } });

OUTPUT:

{
    _id: ObjectId('666c78f13c52fc36f3cdcdf7'),
    name: 'Shubham Kumar',
    age: 35,
    email: 'shubham@gmail.com',
    status: 'active'
}

● Update multiple documents:

mysql> db.users.updateMany(
    { age: { $gt: 28 } },
    { $set: { status: "inactive" } }
);

OUTPUT:

[
    {
        _id: ObjectId('666c78f13c52fc36f3cdcdf7'),
        name: 'Shubham Kumar',
        age: 35,
        email: 'shubham@gmail.com',
        status: 'active'
    },
    {
        _id: ObjectId('666c78f13c52fc36f3cdcdf8'),
        name: 'Bikash Singh',
        age: 20,
        email: 'bikash@gmail.com',
        status: 'active'
    },
    {
        _id: ObjectId('666c78f13c52fc36f3cdcdf9'),
        name: 'Shoaib Akhtar',
        age: 28,
        email: 'shoaib@gmail.com',
        status: 'active'
    }
]

5. Delete Query:

● Delete a single document:

mysql> db.users.deleteOne({ name: "Bikash Singh" });

● Delete multiple documents:

mysql> db.myCollection.deleteMany({ age: { $gt: 20 } });

6. Basic Queries:

● Count documents in a collection:

mysql> db.users.count();

● Sorting documents:

mysql> db.users.find().sort({ age: 1 }); // Ascending order
mysql> db.users.find().sort({ age: -1 }); // Descending order

● Limiting the number of documents returned:

mysql> db.users.find().limit(5);

● Aggregation queries (e.g., group by and aggregate functions):

mysql> db.users.aggregate([
    { $group: { _id: "$age", count: { $sum: 1 } } }
]);


Getting Info...
Oops!
It seems there is something wrong with your internet connection. Please connect to the internet and start browsing again.
AdBlock Detected!
We have detected that you are using adblocking plugin in your browser.
The revenue we earn by the advertisements is used to manage this website, we request you to whitelist our website in your adblocking plugin.