1,下载MongoDB
https://www.mongodb.com/try/download/community
2,启动服务
cd %mongodb_home%\bin
# --dbpath 指定存放数据位置,如果路径含有空格,需要使用""
# --auth 使用基本访问控制
mongod.exe --dbpath D:\UserData\mongodb --auth
# 使用命令查看是否启动实例
tasklist /FI "IMAGENAME eq mongod.exe"
# 输出
映像名称 PID 会话名 会话# 内存使用
========================= ======== ================ =========== ============
mongod.exe 5264 Console 4 179,792 K
3,使用命令行客户端连接
cd %mongodb_home%\bin
# 不带认证
mongo.exe [--host <HOSTNAME> --port <PORT>]
# 带认证
mongo.exe mongodb://admin:123456@127.0.0.1:27017/admin
mongo --host 127.0.0.1 --port 27017 -u "admin" -p "123456" --authenticationDatabase "admin"
# 例如
mongo.exe --host 127.0.0.1 --port 27017
# 查询所有的数据库
show databases;
# 退出客户端
exit
4,设置管理员
1,切换admin数据库
use admin;
2,创建管理员
db.createUser(
{
user: "admin",
pwd: "123456",
roles: [ "root" ] # 管理员角色,拥有所有的权限
}
);
# 输出
Successfully added user: { "user" : "admin", "roles" : [ "root" ] }
3,验证用户是否添加成功
show users;
5,关闭mongodb实例
db.shutdownServer();
# 输出
server should be down...
6,数据库增删改查
插入数据
概念: 数据库 -> 集合 -> 文档
1,切换test数据库
use test;
2,想book集合中插入文档
db.book.insertOne(
{ "item" : "canvas",
"qty" : 100,
"tags" : ["cotton"],
"size" : { "h" : 28, "w" : 35.5, "uom" : "cm" }
}
);
# 若果没有指定_id字段,会自动添加的
# 输出
{
"acknowledged" : true,
"insertedId" : ObjectId("60ee971f14e95ff0269158c7")
}
读数据
1,切换test数据库
use test;
2,获取book集合中所有的文档
books = db.book.find( {} );
# 输出
{ "_id" : ObjectId("60ee971f14e95ff0269158c7"), "item" : "canvas", "qty" : 100, "tags" : [ "cotton" ], "size" : { "h" : 28, "w" : 35.5, "uom" : "cm" } }
3,插入多条测试数据
db.book.insertMany( [
{ "item": "journal", "qty": 25, "size": { "h": 14, "w": 21, "uom": "cm" }, "status": "A" },
{ "item": "notebook", "qty": 50, "size": { "h": 8.5, "w": 11, "uom": "in" }, "status": "A" },
{ "item": "paper", "qty": 100, "size": { "h": 8.5, "w": 11, "uom": "in" }, "status": "D" },
{ "item": "planner", "qty": 75, "size": { "h": 22.85, "w": 30, "uom": "cm" }, "status": "D" },
{ "item": "postcard", "qty": 45, "size": { "h": 10, "w": 15.25, "uom": "cm" }, "status": "A" }
]);
# 查询 status = D
books = db.book.find( { status: "D" } ); # 默认最多显示前20条
# 嵌入文档查询
books = db.book.find( { size: { h: 14, w: 21, uom: "cm" } } );
# 逗号.字段
books = db.book.find( { "size.uom": "in" } );
# size.h < 15
books = db.book.find( { "size.h": {$lt: 15} } );
# 组合查询
# status = "A" and qty < 30,同时满足多个条件
books = db.book.find( { status: "A" ,qty: {$lt: 30} } );
# 逻辑or查询
books = db.book.find( { $or: [ { status: "A" }, { qty: { $lt: 30 } } ] } );
# status = "A" and qty < 30 or item.startWith(p)
books = db.book.find( {
status: "A",
$or: [ { qty: { $lt: 30 } }, { item: /^p/ } ]
} );
更新文档
1,更新item的值为paper的第一个文档
db.book.updateOne(
{ "item" : "paper" }, // specifies the document to update
{
$set: { "size.uom" : "cm", "status" : "P" },
$currentDate: { "lastModified": true }
}
);
# 查询更新的数据
books=db.book.find( { "item" : "paper" });
2,更新多个文档,匹配 qty<50 的所有的文档
db.book.updateMany(
{ "qty" : { $lt: 50 } }, // specifies the documents to update
{
$set: { "size.uom" : "cm", "status": "P" },
$currentDate : { "lastModified": true }
}
);
# 输出
{ "acknowledged" : true, "matchedCount" : 2, "modifiedCount" : 2 }
ps: 更新时如果字段不存在,就新建
删除文档
1,删除单个文档
db.book.deleteOne(
{ "status": "D" } // specifies the document to delete
);
# 输出
{ "acknowledged" : true, "deletedCount" : 1 }
2,删除多个文档
db.book.deleteMany(
{ "status" : "A" } // specifies the documents to delete
);
网友评论