乐趣区

MongoDB-v40-命令

MongoDB v4.0 命令

官方文档 > 点这里 <

操作系统库

# 操作管理员库
use admin
#鉴权
db.auth("root","admin");
#用户查看 (格式美化)
db.system.users.find().pretty();

#新增用户
db.createUser({
    user: 'root1',
    pwd: 'admin1',
    roles: [{ role: "dbOwner", db: "yapi"}]
});
  
#更新用户信息 / 密码 / 权限
db.updateUser("root1",{
    pwd: "admin2",
    roles:
    [{
        role: "root",
        db: "admin"
      },{
        role: "userAdminAnyDatabase",
        db: "admin"
      },{
        role: "readWriteAnyDatabase",
        db: "admin"
      },{
        role: "dbAdminAnyDatabase",
        db: "admin"
}]})

#删除数据库所属用户
db.dropUser("root1");

操作自定义库

# 操作自定义库
use persionalDB

#鉴权
db.auth("user","pwd");

#创建数据库所属用户
db.createUser({
    user: 'username1',
    pwd: 'password1',
    roles: [{ role: "dbOwner", db: "yapi"}]
});
  
#更新用户信息 / 密码 / 权限
db.updateUser("username1",{
    pwd: "NEW pass",
    roles:
    [{
        role: "root",
        db: "admin"
      },{
        role: "userAdminAnyDatabase",
        db: "admin"
      },{
        role: "readWriteAnyDatabase",
        db: "admin"
      },{
        role: "dbAdminAnyDatabase",
        db: "admin"
}]});

#删除数据库所属用户
db.dropUser("username1");

1.0.0 基础操作 [collection]= 所查集合名

use [db_name];

db.[collection].insert({[title]:'[value]'});
eg: 
    db.hellocollction.insert({name:'hello'});

# 单个插入
db.inventory.insertOne(
   { item: "canvas", 
     qty: 100, 
     tags: ["cotton"], 
     size: { 
            h: 28, 
            w: 35.5, 
            uom: "cm" 
        } 
    }
)
# 批量插入
db.inventory.insertMany([{ item: "journal", qty: 25, tags: ["blank", "red"], size: {h: 14, w: 21, uom: "cm"} },
   {item: "mat", qty: 85, tags: ["gray"], size: {h: 27.9, w: 35.5, uom: "cm"} },
   {item: "mousepad", qty: 25, tags: ["gel", "blue"], size: {h: 19, w: 22.85, uom: "cm"} }
])

1.1 ============> 增 <============

  • db.[collection].insertOne()/insertMany()

1.2 ============> 查 <============

  • db.[[collection]].find()

1.3 ============> 改 <============

  • db.[collection].updateOne()/updateMany()/replaceOne()

1.4 ============> 删 <============

  • db.collection.deleteOne()/deleteMany()

2.0 高级: 联表查询 Join

初始数据

db.product.insert({"_id":1,"productname":"商品 1","price":15});
db.product.insert({"_id":2,"productname":"商品 2","price":36});
 
db.order.insert({"_id":1,"pid":1,"ordername":"订单 1","uid":1});
db.order.insert({"_id":2,"pid":2,"ordername":"订单 2","uid":2});
db.order.insert({"_id":3,"pid":2,"ordername":"订单 3","uid":2});
db.order.insert({"_id":4,"pid":1,"ordername":"订单 4","uid":1});
 
db.user.insert({"_id":1,"username":1});
db.user.insert({"_id":2,"username":2});
 
db.product.find();
db.order.find();
db.user.find();

2.1 $lookup 两表关联 join

db.product.aggregate([
    {
      $lookup:
        {
          from: "order",
          localField: "_id",
          foreignField: "pid",
          as: "inventory_docs"
        }
   }
]);
#结果:
{
    "_id" : 1.0,
    "productname" : "商品 1",
    "price" : 15.0,
    "inventory_docs" : [
        {
            "_id" : 1.0,
            "pid" : 1.0,
            "ordername" : "订单 1"
        },
        {
            "_id" : 4.0,
            "pid" : 1.0,
            "ordername" : "订单 4"
        }
    ]
}

lookup 就是使用 aggregate 的 $lookup 属性,$lookup 操作需要一个四个参数的对象:

  • localField:在输入文档中的查找字段
  • from:需要连接的集合
  • foreignField:需要在 from 集合中查找的字段
  • as:输出的字段名字

2.2 $lookup 三表关联 join

db.order.aggregate([
    {
        "lookup": {
            "from": "product",
            "localField": "pid",
            "foreignField": "_id",
            "as": "inventory_docs"
        }
    },{
        "lookup": {
            "from": "user",
            "localField": "uid",
            "foreignField": "_id",
            "as": "user_docs"
        }
    }
]);

除了 $lookup,populate 也可以用来进行关联查询

退出移动版