MongoDB basic combat: CRUD

1 Dependence

The database used by the background project is MongoDB.
During a test joint debugging process, the test colleague
asked me how to check the data and how to calculate the results of the verification data when testing the accuracy of the data.
I was not familiar with MongoDB data operations at the time. , After consulting other experienced colleagues,
I started to learn MongoDB, starting from the most basic: CRUD.
Share it as follows to help more developers in need to easily deal with daily development and knowledge exchange.

2 CRUD

insert image description here

2.1 New

2.1.1 Create a new database

Format:

use database-name;

Create a new database: tutorial

use tutorial;

Database does not exist: create database
Database exists: switch to this database

2.1.2 Create new data

Format:

db.collection_name.insertOne({数据})
db.collection_name.insertMany([{数据-1},{数据-n}]);
  • Create a single piece of data
    Timestamp: Doule timestamp = new Date().valueOf();
db.user.insertOne(
    {
        "age": 10,
        "createdTime": new Date().valueOf(),
        "money": 100,
        "sex": "male",
        "uid": "0x0001",
        "username": "xiaoyi"
    }
);
  • Create multiple data
db.user.insertMany(
    [
        {
            "age": 10,
            "createdTime": new Date().valueOf(),
            "money": Double(100),
            "sex": "male",
            "uid": "0x0007",
            "username": "xiaoqi"
        }, {
            "age": 10,
            "createdTime": new Date().valueOf(),
            "money": Double(100),
            "sex": "male",
            "uid": "0x0008",
            "username": "xiaoba"
        }]
);

2.2 Query data

2.2.1 Operators

serial number operator describe
1 lt <less than
2 gt > greater than
3 lte <= less than or equal to
4 gte >= greater than or equal to

Format:

db.collection_name.find({条件});

2.2.2 Query all data

db.user.find();

insert image description here

2.2.3 Query the data of specified conditions

db.user.find(
    {uid: '0x0002'})
db.user.find(
    {age: {$gt: 11}});

2.2.4 Aggregation query

db.user.aggregate([
    {
        $match: {age: {$gte: 11}}
    },
    {
        $group: {_id: "$sex", totalMoney: {$sum: "$money"}}
    }
    ]);

insert image description here

2.3 Update data

Format:

db.collection_name.updateOne({搜索条件}, {更新数据操作});
db.collection_name.updateMany({搜索条件}, {更新数据操作});

2.3.1 Update a single piece of data

db.user.updateOne({ _id: ObjectId("647c4ea010d72b63acb01301") }, {
    $set: {
        "age": 10,
        "createdTime": Long("1685868192340"),
        "money": Double(100),
        "sex": "male",
        "uid": "0x0001",
        "username": "xiaoyi"
    }
})

2.3.2 Update multiple pieces of data

  • For users whose age is greater than 10, money is set to 10
db.user.updateMany({ age: {$gt: 10}}, {
    $set: {
        "money": Double("10"),
    }
});
  • For users whose age is greater than 10, money increases by 10
db.user.updateMany({ age: {$gt: 10}}, {
    $inc: {
        "money": Double("10"),
    }
});

2.4 delete

2.4.1 Delete database

Format:

use database_name;
db.dropDatabase();

2.4.2 Delete data

Format:

# 删除所有数据
db.collection_name.deleteMany({});
# 删除所有数据
db.collection_name.drop();
# 删除特定数据
db.collection_name.deleteOne({field_name:  value});
# 删除多条数据
db.collection_name.deleteMany({field_name: value});

2.4.1 Delete a single piece of data

db.user.deleteOne({_id:ObjectId("647c4e5e10d72b63acb01300")});

2.4.2 Delete multiple pieces of data

db.user.deleteMany(
    {
        _id: {
            $in:
                [
                    ObjectId("647c685810d72b63acb01308"),
                    ObjectId("647c666410d72b63acb01307"),
                    ObjectId("647c685810d72b63acb01309")
                ]
        }
    }
);

Guess you like

Origin blog.csdn.net/Xin_101/article/details/131029851