MongoDB v4.0 命令
官方文檔 > 點(diǎn)這里 <
操作系統(tǒng)庫(kù)
#操作管理員庫(kù)
use admin
#鑒權(quán)
db.auth("root","admin");
#用戶查看(格式美化)
db.system.users.find().pretty();
#新增用戶
db.createUser({
user: 'root1',
pwd: 'admin1',
roles: [ { role: "dbOwner", db: "yapi" }]
});
#更新用戶信息/密碼/權(quán)限
db.updateUser("root1",{
pwd: "admin2",
roles:
[{
role: "root",
db: "admin"
},{
role: "userAdminAnyDatabase",
db: "admin"
},{
role: "readWriteAnyDatabase",
db: "admin"
},{
role: "dbAdminAnyDatabase",
db: "admin"
}]})
#刪除數(shù)據(jù)庫(kù)所屬用戶
db.dropUser("root1");
操作自定義庫(kù)
#操作自定義庫(kù)
use persionalDB
#鑒權(quán)
db.auth("user","pwd");
#創(chuàng)建數(shù)據(jù)庫(kù)所屬用戶
db.createUser({
user: 'username1',
pwd: 'password1',
roles: [ { role: "dbOwner", db: "yapi" }]
});
#更新用戶信息/密碼/權(quán)限
db.updateUser("username1",{
pwd: "NEW pass",
roles:
[{
role: "root",
db: "admin"
},{
role: "userAdminAnyDatabase",
db: "admin"
},{
role: "readWriteAnyDatabase",
db: "admin"
},{
role: "dbAdminAnyDatabase",
db: "admin"
}]});
#刪除數(shù)據(jù)庫(kù)所屬用戶
db.dropUser("username1");
1.0.0 基礎(chǔ)操作 [collection]= 所查集合名
use [db_name];
db.[collection].insert({[title]:'[value]'});
eg:
db.hellocollction.insert({name:'hello'});
# 單個(gè)插入
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()
title
1.2 ============>查<============
- db.[[collection]].find()
title
1.3 ============>改<============
- db.[collection].updateOne()/updateMany()/replaceOne()
title
1.4 ============>刪<============
- db.collection.deleteOne()/deleteMany()
title
2.0 高級(jí): 聯(lián)表查詢 Join
初始數(shù)據(jù)
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
兩表關(guān)聯(lián)join
db.product.aggregate([
{
$lookup:
{
from: "order",
localField: "_id",
foreignField: "pid",
as: "inventory_docs"
}
}
]);
#結(jié)果:
{
"_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 操作需要一個(gè)四個(gè)參數(shù)的對(duì)象:
- localField:在輸入文檔中的查找字段
- from:需要連接的集合
- foreignField:需要在from集合中查找的字段
- as:輸出的字段名字
2.2 $lookup 三表關(guān)聯(lián)join
db.order.aggregate([
{
"lookup": {
"from": "product",
"localField": "pid",
"foreignField": "_id",
"as": "inventory_docs"
}
},{
"lookup": {
"from": "user",
"localField": "uid",
"foreignField": "_id",
"as": "user_docs"
}
}
]);