Microsoft Windows [版本 10.0.17134.472]
© 2018 Microsoft Corporation。保留所有权利。
C:\WINDOWS\system32>mongo --开启mongob客户端
MongoDB shell version v3.4.10
connecting to: mongodb://127.0.0.1:27017
MongoDB server version: 3.4.10
Server has startup warnings:
2019-07-24T08:51:53.290+0800 I CONTROL [initandlisten]
2019-07-24T08:51:53.290+0800 I CONTROL [initandlisten] ** WARNING: Access control is not enabled for the database.
2019-07-24T08:51:53.290+0800 I CONTROL [initandlisten] ** Read and write access to data and configuration is unrestricted.
2019-07-24T08:51:53.290+0800 I CONTROL [initandlisten]
db --查看当前的数据库
test
change db — 错误
2019-07-25T19:51:00.735+0800 E QUERY [thread1] SyntaxError: missing ; before statement @(shell):1:7
use db --使用db这个数据库,如果db不存在会直接创建一个
switched to db db
show dbs --查看当前所有的数据库
admin 0.000GB
local 0.000GB
test 0.000GB
use db1
switched to db db1
db
db1
db.db1.insert({“name”:“bingbing”}) --给db1这个集合(相当于表)插入一条记录.注意:如果db1这个集合不存在会直接创建一个.
–数据要使用{}括起来,形式:{key:value}
show dbs
admin 0.000GB
db1 0.000GB
local 0.000GB
test 0.000GB
show collections --查看当前数据库中所有的集合
db1
db.createCollection(“Emp”,{capped:true,size:6124800,max:10000}) --创建新的集合
{ “ok” : 1 }
show collections
Emp
db1
db.db1.drop() --删除集合
true
show collections
Emp
db.Emp.insert({“name”:“bing”})
WriteResult({ “nInserted” : 1 })
db.Emp.find() --查看集合的详情
{ “_id” : ObjectId(“5d399bf3cd6648e542dac1a5”), “name” : “bing” }
db.user.insert({name:‘chen’,age:‘20’,sex:‘male’,hobby:[‘song’,‘readbook’]}) --文档采用bjson,类似于json
WriteResult({ “nInserted” : 1 })
db.user.find()
{ “_id” : ObjectId(“5d399d59cd6648e542dac1a9”), “name” : “chen”, “age” : “20”, “sex” : “male”, “hobby” : [ “song”, “readbook” ] }
document={name:‘zhangsan’,age:‘10’,sex:‘male’} --插入数据时,可以先创建一个文档
{ “name” : “zhangsan”, “age” : “10”, “sex” : “male” }
db.user.insert(document) --直接将文档整体插入
WriteResult({ “nInserted” : 1 })
db.user.find()
{ “_id” : ObjectId(“5d399d59cd6648e542dac1a9”), “name” : “chen”, “age” : “20”, “sex” : “male”, “hobby” : [ “song”, “readbook” ] }
{ “_id” : ObjectId(“5d399db3cd6648e542dac1aa”), “name” : “zhangsan”, “age” : “10”, “sex” : “male” }
document={name:‘lisi’,age:‘30’,sex:‘female’}
{ “name” : “lisi”, “age” : “30”, “sex” : “female” }
db.user1.save(document) – 也可以使用save()函数
WriteResult({ “nInserted” : 1 })
db.user1.find()
{ “_id” : ObjectId(“5d39a51dcd6648e542dac1ab”), “name” : “lisi”, “age” : “30”, “sex” : “female” }
var docu = db.user1.insertOne({“a”:3}) --3.2后 一次插入一条记录
docu --查看插入后的结果
{
“acknowledged” : true,
“insertedId” : ObjectId(“5d39a5c8cd6648e542dac1ac”)
}
var docu1 = db.collection.insertMany({‘b’:4},{‘c’:5}) --一次插入多条,这里要使用 [ ]
2019-07-25T20:53:34.148+0800 E QUERY [thread1] TypeError: documents.map is not a function :
DBCollection.prototype.insertMany@src/mongo/shell/crud_api.js:283:1
@(shell):1:17
var docu1 = db.collection.insertMany([{‘b’:4},{‘c’:5}])
douc1
2019-07-25T20:54:11.092+0800 E QUERY [thread1] ReferenceError: douc1 is not defined :
@(shell):1:1
docu1
{
“acknowledged” : true,
“insertedIds” : [
ObjectId(“5d39a66fcd6648e542dac1ad”),
ObjectId(“5d39a66fcd6648e542dac1ae”)
]
}
db.collection.find()
{ “_id” : ObjectId(“5d39a5c8cd6648e542dac1ac”), “a” : 3 }
{ “_id” : ObjectId(“5d39a66fcd6648e542dac1ad”), “b” : 4 }
{ “_id” : ObjectId(“5d39a66fcd6648e542dac1ae”), “c” : 5 }
var docu1 = db.temp.insertMany([{‘b’:4},{‘c’:5}])
docu1
{
“acknowledged” : true,
“insertedIds” : [
ObjectId(“5d39a6b3cd6648e542dac1af”),
ObjectId(“5d39a6b3cd6648e542dac1b0”)
]
}
db.temp.find() --查看当前集合的记录
{ “_id” : ObjectId(“5d39a6b3cd6648e542dac1af”), “b” : 4 }
{ “_id” : ObjectId(“5d39a6b3cd6648e542dac1b0”), “c” : 5 }
db.collection.find()
{ “_id” : ObjectId(“5d39a5c8cd6648e542dac1ac”), “a” : 3 }
{ “_id” : ObjectId(“5d39a66fcd6648e542dac1ad”), “b” : 4 }
{ “_id” : ObjectId(“5d39a66fcd6648e542dac1ae”), “c” : 5 }
db.user.find()
{ “_id” : ObjectId(“5d399d59cd6648e542dac1a9”), “name” : “chen”, “age” : “20”, “sex” : “male”, “hobby” : [ “song”, “readbook” ] }
{ “_id” : ObjectId(“5d399db3cd6648e542dac1aa”), “name” : “zhangsan”, “age” : “10”, “sex” : “male” }
db.user.find().pretty() --以格式化方式查看详情
{
“_id” : ObjectId(“5d399d59cd6648e542dac1a9”),
“name” : “chen”,
“age” : “20”,
“sex” : “male”,
“hobby” : [
“song”,
“readbook”
]
}
{
“_id” : ObjectId(“5d399db3cd6648e542dac1aa”),
“name” : “zhangsan”,
“age” : “10”,
“sex” : “male”
}
db.user.update({‘name’:‘chen’},{KaTeX parse error: Expected 'EOF', got '}' at position 24: …me':'xiaochen'}}̲) --使用update()…set:{‘name’:‘xiaochen’}}) --使用update()进行更新指定的值
WriteResult({ “nMatched” : 1, “nUpserted” : 0, “nModified” : 1 })
db.user.insert({name:‘xiaochen’,age:‘50’,sex:‘female’,hobby:[‘song1’,‘writebook’]})
WriteResult({ “nInserted” : 1 })
db.user.find().pretty()
{
“_id” : ObjectId(“5d399d59cd6648e542dac1a9”),
“name” : “xiaochen”,
“age” : “20”,
“sex” : “male”,
“hobby” : [
“song”,
“readbook”
]
}
{
“_id” : ObjectId(“5d399db3cd6648e542dac1aa”),
“name” : “zhangsan”,
“age” : “10”,
“sex” : “male”
}
{
“_id” : ObjectId(“5d39ab70cd6648e542dac1b1”),
“name” : “xiaochen”,
“age” : “50”,
“sex” : “female”,
“hobby” : [
“song1”,
“writebook”
]
}
db.user.update({‘name’:‘xiaochen’},{$set:{‘name’:‘xiaolan’}},{multi:true}) – 可以一次更新多条 ,这时要将multi设置成true
WriteResult({ “nMatched” : 2, “nUpserted” : 0, “nModified” : 2 })
db.user.find().pretty()
{
“_id” : ObjectId(“5d399d59cd6648e542dac1a9”),
“name” : “xiaolan”,
“age” : “20”,
“sex” : “male”,
“hobby” : [
“song”,
“readbook”
]
}
{
“_id” : ObjectId(“5d399db3cd6648e542dac1aa”),
“name” : “zhangsan”,
“age” : “10”,
“sex” : “male”
}
{
“_id” : ObjectId(“5d39ab70cd6648e542dac1b1”),
“name” : “xiaolan”,
“age” : “50”,
“sex” : “female”,
“hobby” : [
“song1”,
“writebook”
]
}
查询
逻辑与或
db.user.find({“name”:“xiaolan”})
{ “_id” : ObjectId(“5d399d59cd6648e542dac1a9”), “name” : “xiaolan”, “age” : “20”, “sex” : “male”, “hobby” : [ “song”, “readbook” ] }
{ “_id” : ObjectId(“5d39ab70cd6648e542dac1b1”), “name” : “xiaolan”, “age” : “50”, “sex” : “female”, “hobby” : [ “song1”, “writebook” ] }
db.user.find({“name”:“xiaolan”,“hobby”:[“song1”,“writebook”]}) — 直接写多个,中间以,隔开
{ “_id” : ObjectId(“5d39ab70cd6648e542dac1b1”), “name” : “xiaolan”, “age” : “50”, “sex” : “female”, “hobby” : [ “song1”, “writebook” ] }
db.user.find({KaTeX parse error: Expected 'EOF', got '}' at position 38: …,{"age":"10"}] }̲) --或:以or开头,后面跟的是[]
{ “_id” : ObjectId(“5d399d59cd6648e542dac1a9”), “name” : “xiaolan”, “age” : “20”, “sex” : “male”, “hobby” : [ “song”, “readbook” ] }
{ “_id” : ObjectId(“5d399db3cd6648e542dac1aa”), “name” : “zhangsan”, “age” : “10”, “sex” : “male” }
{ “_id” : ObjectId(“5d39ab70cd6648e542dac1b1”), “name” : “xiaolan”, “age” : “50”, “sex” : “female”, “hobby” : [ “song1”, “writebook” ] }
db.user.find({name:/ao/}) --模糊查询 ,注意:必须有{}
{ “_id” : ObjectId(“5d399d59cd6648e542dac1a9”), “name” : “xiaolan”, “age” : “20”, “sex” : “male”, “hobby” : [ “song”, “readbook” ] }
{ “_id” : ObjectId(“5d39ab70cd6648e542dac1b1”), “name” : “xiaolan”, “age” : “50”, “sex” : “female”, “hobby” : [ “song1”, “writebook” ] }
{ “_id” : ObjectId(“5d39b4ca19e5313df358bd2f”), “name” : “xiaozhao” }
{ “_id” : ObjectId(“5d39b53319e5313df358bd34”), “name” : “xiaozhao” }
{ “_id” : ObjectId(“5d39b60819e5313df358bd3e”), “name” : “xiaozhao” }
db.user.find({name:/^ao/}) --模糊查询 以ao开头
db.user.find({name:/ao$/}) --模糊查询 以ao结尾
{ “_id” : ObjectId(“5d39b4ca19e5313df358bd2f”), “name” : “xiaozhao” }
{ “_id” : ObjectId(“5d39b53319e5313df358bd34”), “name” : “xiaozhao” }
{ “_id” : ObjectId(“5d39b60819e5313df358bd3e”), “name” : “xiaozhao” }
db.user.find({},{“name”:1,_id:0}).limit(2) --查询前两条记录,只显示name 注意:find后面第一个空的{}必须有 name后面的1代表显示 ,0是不显示
–_id:0 表示不显示_id limit(2) 表示查询2条
{ “name” : “xiaolan” }
{ “name” : “zhangsan” }
db.user.find()
{ “_id” : ObjectId(“5d399d59cd6648e542dac1a9”), “name” : “xiaolan”, “age” : “20”, “sex” : “male”, “hobby” : [ “song”, “readbook” ] }
{ “_id” : ObjectId(“5d399db3cd6648e542dac1aa”), “name” : “zhangsan”, “age” : “10”, “sex” : “male” }
{ “_id” : ObjectId(“5d39ab70cd6648e542dac1b1”), “name” : “xiaolan”, “age” : “50”, “sex” : “female”, “hobby” : [ “song1”, “writebook” ] }
{ “_id” : ObjectId(“5d39b4ca19e5313df358bd2f”), “name” : “xiaozhao” }
{ “_id” : ObjectId(“5d39b53319e5313df358bd34”), “name” : “xiaozhao” }
{ “_id” : ObjectId(“5d39b58519e5313df358bd39”), “name1” : “xiaozhao” }
{ “_id” : ObjectId(“5d39b60819e5313df358bd3e”), “name” : “xiaozhao” }
db.user.find({},{“name”:1,_id:0}).limit(3).skip(1) --跳过第一条,查询后面的3条
{ “name” : “zhangsan” }
{ “name” : “xiaolan” }
{ “name” : “xiaozhao” }
db.user.find({},{“name”:1,_id:0}).limit(2).skip(1)
{ “name” : “zhangsan” }
{ “name” : “xiaolan” }
db.user.find({},{“age”:1,_id:0}).limit(2).skip(1)
{ “age” : “10” }
{ “age” : “50” }
db.user.find({},{“age”:1,_id:2}).limit(2).skip(1) --显示age,同时显示_id
{ “_id” : ObjectId(“5d399db3cd6648e542dac1aa”), “age” : “10” }
{ “_id” : ObjectId(“5d39ab70cd6648e542dac1b1”), “age” : “50” }
db.user.find({},{“age”:1,_id:0}).limit(2).skip(1)
{ “age” : “10” }
{ “age” : “50” }
db.user.find({},{“age”:1}).limit(2).skip(1) --默认显示_id
{ “_id” : ObjectId(“5d399db3cd6648e542dac1aa”), “age” : “10” }
{ “_id” : ObjectId(“5d39ab70cd6648e542dac1b1”), “age” : “50” }
db.user.find({},{“age”:0,_id:0}).limit(2).skip(1) —“age”:0 表示显示除age以外的内容
{ “name” : “zhangsan”, “sex” : “male” }
{ “name” : “xiaolan”, “sex” : “female”, “hobby” : [ “song1”, “writebook” ] }