MongoDB官网下载地址:https://www.mongodb.com/try/download/community MongoDB官方文档:https://docs.mongodb.com/v4.2/crud/

一、数据库操作

由于数据库名称在 MongoDB中不区分大小写,因此数据库名称不能仅因字符的大小写而不同。
数据库名称命名规范:
• 不能为空,不能包含空字符串(""),并且必须少于64个字符。
• 在Windows上,不能包含这些字符(/ \。“ $ * <>:|?)。
• 在Unix和Linux系统上,不能包含这些字符(/ \。“ $)。

1、查看数据库

1.1 查看当前有权限的所有数据库

语法格式如下:
show dbs
或者
show databases

> show dbs
admin   0.000GB
config  0.000GB
local   0.000GB
> show databases
admin   0.000GB
config  0.000GB
local   0.000GB

1.2 查看当前正在使用的数据库

语法格式如下:
db

> db
test

MongoDB默认的数据库是test,如果你没有选择数据库,集合就默认存放到 test数据库中(不推荐使用)。

2、选择和创建数据库

语法格式如下:
use DATABASE_NAME

• 如果数据库不存在就自动创建并选择该数据库,如果存在则选择使用该数据库。

> use m_db1
switched to db m_db1
> db
m_db1
> show dbs
admin   0.000GB
config  0.000GB
local   0.000GB

上面创建了 m_db1数据库,并切换到该数据库。
为什么查询所有数据库时,刚刚创建的 m_db1数据库并不在列表中, 数据库中没有数据是不会显示出来的。
如果要显示它,我们需要向 m_db1数据库中插入一些数据。

3、删除数据库

语法格式如下:
db.dropDatabase()

• 删除当前数据库,默认为 test数据库,你可以使用 db 命令查看当前数据库名。

> db
m_db1
> db.dropDatabase()
{ "ok" : 1 }
> db
m_db1

因为还处在当前数据库,还能看到数据库名称(MongoDB的特点),其实里面的数据已经真正删除了。退出之后就会删除数据库名。

二、集合操作

集合名称命名规范 :
• 不能包含$。
• 不能是一个空字符串(例如"")。
• 不能包含空字符。
• 不能以system.前缀开头。(仅供内部使用)。
集合名称空间的最大长度为120个字节,其中包括数据库名称,点(.)分隔符和集合名称(即 <database>.<collection>)。

1、创建集合

注意:在 MongoDB中,集合只有在内容插入后才会创建。因此,创建集合(数据表)后,需要再插入一个文档(记录),集合才会真正创建。

语法格式如下:
db.createCollection(name, options)

参数说明:

• name: 要创建的集合名称

• options: 可选参数, 指定有关内存大小及索引的选项

MongoDB不支持中文的全文 mongodb中文文档_数据库

在 m_db1数据库中创建集合。
(1)创建集合,然后在插入文档数据

> db
m_db1
> db.createCollection("coll1")
{ "ok" : 1 }
> db.coll1.insert({"name":"nnn"})
WriteResult({ "nInserted" : 1 })

(2)创建固定集合 ,整个集合空间大小 1024 B, 文档最大个数为 100 个。

> db.createCollection("coll2", {capped:true, size : 1024, max : 100})
{ "ok" : 1 }

2、查看集合

语法格式如下:
show tables
或者
show collections

> db
m_db1
> show tables
coll1
coll2
> show collections
coll1
coll2

3、删除集合

语法格式如下:
db.COLLECTION_NAME.drop()

• 如果成功删除选定集合,则 drop() 方法返回 true,否则返回 false。

> db
m_db1
> db.coll1.drop()
true
> db.coll1212.drop()
false
> show collections
coll2

三、文档增删改操作

文档(Document)名称命名规范 :
• 字段名称不能包含null字符。
• 顶级字段名称不能以美元符号($)字符开头。从MongoDB 3.6开始,服务器允许存储包含点(即.)和美元符号(即 $)的字段名称(不推荐)。

在MongoDB中,存储在集合中的每个文档都需要一个唯一的 _id 字段作为主键。默认情况下,_id 字段类型是ObjectId自动生成。如果插入的文档省略了该 _id 字段,则MongoDB驱动程序会自动为该字段生成一个ObjectId_id。
如果手动指定id时可以使用ObjectId,也可以使用MongoDB支持的任意类型,则使用手动指定的数据。

MongoDB 数据类型

下表为MongoDB中常用的几种数据类型。

MongoDB不支持中文的全文 mongodb中文文档_数据库/集合/文档基本操作_02


MongoDB不支持中文的全文 mongodb中文文档_MongoDB_03

1、插入文档

1.1 单条数据插入

(1)insert()
语法格式如下:

db.COLLECTION_NAME.insert(document)

  • save():如果 _id 主键存在则更新数据,如果不存在就插入数据。该方法在 3.2 版本之后已废弃,可以使用 db.collection.insertOne() 或 db.collection.replaceOne() 来代替。
    -insert(): 若插入的数据主键已经存在,则会抛 org.springframework.dao.DuplicateKeyException 异常,提示主键重复,不保存当前数据。
> db
m_db1
> db.coll3.insert({"name":"赵云1", age:18, sex:1})
WriteResult({ "nInserted" : 1 })

(2)insertOne()

语法格式如下:
db.COLLECTION_NAME.insertOne( <document>, { writeConcern: <document> } )

> db.coll3.insertOne({"name":"赵云2", age:19, sex:1})
{
        "acknowledged" : true,
        "insertedId" : ObjectId("60819f736d582e6a56373bfd")
}

1.2 多条数据批量插入

语法格式如下:
db.COLLECTION_NAME.insertMany( [ <document 1> , <document 2>, ... ], { writeConcern: <document>, ordered: <boolean> } )

参数说明:
• document:要写入的文档。
• writeConcern:写入策略,默认为 1,即要求确认写操作,0 是不要求。
• ordered:指定是否按顺序写入,默认 true,按顺序写入。

MongoDB的批量插入并不是同时成功或者同时失败。如果在插入过程中有一条数据失败,就会终止插入,但是在此之前的数据都会插入成功。

> db.coll3.insertMany([{"name":"赵云3", age:18, sex:0}, {"name":"赵云4", age:20, sex:1}])
{
        "acknowledged" : true,
        "insertedIds" : [
                ObjectId("6081a0066d582e6a56373bfe"),
                ObjectId("6081a0066d582e6a56373bff")
        ]
}
> db.coll3.find()
{ "_id" : ObjectId("60819f1e6d582e6a56373bfc"), "name" : "赵云1", "age" : 18, "sex" : 1 }
{ "_id" : ObjectId("60819f736d582e6a56373bfd"), "name" : "赵云2", "age" : 19, "sex" : 1 }
{ "_id" : ObjectId("6081a0066d582e6a56373bfe"), "name" : "赵云3", "age" : 18, "sex" : 0 }
{ "_id" : ObjectId("6081a0066d582e6a56373bff"), "name" : "赵云4", "age" : 20, "sex" : 1 }

2、更新文档

语法格式如下:
db.COLLECTION_NAME.update( <query>, <update>, { upsert: <boolean>, multi: <boolean>, writeConcern: <document> } )

参数说明:

• query : update的查询条件,类似sql update查询内where后面的。

• update : update的对象和一些更新的操作符(如MongoDB不支持中文的全文 mongodb中文文档_MongoDB_04inc…)等,也可以理解为sql update查询内set后面的

upsert: 可选,这个参数的意思是,如果不存在update的记录,是否插入objNew,true为插入,默认是false,不插入。

• multi : 可选,mongodb 默认是false,只更新找到的第一条记录,如果这个参数为true,就把按条件查出来多条记录全部更新。

• writeConcern :可选,抛出异常的级别。

MongoDB不支持中文的全文 mongodb中文文档_数据_05

2.1 覆盖修改单条数据(慎用

> db.coll3.update({"_id":ObjectId("60819f1e6d582e6a56373bfc")}, {"name":"赵子龙1"})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.coll3.find()
{ "_id" : ObjectId("60819f1e6d582e6a56373bfc"), "name" : "赵子龙1" }
{ "_id" : ObjectId("60819f736d582e6a56373bfd"), "name" : "赵云2", "age" : 19, "sex" : 1 }
{ "_id" : ObjectId("6081a0066d582e6a56373bfe"), "name" : "赵云3", "age" : 18, "sex" : 0 }
{ "_id" : ObjectId("6081a0066d582e6a56373bff"), "name" : "赵云4", "age" : 20, "sex" : 1 }

2.2 局部修改单条数据

> db.coll3.update({_id : ObjectId("60819f736d582e6a56373bfd")}, {$set: {name : "赵子龙2"}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.coll3.find()
{ "_id" : ObjectId("60819f1e6d582e6a56373bfc"), "name" : "赵子龙1" }
{ "_id" : ObjectId("60819f736d582e6a56373bfd"), "name" : "赵子龙2", "age" : 19, "sex" : 1 }
{ "_id" : ObjectId("6081a0066d582e6a56373bfe"), "name" : "赵云3", "age" : 18, "sex" : 0 }
{ "_id" : ObjectId("6081a0066d582e6a56373bff"), "name" : "赵云4", "age" : 20, "sex" : 1 }

> db.coll3.updateOne({_id : ObjectId("60819f736d582e6a56373bfd")}, {$set: {name : "赵子龙22"}})
{ "acknowledged" : true, "matchedCount" : 1, "modifiedCount" : 1 }
> db.coll3.find()
{ "_id" : ObjectId("60819f1e6d582e6a56373bfc"), "name" : "赵子龙1" }
{ "_id" : ObjectId("60819f736d582e6a56373bfd"), "name" : "赵子龙22", "age" : 19, "sex" : 1 }
{ "_id" : ObjectId("6081a0066d582e6a56373bfe"), "name" : "赵云3", "age" : 18, "sex" : 0 }
{ "_id" : ObjectId("6081a0066d582e6a56373bff"), "name" : "赵云4", "age" : 20, "sex" : 1 }

2.3 批量修改多条数据

> db.coll3.update({sex : NumberInt(1)}, {$set: {age : 30}}, {multi : true})
WriteResult({ "nMatched" : 2, "nUpserted" : 0, "nModified" : 2 })
> db.coll3.find()
{ "_id" : ObjectId("60819f1e6d582e6a56373bfc"), "name" : "赵子龙1" }
{ "_id" : ObjectId("60819f736d582e6a56373bfd"), "name" : "赵子龙2", "age" : 30, "sex" : 1 }
{ "_id" : ObjectId("6081a0066d582e6a56373bfe"), "name" : "赵云3", "age" : 18, "sex" : 0 }
{ "_id" : ObjectId("6081a0066d582e6a56373bff"), "name" : "赵云4", "age" : 30, "sex" : 1 }

> db.coll3.updateMany({age : {$gt: 17}}, {$set: {age : 33}})
{ "acknowledged" : true, "matchedCount" : 3, "modifiedCount" : 3 }
> db.coll3.find()
{ "_id" : ObjectId("60819f1e6d582e6a56373bfc"), "name" : "赵子龙1" }
{ "_id" : ObjectId("60819f736d582e6a56373bfd"), "name" : "赵子龙2", "age" : 33, "sex" : 1 }
{ "_id" : ObjectId("6081a0066d582e6a56373bfe"), "name" : "赵云3", "age" : 33, "sex" : 0 }
{ "_id" : ObjectId("6081a0066d582e6a56373bff"), "name" : "赵云4", "age" : 33, "sex" : 1 }

3、删除文档

语法格式如下:
db.COLLECTION_NAME.remove( <query>, { justOne: <boolean>, writeConcern: <document> } ) 下面的语句会删除所有数据,慎用
db.COLLECTION_NAME.remove({})

参数说明:
• query :(可选)删除的文档的条件。
• justOne : (可选)如果设为 true 或 1,则只删除一个文档,如果不设置该参数,或使用默认值 false,则删除所有匹配条件的文档。
• writeConcern :(可选)抛出异常的级别。

> db.coll3.remove({"_id": ObjectId("60819f1e6d582e6a56373bfc")})
WriteResult({ "nRemoved" : 1 })
> db.coll3.find()
{ "_id" : ObjectId("60819f736d582e6a56373bfd"), "name" : "赵子龙2", "age" : 33, "sex" : 1 }
{ "_id" : ObjectId("6081a0066d582e6a56373bfe"), "name" : "赵云3", "age" : 33, "sex" : 0 }
{ "_id" : ObjectId("6081a0066d582e6a56373bff"), "name" : "赵云4", "age" : 33, "sex" : 1 }

> db.coll3.deleteMany({"sex":1})
{ "acknowledged" : true, "deletedCount" : 2 }
> db.coll3.find()
{ "_id" : ObjectId("6081a0066d582e6a56373bfe"), "name" : "赵云3", "age" : 33, "sex" : 0 }

> db.coll3.deleteOne({"age":{"$gte":33}})
{ "acknowledged" : true, "deletedCount" : 1 }
> db.coll3.find()

进行一个或者多个记录操作时,推荐使用那种见名知意的操作方法。

Stay Hungry, Stay Foolish. 求知若饥,虚心若愚。