Skip to content

MongoDB

公司有慢SQL,筛选出来后发现是MongoDB的。因为之前从来没有接触过MongoDB,所以正好可以趁着这次机会来学习下MongoDB

安装MongoDB

我这边使用docker来安装 官网教程

Terminal window
export MONGODB_VERSION=6.0-ubi8
docker run --name mongodb -d -p 27017:27017 -e MONGO_INITDB_ROOT_USERNAME=root -e MONGO_INITDB_ROOT_PASSWORD=123456 mongodb/mongodb-community-server:$MONGODB_VERSION

使用MongoDB

教程

https://blog.51cto.com/u_16175431/8078672

https://www.cnblogs.com/eternityz/p/13595660.html

use moatkon

db.testcollection.find() db.testcollection.findOne() db.testcollection.count()

db.testcollection.insertOne({ name: “google”, birth: 1991 })

db.testcollection.find()

db.testcollection.deleteOne({name:“google”})

db.testcollection.aggregate([ {$group:{_id:“$name”,count: {$sum: 1}} } ])

db.testcollection.find({ name: “google” })

db.testcollection.update( { name: “google” } , { $set: { “birth”: 2023 } } )

db.testcollection.createIndex({birth:1} )

— 使用了索引 db.testcollection.find({ birth: 2023 }).explain()

db.testcollection.find({ name: “google” }).explain()

db.testcollection.getIndexes()