2022-05-19 MongoDB部分操作记录

// 更新文档

db.getCollection("less").update({"start":"update"},{$set:{"start":"new"}})

// 查找文档

db.getCollection("userProfile").find({"status":1}).limit(1).pretty()
db.getCollection("less").find().limit(10).pretty()

// 创建删除集合
// 执行insert的时候,如果集合不存在,则会自动创建这个集合
// 删除集合 remove

db.newTable.insert({"start":"start"})
db.getCollection("less").insert({"start":"start2"})
db.getCollection("less").remove({"start":"start1"})

// 排序 1 -1(倒序)

db.getCollection("less").find().limit(10).sort({
    "_id":  - 1
}).pretty()

// or 与 and 的查询条件 并过滤展示字段

db.getCollection("userProfile").find(
    {
        "countryCode": "CN",
        $or:[
            {
                "nickName": "Hana"
            },
            {
                "nickName": "JackIos"
            }
        ]
    },
        {
            _id:0
        }
).limit(10)

// 结果处理
// 连表查询 过滤 分组

db.getCollection("userProfile").aggregate(
    [
        {
            $lookup: {  // 连表
                from: "userVedio",
                localField: "_id",
                foreignField: "userId",
                as: "userVideo"
            }
        },
                {
                        $match:{    // 过滤条件
                            "_id":3103255
                        }
                },
                {
                        $unwind:{   // 拆分数组
                            path:"$userVideo"
                        }
                },
                {
                        $group:{    // 分组
                            _id:"$_id",
                            callVideo:{
                                $min:"$userVideo.url"
                            }
                        }
                },
        {
            $limit: 10
        }
    ]
)

// 公式/数据运算

// (>) 大于 - $gt
// (<) 小于 - $lt
// (>=) 大于等于 - $gte
// (<= ) 小于等于 - $lte

// 聚合 等价于 mysql 的 group by city

db.getCollection("userProfile").aggregate([
    {
        $match: {
            "status": 1,
            "_id": 100000
        }
    },
    {
        $group: {
            _id: "$_id",
            age: {
                $min: "$age"
            },
            countryCode: {
                $min: "$countryCode"
            },
            status: {
                $min: "$status"
            }
        }
    },
    {
        $project: {
            _id: 1,
            age: 1,
            countryCode: 1,
            status: 1
        }
    },
    {
        $sort: {
            "_id": - 1
        }
    },
    {
        $limit: 10
    }
])

// 数据库监控 状态查询

db.serverStatus()

// 锁信息监控

db.serverStatus().globalLock
{
    "totalTime" : NumberLong("2651301900000"),      //自上次发生lock以来的时间
    "currentQueue" : {          //锁等待队列信息
        "total" : 0,            //因为锁而产生的排队的总数
        "readers" : 0,          //等待读锁而产生的排队数(kQueuedReader)
        "writers" : 0           //等待写锁而产生的排队数(kQueuedWriter)
    },
    "activeClients" : {         //活跃连接数信息
        "total" : 38,           //当前活跃连接数
        "readers" : 0,          //当前执行读操作的活跃连接数(kActiveReader)
        "writers" : 0           //当前执行写操作的活跃连接数(kActiveWriter)
    }
}

// 连接信息监控

db.serverStatus().connections
{
    "current": 5,                       //当前连接数
    "available": 814,                   //剩余可以连接数
    "totalCreated": NumberLong(186)     //截止到现在创建连接数
}

// 内存信息监控

db.serverStatus().mem
{
    "bits" : 64,                    //64位
    "resident" : 245,               //物理内存消耗
    "virtual" : 1262,               //虚拟内存消耗
    "supported" : true,             //支持显示额外内存信息
    "mapped" : 0,                   //映射内存
    "mappedWithJournal" : 0         //除了映射内存外还包括journal日志消耗的映射内存
}

// 错误信息监控

db.serverStatus().asserts
{
    "regular": 0,           //服务启动后asserts错误个数
    "warning": 0,           //服务启动后warning个数
    "msg": 0,               //服务启动后message asserts个数
    "user": 22,             //服务启动后user asserts格式
    "rollovers": 0          //服务启动后重置次数
}

// 网络流量监控

db.serverStatus().network
{
    "bytesIn" : NumberLong(1013083142),     //网络入流量
    "bytesOut" : NumberLong(1123552013),    //网络处流量
    "numRequests" : NumberLong(3592562)     //累积请求数
}

// 数据库状态

db.stats()
{
    "db" : "test",                          //数据库名
    "collections" : 5,                      //数据库中集合数
    "objects" : 139,                        //数据库预估数据行
    "avgObjSize" : 63.65467625899281,       //平均每行数据大小,单位为bytes
    "dataSize" : 8848,                      //当前数据库数据大小,单位为bytes
    "storageSize" : 1077248,                //当前数据库物理存储大小,单位为bytes
    "numExtents" : 5,                      
    "indexes" : 2,                          
    "indexSize" : 16352,                    //索引空间大小,单位为bytes
    "fileSize" : 67108864,                  //数据库预分配文件大小
    "nsSizeMB" : 16,
    "extentFreeList" : {
        "num" : 1,
        "totalSize" : 32768
    },
    "dataFileVersion" : {
        "major" : 4,
        "minor" : 22
    },
    "ok" : 1
}

// 查看当前活跃会话

db.currentOp()
{
    "inprog" : [
        {
            "desc" : "conn10",
            "threadId" : "0x3fdf860",
            "connectionId" : 10,
            "opid" : 380692,                        //db.killOp使用的就是该opid
            "active" : true,                        //是否活跃
            "secs_running" : 4,                     //执行时间(秒)
            "microsecs_running" : NumberLong(4603324),
            "op" : "insert",                        //执行操作类型
            "ns" : "test.cc",                       //执行操作数据库
            "insert" : {                            //执行操作语句
                "_id" : ObjectId("5bee323020e268b4d947a580"),
                "name" : "aa"
            },
            "client" : "127.0.0.1:42066",           //执行操作客户端
            "numYields" : 0,
            "locks" : {                             //执行操作需要持有锁
                "Global" : "w"
            },
            "waitingForLock" : true,                //是否锁等待 ?
            "lockStats" : {
                "Global" : {
                    "acquireCount" : {
                        "r" : NumberLong(1),
                        "w" : NumberLong(1)
                    },
                    "acquireWaitCount" : {
                        "w" : NumberLong(1)
                    },
                    "timeAcquiringMicros" : {
                        "w" : NumberLong(22503637)
                    }
                }
            }
        }
    ],
    "fsyncLock" : true,                             //是否全局锁定数据库
    "info" : "use db.fsyncUnlock() to terminate the fsync write/snapshot lock"
}

// 杀掉慢会话

db.killOp(380692)

你可能感兴趣的:(2022-05-19 MongoDB部分操作记录)