mongodb学习笔记2:基本增删改查使用

#MongoDB的基本使用:
在bin目录下,执行mongo脚本,可以开启mongo的shell命令行。
`./mongo`
MongoDB shell version: 3.0.6
connecting to: test
Server has startup warnings:
2020-04-24T04:43:56.723-0700 I CONTROL  [initandlisten] ** WARNING: You are running this process as the root user, which is not recommended.
2020-04-24T04:43:56.723-0700 I CONTROL  [initandlisten]
2020-04-24T04:43:56.723-0700 I CONTROL  [initandlisten]
2020-04-24T04:43:56.723-0700 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.
2020-04-24T04:43:56.723-0700 I CONTROL  [initandlisten] **        We suggest setting it to 'never'
2020-04-24T04:43:56.723-0700 I CONTROL  [initandlisten]
2020-04-24T04:43:56.723-0700 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.
2020-04-24T04:43:56.723-0700 I CONTROL  [initandlisten] **        We suggest setting it to 'never'
2020-04-24T04:43:56.723-0700 I CONTROL  [initandlisten]
>
数据库:  
创建数据库:`use studydb`
> use studydb
switched to db studydb
如果数据库不存在,mongodb会自动创建数据库。
查询当前数据库:`db`
~~~
> db
studydb
~~~
查询所有数据库:`show dbs`
> show dbs
local  0.078GB
上面并没有我们的数据库,要想显示它,需要先往里面填加数据。mongodb中,集合只有在插入数据后才会真正被创建。也就是说创建集合后再插入一个文档,集合才会被创建。  
新增数据:`db.bookcoll.insert({"name":"mongodb","content":"hello world!"})`
> db.bookcoll.insert({"name":"mongodb","content":"hello world!"})
WriteResult({ "nInserted" : 1 })
db表示当前数据库,bookcoll表示集合, 如果没有会自动创建。
> show dbs
local  0.078GB
study  0.078GB
创建集合:`db.createCollection(name,options)`
**name: 集合名称(必填)
options: 可配置参数(选填)**
options取值:
    capped:  布尔型。如果为true,则创建固定集合。固定集合是有固定大小的集合,当达到最大大小时,它会自动覆盖最早的文档。当为true时,必须指定size参数。    
    autoIndexId: 布尔型。如果为true,自动在_id字段创建索引。默认为false。  
    size:  数值型。为固定集合指定一个最大值,以千字节(KB)记。  
    max: 数值型。指定固定集合中包含文档的最大数量。
mongodb在插入文档时,先检查固定集合的size字段,在检查max字段。  
> db.createCollection("java")
{ "ok" : 1 }
查看所有集合:`show collections`或`show tables`
> show collections
bookcoll
java
system.indexes
> show tables
bookcoll
java
system.indexes
>
创建固定集合:`db.createCollection("books", {capped:true, autoIndexId:true, size:1000, max:5})`  
> db.createCollection("book",{capped:true, autoIndexId:true, size:1000, max:5})
{ "ok" : 1 }
插入文档:`db.book.insert()`或`db.book.save()`  
可以通过insert或save方法向集合中插入文档。db当前数据库,book:要插入数据的集合名。还可以通过定义变量的方式插入。
之前创建了一个固定集合。设置的最大值是5,现在插入五条数据测试。
> db.book.insert({"name":"java"})
WriteResult({ "nInserted" : 1 })
> db.book.save({"price":100.00})
WriteResult({ "nInserted" : 1 })
> db.book.save({"age":10})
WriteResult({ "nInserted" : 1 })
> document={"phone":"10086"}
{ "phone" : "10086" }
> db.book.insert(document)
WriteResult({ "nInserted" : 1 })
> db.book.insert({"content":"hello world"})
WriteResult({ "nInserted" : 1 })
查看插入的数据:`db.book.find()`
{ "_id" : ObjectId("5ea2e439383590406bd4e0f7"), "name" : "java" }
{ "_id" : ObjectId("5ea2e454383590406bd4e0f8"), "price" : 100 }
{ "_id" : ObjectId("5ea2e483383590406bd4e0f9"), "age" : 10 }
{ "_id" : ObjectId("5ea2e4f0383590406bd4e0fa"), "phone" : "10086" }
{ "_id" : ObjectId("5ea2e554383590406bd4e0fb"), "content" : "hello world" }
_id字段为mongodb自动为文档生成的主键。在mongodb中,每一个database就相当于关系型数据库中的database。
collection就相当于关系型数据库中的表table。而每一条文档就是行row,文档中的字段就是列column。在mongodb中,并不要求每一行的每一列都相同。
无论是列名还是列的数据格式,都可以不同。
上面插入的是一个固定集合,我们设置的最大数是五个,现在已经五个了,我们再插入一条记录。
> db.book.insert({"price":"50"})
WriteResult({ "nInserted" : 1 })
> db.book.find()
{ "_id" : ObjectId("5ea2e454383590406bd4e0f8"), "price" : 100 }
{ "_id" : ObjectId("5ea2e483383590406bd4e0f9"), "age" : 10 }
{ "_id" : ObjectId("5ea2e4f0383590406bd4e0fa"), "phone" : "10086" }
{ "_id" : ObjectId("5ea2e554383590406bd4e0fb"), "content" : "hello world" }
{ "_id" : ObjectId("5ea2e725383590406bd4e0fc"), "price" : "50" }
可以看到, 超过最大数量时,会自动覆盖掉最早的数据,而且即使字段名一样时,字段的值也可以不相同。再mongodb中,每一条文档就是单独的行,与其他的行的字段类型等没有关连。
更新文档: `db.book.update()`或`db.book.save()`
mongodb中通过update()方法或save()方法来更新。
**update方法:** `db.book.update(<query>,<update>,{upsert: <boolean>, multi: <boolean>, writeConcern: <document>})`
方法参数介绍:
    query: 类似sql语句中的where后的条件  
    update: 更新的对象,类似与sql update语句中set后的属性
    upsert: 当更新的对象找不到时,是否插入数据,默认false。
    multi:mongodb默认只更新找到的第一条记录,如果改为true,则更改  
    writeConcern: 抛出异常级别
> db.book.find()
{ "_id" : ObjectId("5ea2e454383590406bd4e0f8"), "price" : 100 }
{ "_id" : ObjectId("5ea2e483383590406bd4e0f9"), "age" : 10 }
{ "_id" : ObjectId("5ea2e4f0383590406bd4e0fa"), "phone" : "10086" }
{ "_id" : ObjectId("5ea2e554383590406bd4e0fb"), "content" : "hello world" }
{ "_id" : ObjectId("5ea2e725383590406bd4e0fc"), "price" : "50" }
> db.book.update({"price":"50"},{$set:{"price":"30"}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.book.find()
{ "_id" : ObjectId("5ea2e454383590406bd4e0f8"), "price" : 100 }
{ "_id" : ObjectId("5ea2e483383590406bd4e0f9"), "age" : 10 }
{ "_id" : ObjectId("5ea2e4f0383590406bd4e0fa"), "phone" : "10086" }
{ "_id" : ObjectId("5ea2e554383590406bd4e0fb"), "content" : "hello world" }
{ "_id" : ObjectId("5ea2e725383590406bd4e0fc"), "price" : "30" }
>
加上可选参数:
> db.book.update({"price":100},{$set:{"price":"30"}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.book.find()
{ "_id" : ObjectId("5ea2e454383590406bd4e0f8"), "price" : "30" }
{ "_id" : ObjectId("5ea2e483383590406bd4e0f9"), "age" : 10 }
{ "_id" : ObjectId("5ea2e4f0383590406bd4e0fa"), "phone" : "10086" }
{ "_id" : ObjectId("5ea2e554383590406bd4e0fb"), "content" : "hello world" }
{ "_id" : ObjectId("5ea2e725383590406bd4e0fc"), "price" : "30" }
> db.book.update({"price":"30"},{$set:{"price":"50"}},{multi:true})
WriteResult({ "nMatched" : 2, "nUpserted" : 0, "nModified" : 2 })
> db.book.find()
{ "_id" : ObjectId("5ea2e454383590406bd4e0f8"), "price" : "50" }
{ "_id" : ObjectId("5ea2e483383590406bd4e0f9"), "age" : 10 }
{ "_id" : ObjectId("5ea2e4f0383590406bd4e0fa"), "phone" : "10086" }
{ "_id" : ObjectId("5ea2e554383590406bd4e0fb"), "content" : "hello world" }
{ "_id" : ObjectId("5ea2e725383590406bd4e0fc"), "price" : "50" }
修改时, {$set:{}},修改的内容的key必须是当前记录内已经有的key,如果没有就会修改失败,不能修改已有的子段。就像再mysql中update命令一样。    
 update table set a=?,b=? where c=? 
条件字段必须存在, set的字段也必须是已有字段,不能是不存在的字段。
**save方法:通过传入的文档来替换已有的文档** `db.book.save(<documemt>,{writeConcern:<document>})`
> db.book.save({ "_id" : ObjectId("5ea2e483383590406bd4e0f9"), "age":"50" })
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.book.find().pretty()
{ "_id" : ObjectId("5ea2e454383590406bd4e0f8"), "price" : "50" }
{ "_id" : ObjectId("5ea2e483383590406bd4e0f9"), "age" : "50" }
{ "_id" : ObjectId("5ea2e4f0383590406bd4e0fa"), "phone" : "10086" }
{ "_id" : ObjectId("5ea2e554383590406bd4e0fb"), "content" : "hello world" }
{ "_id" : ObjectId("5ea2e725383590406bd4e0fc"), "price" : "50" }
我们通过修改了{ "_id" : ObjectId("5ea2e483383590406bd4e0f9")}的整个文档,同样,修改时只能修改已有字段,不能新增字段。  
查询文档:`db.book.find(query,projection)`
query: 可选搜索条件
projection: 使用投影操作符返回指定的键值。
首先插入一条数据:
> show collections
book
bookcoll
java
system.indexes
> db.bookcoll.find()
{ "_id" : ObjectId("5ea2d99b383590406bd4e0f6"), "name" : "mongodb", "content" : "hello world!" }
> db.bookcoll.insert({"name":"java","size":"30","content":"hello world","price":"100"})
WriteResult({ "nInserted" : 1 })
> db.bookcoll.find({"name":"java"},{"name":1,"price":1,"content":1})
{ "_id" : ObjectId("5ea433ede672477b2e10d90d"), "name" : "java", "content" : "hello world", "price" : "100" }
projection 指定要返回的键。用法:
db.collection.find(query,{key1:1,key2:1})
当值为1时返回,指定的键,返回key为1的键,不返回其他键,当key为0时,返回其他键,不返回指定键。
要么都为1,要么都为0,不能混用。
正例:db.collection.find(query,{key1:1,key2:1})或db.collection.find(query,{key1:0,key2:0})
反例:db.collection.find(query,{key1:1,key2:0})
此外,查询条件query还支持or语句and语句大于小于等等。
db.collection.find({$or:[{"content":"hello world!"},{"name":"java"}]})
> db.bookcoll.find()
{ "_id" : ObjectId("5ea2d99b383590406bd4e0f6"), "name" : "mongodb", "content" : "hello world!" }
{ "_id" : ObjectId("5ea433ede672477b2e10d90d"), "name" : "java", "size" : "30", "content" : "hello world", "price" : "100" }
> db.bookcoll.find({$or:[{"content":"hello world!"},{"name":"java"}]})
{ "_id" : ObjectId("5ea2d99b383590406bd4e0f6"), "name" : "mongodb", "content" : "hello world!" }
{ "_id" : ObjectId("5ea433ede672477b2e10d90d"), "name" : "java", "size" : "30", "content" : "hello world", "price" : "100" }
该语句等价与sql:select * from bookcoll where name = 'java' or content = 'hello world!'
删除一个文档:`db.collection.remove(query,justOne)`
query: 搜索条件
justOne: 可选参数,如果设为true,或1,则只删除找到的第一条文档,默认为false,删除所有匹配条件的文档。
**注意:固定集合不能删除记录,但可以删除整个集合重建**
> db.bookcoll.find()
{ "_id" : ObjectId("5ea2d99b383590406bd4e0f6"), "name" : "mongodb", "content" : "hello world!" }
{ "_id" : ObjectId("5ea433ede672477b2e10d90d"), "name" : "java", "size" : "30", "content" : "hello world", "price" : "100" }
> db.bookcoll.remove({"name":"mongodb"})
WriteResult({ "nRemoved" : 1 })
> db.bookcoll.find()
{ "_id" : ObjectId("5ea433ede672477b2e10d90d"), "name" : "java", "size" : "30", "content" : "hello world", "price" : "100" }
> db.bookcoll.insert({"name":"mongodb","price":"100","content":"hello mongodb"})
WriteResult({ "nInserted" : 1 })
> db.bookcoll.insert({"name":"python","price":"100","content":"hello python"})
WriteResult({ "nInserted" : 1 })
> db.bookcoll.find()
{ "_id" : ObjectId("5ea433ede672477b2e10d90d"), "name" : "java", "size" : "30", "content" : "hello world", "price" : "100" }
{ "_id" : ObjectId("5ea43a99e672477b2e10d90e"), "name" : "mongodb", "price" : "100", "content" : "hello mongodb" }
{ "_id" : ObjectId("5ea43ab1e672477b2e10d90f"), "name" : "python", "price" : "100", "content" : "hello python" }
删除一条
> db.bookcoll.remove({"price":"100"},true)
WriteResult({ "nRemoved" : 1 })
> db.bookcoll.find()
{ "_id" : ObjectId("5ea43a99e672477b2e10d90e"), "name" : "mongodb", "price" : "100", "content" : "hello mongodb" }
{ "_id" : ObjectId("5ea43ab1e672477b2e10d90f"), "name" : "python", "price" : "100", "content" : "hello python" }
满足条件全部删除
> db.bookcoll.remove({"price":"100"})
WriteResult({ "nRemoved" : 2 })
> db.bookcoll.find()
>
> db.bookcoll.insert({"name":"mongodb","price":"100","content":"hello mongodb"})
WriteResult({ "nInserted" : 1 })
> db.bookcoll.insert({"name":"python","price":"100","content":"hello python"})
WriteResult({ "nInserted" : 1 })
> db.bookcoll.insert({"name":"java","price":"100","content":"hello java"})
WriteResult({ "nInserted" : 1 })
删除集合所有数据
> db.bookcoll.remove({})
WriteResult({ "nRemoved" : 3 })
删除集合:`db.collection.drop()`
> show collections
book
bookcoll
java
system.indexes
> db.book.drop()
true
> show collections
bookcoll
java
system.indexes
删除数据库:`db.dropDatabase()`
> show dbs
local  0.078GB
study  0.078GB
> use study
switched to db study
> db.dropDatabase()
{ "dropped" : "study", "ok" : 1 }
> show dbs
local  0.078GB
原文地址:https://www.cnblogs.com/Zs-book1/p/12773314.html