原生mongodb操作
配置mongo环境变量(可以忽略)
window10 编辑环境变量 path
输入mongo检查是否配置成功
window
cmd窗口
mongo
MongoDB shell version v4.0.10
connecting to: mongodb://127.0.0.1:27017/?gssapiServiceName=mongodb
Implicit session: session { “id” : UUID(“5e46a1cc-7425-4d44-9935-7edf888b6392”) }
MongoDB server version: 4.2.1
WARNING: shell and server versions do not match
Server has startup warnings:
2019-10-31T16:12:54.220+0800 I CONTROL [initandlisten]
2019-10-31T16:12:54.221+0800 I CONTROL [initandlisten] ** WARNING: Access control is not enabled for the database.
2019-10-31T16:12:54.221+0800 I CONTROL [initandlisten] ** Read and write access to data and configuration is unrestricted.
2019-10-31T16:12:54.221+0800 I CONTROL [initandlisten]
Enable MongoDB’s free cloud-based monitoring service, which will then receive and display
metrics about your deployment (disk utilization, CPU, operation statistics, etc).
The monitoring data will be available on a MongoDB website with a unique URL accessible to you
and anyone you share the URL with. MongoDB may use this information to make product
improvements and to suggest MongoDB products and deployment options to you.
To enable free monitoring, run the following command: db.enableFreeMonitoring()
To permanently disable this reminder, run the following command: db.disableFreeMonitoring()
数据库列表展示
show dbs;
admin 0.000GB
comment 0.000GB
config 0.000GB
itying 0.000GB
local 0.000GB
test 0.000GB
选择数据库操作
use 数据库名
use itying;
switched to db itying
新建数据库
use qun;
switched to db qun
show dbs;
admin 0.000GB
comment 0.000GB
config 0.000GB
itying 0.000GB
local 0.000GB
test 0.000GB
数据库不存在,因为没有数据
新建一个数据库只需要新增一个数据即可
db.表名.insert({“字段”:“值”});
use qun;
db.room.insert({“color”:“red”,“age”:30});WriteResult({ “nInserted” : 1 });
show dbs;
admin 0.000GB
comment 0.000GB
config 0.000GB
itying 0.000GB
local 0.000GB
qun 0.000GB
test 0.000GB
use qun;
switched to db qun
展示表
show collections;
room
db.book.insert({“pageNum”:100,“color”:“red”,“length”:20,“weigth”:10,“heigth”:3};
WriteResult({ “nInserted” : 1 })
db.cup.insert({“size”:“middle”});
WriteResult({ “nInserted” : 1 })
show collections;
book
cup
room
查询表记录
db.表名.find();
db.book.find();
结果:
{ “_id” : ObjectId(“5dc36c509892f2b414f98019”), “pageNum” : 100, “color” : “red”, “length” : 20, “weigth” : 10, “heigth” : 3 }
多新增几个数据
db.book.insert({“pageNum”:100,“color”:“red”,“length”:20,“weigth”:10,“heigth”:3});
WriteResult({ “nInserted” : 1 })
db.book.insert({“pageNum”:100,“color”:“green”,“length”:20,“weigth”:10,“heigth”:4});
WriteResult({ “nInserted” : 1 })
db.book.insert({“pageNum”:1000,“color”:“yellow”,“length”:25,“weigth”:15,“heigth”:4});
WriteResult({ “nInserted” : 1 })
db.book.find();
{ “_id” : ObjectId(“5dc36c509892f2b414f98019”), “pageNum” : 100, “color” : “red”, “length” : 20, “weigth” : 10, “heigth” : 3 }
{ “_id” : ObjectId(“5dc36d059892f2b414f9801b”), “pageNum” : 100, “color” : “red”, “length” : 20, “weigth” : 10, “heigth” : 3 }
{ “_id” : ObjectId(“5dc36d169892f2b414f9801c”), “pageNum” : 100, “color” : “green”, “length” : 20, “weigth” : 10, “heigth” : 4 }
{ “_id” : ObjectId(“5dc36d329892f2b414f9801d”), “pageNum” : 1000, “color” : “yellow”, “length” : 25, “weigth” : 15, “heigth” : 4 }
条件查询表记录
db.表名.find({“字段”:条件(默认是等于)});
db.book.find({“color”:“red”});
{ “_id” : ObjectId(“5dc36c509892f2b414f98019”), “pageNum” : 100, “color” : “red”, “length” : 20, “weigth” : 10, “heigth” : 3 }
{ “_id” : ObjectId(“5dc36d059892f2b414f9801b”), “pageNum” : 100, “color” : “red”, “length” : 20, “weigth” : 10, “heigth” : 3 }
db.book.find({“weigth”:10});
{ “_id” : ObjectId(“5dc36c509892f2b414f98019”), “pageNum” : 100, “color” : “red”, “length” : 20, “weigth” : 10, “heigth” : 3 }
{ “_id” : ObjectId(“5dc36d059892f2b414f9801b”), “pageNum” : 100, “color” : “red”, “length” : 20, “weigth” : 10, “heigth” : 3 }
{ “_id” : ObjectId(“5dc36d169892f2b414f9801c”), “pageNum” : 100, “color” : “green”, “length” : 20, “weigth” : 10, “heigth” : 4 }
db.表名.find({“字段”:条件(默认是等于),“字段”:“条件”(默认是等于)});
db.book.find({“color”:“green”,“length”:20});
{ “_id” : ObjectId(“5dc36d169892f2b414f9801c”), “pageNum” : 100, “color” : “green”, “length” : 20, “weigth” : 10, “heigth” : 4 }
db.book.find({“pagenum”:100});
字段不存在,不报错,查不到而已
db.book.find({“pageNum”:100});
{ “_id” : ObjectId(“5dc36c509892f2b414f98019”), “pageNum” : 100, “color” : “red”, “length” : 20, “weigth” : 10, “heigth” : 3 }
{ “_id” : ObjectId(“5dc36d059892f2b414f9801b”), “pageNum” : 100, “color” : “red”, “length” : 20, “weigth” : 10, “heigth” : 3 }
{ “_id” : ObjectId(“5dc36d169892f2b414f9801c”), “pageNum” : 100, “color” : “green”, “length” : 20, “weigth” : 10, “heigth” : 4 }
db.book.find({“pageNum”:100,“pageNum”:1000});(此处想表达的是pageNum是100或是1000,写法错误)
{ “_id” : ObjectId(“5dc36d329892f2b414f9801d”), “pageNum” : 1000, “color” : “yellow”, “length” : 25, “weigth” : 15, “heigth” : 4 }
db.book.find({“pageNum”:100,“pageNum”:1000,“pageNum”:100});(写法错误,pageNum前面两个条件屏蔽了)
{ “_id” : ObjectId(“5dc36c509892f2b414f98019”), “pageNum” : 100, “color” : “red”, “length” : 20, “weigth” : 10, “heigth” : 3 }
{ “_id” : ObjectId(“5dc36d059892f2b414f9801b”), “pageNum” : 100, “color” : “red”, “length” : 20, “weigth” : 10, “heigth” : 3 }
{ “_id” : ObjectId(“5dc36d169892f2b414f9801c”), “pageNum” : 100, “color” : “green”, “length” : 20, “weigth” : 10, “heigth” : 4 }
条件
{$gt:20} 大于20
db.book.find({“heigth”:{KaTeX parse error: Expected 'EOF', got '}' at position 5: gt:3}̲}); { "_id" : O…lt:20} 小于20
db.book.find({“heigth”:{KaTeX parse error: Expected 'EOF', got '}' at position 5: lt:4}̲}); { "_id" : O…gte:20} 大于等于20
db.book.find({“heigth”:{KaTeX parse error: Expected 'EOF', got '}' at position 6: gte:3}̲}); { "_id" : O…lte:20} 小于等于20
db.book.find({“heigth”:{KaTeX parse error: Expected 'EOF', got '}' at position 6: lte:4}̲}); { "_id" : O…lte:20,KaTeX parse error: Expected 'EOF', got '}' at position 7: gte:10}̲ 小于等于20,大于等于10 …lte:5,$gte:2}});
{ “_id” : ObjectId(“5dc36c509892f2b414f98019”), “pageNum” : 100, “color” : “red”, “length” : 20, “weigth” : 10, “heigth” : 3 }
{ “_id” : ObjectId(“5dc36d059892f2b414f9801b”), “pageNum” : 100, “color” : “red”, “length” : 20, “weigth” : 10, “heigth” : 3 }
{ “_id” : ObjectId(“5dc36d169892f2b414f9801c”), “pageNum” : 100, “color” : “green”, “length” : 20, “weigth” : 10, “heigth” : 4 }
{ “_id” : ObjectId(“5dc36d329892f2b414f9801d”), “pageNum” : 1000, “color” : “yellow”, “length” : 25, “weigth” : 15, “heigth” : 4 }
模糊查询
db.表名.find({“字段”:/关键字/});
db.book.find({“color”:/r/});
{ “_id” : ObjectId(“5dc36c509892f2b414f98019”), “pageNum” : 100, “color” : “red”, “length” : 20, “weigth” : 10, “heigth” : 3 }
{ “_id” : ObjectId(“5dc36d059892f2b414f9801b”), “pageNum” : 100, “color” : “red”, “length” : 20, “weigth” : 10, “heigth” : 3 }
{ “_id” : ObjectId(“5dc36d169892f2b414f9801c”), “pageNum” : 100, “color” : “green”, “length” : 20, “weigth” : 10, “heigth” : 4 }
查询条件以关键字开头
db.表名.find({“字段”:/^关键字/});
db.book.find({“color”:/^r/});
{ “_id” : ObjectId(“5dc36c509892f2b414f98019”), “pageNum” : 100, “color” : “red”, “length” : 20, “weigth” : 10, “heigth” : 3 }
{ “_id” : ObjectId(“5dc36d059892f2b414f9801b”), “pageNum” : 100, “color” : “red”, “length” : 20, “weigth” : 10, “heigth” : 3 }
查询指定列
db.表名.find({“字段”:条件}(可空),{“字段”:1});
db.book.find({“color”:/^r/},{“color”:1});
{ “_id” : ObjectId(“5dc36c509892f2b414f98019”), “color” : “red” }
{ “_id” : ObjectId(“5dc36d059892f2b414f9801b”), “color” : “red” }
db.book.find({},{“color”:1});
{ “_id” : ObjectId(“5dc36c509892f2b414f98019”), “color” : “red” }
{ “_id” : ObjectId(“5dc36d059892f2b414f9801b”), “color” : “red” }
{ “_id” : ObjectId(“5dc36d169892f2b414f9801c”), “color” : “green” }
{ “_id” : ObjectId(“5dc36d329892f2b414f9801d”), “color” : “yellow” }
db.book.find({“color”:1});
查询不到
查询排序
db.表名.find({“字段”:条件}).sort({“age”:-1(1)});
db.book.find().sort({“color”:-1});
{ “_id” : ObjectId(“5dc36d329892f2b414f9801d”), “pageNum” : 1000, “color” : “yellow”, “length” : 25, “weigth” : 15, “heigth” : 4 }
{ “_id” : ObjectId(“5dc36c509892f2b414f98019”), “pageNum” : 100, “color” : “red”, “length” : 20, “weigth” : 10, “heigth” : 3 }
{ “_id” : ObjectId(“5dc36d059892f2b414f9801b”), “pageNum” : 100, “color” : “red”, “length” : 20, “weigth” : 10, “heigth” : 3 }
{ “_id” : ObjectId(“5dc36d169892f2b414f9801c”), “pageNum” : 100, “color” : “green”, “length” : 20, “weigth” : 10, “heigth” : 4 }
db.book.find().sort({“color”:1});
{ “_id” : ObjectId(“5dc36d169892f2b414f9801c”), “pageNum” : 100, “color” : “green”, “length” : 20, “weigth” : 10, “heigth” : 4 }
{ “_id” : ObjectId(“5dc36c509892f2b414f98019”), “pageNum” : 100, “color” : “red”, “length” : 20, “weigth” : 10, “heigth” : 3 }
{ “_id” : ObjectId(“5dc36d059892f2b414f9801b”), “pageNum” : 100, “color” : “red”, “length” : 20, “weigth” : 10, “heigth” : 3 }
{ “_id” : ObjectId(“5dc36d329892f2b414f9801d”), “pageNum” : 1000, “color” : “yellow”, “length” : 25, “weigth” : 15, “heigth” : 4 }
分页
db.表名.find({“字段”:条件}).limit(2);前两条
db.表名.find({“字段”:条件}).skip(1).limit(2);跳过第一天查询第二三条
db.book.find().limit(2);
{ “_id” : ObjectId(“5dc36c509892f2b414f98019”), “pageNum” : 100, “color” : “red”, “length” : 20, “weigth” : 10, “heigth” : 3 }
{ “_id” : ObjectId(“5dc36d059892f2b414f9801b”), “pageNum” : 100, “color” : “red”, “length” : 20, “weigth” : 10, “heigth” : 3 }
db.book.find().skip(1).limit(2);
{ “_id” : ObjectId(“5dc36d059892f2b414f9801b”), “pageNum” : 100, “color” : “red”, “length” : 20, “weigth” : 10, “heigth” : 3 }
{ “_id” : ObjectId(“5dc36d169892f2b414f9801c”), “pageNum” : 100, “color” : “green”, “length” : 20, “weigth” : 10, “heigth” : 4 }
or
db.表名。find({$or:[{“字段:条件”},{“字段”:条件}]});
db.book.find({$or:[{“pageNum”:100},{“pageNum”:1000}]});
{ “_id” : ObjectId(“5dc36c509892f2b414f98019”), “pageNum” : 100, “color” : “red”, “length” : 20, “weigth” : 10, “heigth” : 3 }
{ “_id” : ObjectId(“5dc36d059892f2b414f9801b”), “pageNum” : 100, “color” : “red”, “length” : 20, “weigth” : 10, “heigth” : 3 }
{ “_id” : ObjectId(“5dc36d169892f2b414f9801c”), “pageNum” : 100, “color” : “green”, “length” : 20, “weigth” : 10, “heigth” : 4 }
{ “_id” : ObjectId(“5dc36d329892f2b414f9801d”), “pageNum” : 1000, “color” : “yellow”, “length” : 25, “weigth” : 15, “heigth” : 4 }
and
db.表名。find({“字段:条件”,“字段”:条件});
db.book.find({“pageNum”:100,“color”:“green”});
{ “_id” : ObjectId(“5dc36d169892f2b414f9801c”), “pageNum” : 100, “color” : “green”, “length” : 20, “weigth” : 10, “heigth” : 4 }
查询第一条
db.表名.findOne();
db.book.findOne();
{
“_id” : ObjectId(“5dc36c509892f2b414f98019”),
“pageNum” : 100,
“color” : “red”,
“length” : 20,
“weigth” : 10,
“heigth” : 3
}
统计数量
db.表名。find({“字段:条件”,“字段”:条件}).count();
db.book.find().count();
4
修改数据
db.表名.update({“字段”:“条件”}, {$set(不写则修改全部):{“字段”:“值”,“字段”:“值”}});
db.book.update({“pageNum”:1000},{“color”:“green”});
WriteResult({ “nMatched” : 1, “nUpserted” : 0, “nModified” : 1 })
db.book.find();
{ “_id” : ObjectId(“5dc36c509892f2b414f98019”), “pageNum” : 100, “color” : “red”, “length” : 20, “weigth” : 10, “heigth” : 3 }
{ “_id” : ObjectId(“5dc36d059892f2b414f9801b”), “pageNum” : 100, “color” : “red”, “length” : 20, “weigth” : 10, “heigth” : 3 }
{ “_id” : ObjectId(“5dc36d169892f2b414f9801c”), “pageNum” : 100, “color” : “green”, “length” : 20, “weigth” : 10, “heigth” : 4 }
{ “_id” : ObjectId(“5dc36d329892f2b414f9801d”), “color” : “green” }
db.book.update({“heigth”:4},{$set:{“color”:“red”}});
WriteResult({ “nMatched” : 1, “nUpserted” : 0, “nModified” : 1 })
db.book.find();
{ “_id” : ObjectId(“5dc36c509892f2b414f98019”), “pageNum” : 100, “color” : “red”, “length” : 20, “weigth” : 10, “heigth” : 3 }
{ “_id” : ObjectId(“5dc36d059892f2b414f9801b”), “pageNum” : 100, “color” : “red”, “length” : 20, “weigth” : 10, “heigth” : 3 }
{ “_id” : ObjectId(“5dc36d169892f2b414f9801c”), “pageNum” : 100, “color” : “red”, “length” : 20, “weigth” : 10, “heigth” : 4 }
{ “_id” : ObjectId(“5dc36d329892f2b414f9801d”), “color” : “green” }
删除数据
db.表名.remove({“字段”:“条件”});
db.book.remove({“color”:“green”});
WriteResult({ “nRemoved” : 1 })
db.book.find();
{ “_id” : ObjectId(“5dc36c509892f2b414f98019”), “pageNum” : 100, “color” : “red”, “length” : 20, “weigth” : 10, “heigth” : 3 }
{ “_id” : ObjectId(“5dc36d059892f2b414f9801b”), “pageNum” : 100, “color” : “red”, “length” : 20, “weigth” : 10, “heigth” : 3 }
{ “_id” : ObjectId(“5dc36d169892f2b414f9801c”), “pageNum” : 100, “color” : “red”, “length” : 20, “weigth” : 10, “heigth” : 4 }
db.表名.remove({“字段”:“条件”},{justOne:true});
db.book.remove({“color”:“red”},{justOne:true});
WriteResult({ “nRemoved” : 1 })
db.book.find();
{ “_id” : ObjectId(“5dc36d059892f2b414f9801b”), “pageNum” : 100, “color” : “red”, “length” : 20, “weigth” : 10, “heigth” : 3 }
{ “_id” : ObjectId(“5dc36d169892f2b414f9801c”), “pageNum” : 100, “color” : “red”, “length” : 20, “weigth” : 10, “heigth” : 4 }
删除表
db.表名.drop();
db.book.drop();
true
show collections;
cup
room
删除库
db.dropDatabase();
show dbs;
admin 0.000GB
comment 0.000GB
config 0.000GB
itying 0.000GB
local 0.000GB
qun 0.000GB
test 0.000GB
db.dropDatabase()
{ “dropped” : “qun”, “ok” : 1 }
show dbs;
admin 0.000GB
comment 0.000GB
config 0.000GB
itying 0.000GB
local 0.000GB
test 0.000GB