存储在集合中的数据为BSON格式,BSON(Binary JSON)是一种类似于JSON的二进制形式的存储格式
插入文档
db.collection.insertOne(<document>, { writeConcern: <document> })
向集合插入一个文档
db.collection.insertMany([<document 1>, <document 2>, ...], { writeConcern: <document>, ordered: <boolean> })
向集合插入多个文档
若插入的数据主键已经存在,则抛出org.springframework.dao.DuplicateKeyException异常,提示主键重复,不保存当前数据
document,待写入文档
writeConcern,写入策略,缺省值为1,即要求确认写操作,0为不要求
ordered,指定是否按顺序写入,缺省值为true,即按顺序写入
> db.col.insertOne({"username": "foo", "password": "bar"})
{
"acknowledged" : true,
"insertedId" : ObjectId("5f1b8e913cb574c7da6ad4b1")
}
> db.col.find() // 查看已插入文档
{ "_id" : ObjectId("5f1b8e913cb574c7da6ad4b1"), "username" : "foo", "password" : "bar" }
> db.col.insertMany([{"username": "foo"}, {"password": "bar"}])
{
"acknowledged" : true,
"insertedIds" : [
ObjectId("5f1b8f663cb574c7da6ad4b2"),
ObjectId("5f1b8f663cb574c7da6ad4b3")
]
}
> db.col.find()
{ "_id" : ObjectId("5f1b8f663cb574c7da6ad4b2"), "username" : "foo" }
{ "_id" : ObjectId("5f1b8f663cb574c7da6ad4b3"), "password" : "bar" }
> document = {"username": "foo", "password": "bar"}
{ "username" : "foo", "password" : "bar" }
> db.col.insertOne(document)
{
"acknowledged" : true,
"insertedId" : ObjectId("5f1b8fd93cb574c7da6ad4b4")
}
> db.col.find()
{ "_id" : ObjectId("5f1b8fd93cb574c7da6ad4b4"), "username" : "foo", "password" : "bar" }
更新文档
db.collection.updateOne(<query>, <update>, { upsert: <boolean>, writeConcern: <document> })
更新集合中第1个匹配的文档
query,查询条件
update,update对象与更新操作符
$set: { "key": "value" } // 修改值
$unset: { "key": "value" } // 删除键值对
$inc: { "key": number } // 数值变化
upsert,可选项,不存在update对象时是否插入,默认为false
writeConcern,可选项,抛出异常的级别
WriteConcern.NONE // 不抛出异常
WriteConcern.NORMAL // 抛出网络错误异常
WriteConcern.SAFE // 抛出网络错误异常与服务器错误异常,并等待服务器完成写操作
WriteConcern.MAJORITY // 抛出网络错误异常与服务器错误异常,并等待主服务器完成写操作
WriteConcern.FSYNC_SAFE // 抛出网络错误异常与服务器错误异常,写操作等待服务器将数据刷新到磁盘
WriteConcern.JOURNAL_SAFE // 抛出网络错误异常与服务器错误异常,写操作等待服务器提交到磁盘的日志文件
WriteConcern.REPLICAS_SAFE // 抛出网络错误异常与服务器错误异常,等待至少2台服务器完成写操作
> db.col.find()
{ "_id" : ObjectId("5f1b8fd93cb574c7da6ad4b4"), "username" : "foo", "password" : "bar" }
> db.col.updateOne({"username": "foo"}, {$set: {"username": "admin"}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.col.find()
{ "_id" : ObjectId("5f1b8fd93cb574c7da6ad4b4"), "username" : "admin", "password" : "bar" }
db.collection.updateMany(<query>, <update>, { upsert: <boolean>, writeConcern: <document> })
更新集合中所有匹配的文档
> db.col.find()
{ "_id" : ObjectId("5f1bda1ced3b7aae0bdc6cd4"), "username" : "foo", "password" : "bar" }
{ "_id" : ObjectId("5f1bda1ced3b7aae0bdc6cd5"), "username" : "foo", "password" : "bar" }
> db.col.updateMany({"username": "foo"}, {$set:{"username": "admin"}})
{ "acknowledged" : true, "matchedCount" : 2, "modifiedCount" : 2 }
> db.col.find()
{ "_id" : ObjectId("5f1bda1ced3b7aae0bdc6cd4"), "username" : "admin", "password" : "bar" }
{ "_id" : ObjectId("5f1bda1ced3b7aae0bdc6cd5"), "username" : "admin", "password" : "bar" }
db.collection.save(<document>, { writeConcern: <document> })
通过传入文档替换已有文档,若_id主键存在则更新,否则插入
document,文档数据
writeConcern,可选项,抛出异常的级别
> db.col.find()
{ "_id" : ObjectId("5f1b96933cb574c7da6ad4b5"), "username" : "foo", "password" : "bar" }
> db.col.save({ "_id" : ObjectId("5f1b96933cb574c7da6ad4b5"), "username" : "admin", "password" : "bar" })
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.col.find()
{ "_id" : ObjectId("5f1b96933cb574c7da6ad4b5"), "username" : "admin", "password" : "bar" }
删除文档
db.collection.remove(<query>, { justOne: <boolean>, writeConcern: <document> })
query,可选项,删除条件
justOne,可选项,若为true或1,则仅删除第1个匹配文档,若不设置该参数或使用缺省值false,则删除所有匹配文档
writeConcern,可选项,抛出异常的级别
> db.col.find()
{ "_id" : ObjectId("5f1b9baf3cb574c7da6ad4b6"), "username" : "foo", "password" : "bar" }
{ "_id" : ObjectId("5f1b9bb13cb574c7da6ad4b7"), "username" : "foo", "password" : "bar" }
> db.col.remove({"username": "foo"})
WriteResult({ "nRemoved" : 2 })
> db.col.find()
>
> db.col.find()
{ "_id" : ObjectId("5f1b9c0e3cb574c7da6ad4b8"), "username" : "foo", "password" : "bar" }
{ "_id" : ObjectId("5f1b9c103cb574c7da6ad4b9"), "username" : "foo", "password" : "bar" }
> db.col.remove({"username": "foo"}, 1)
WriteResult({ "nRemoved" : 1 })
> db.col.find()
{ "_id" : ObjectId("5f1b9c103cb574c7da6ad4b9"), "username" : "foo", "password" : "bar" }
删除集合中所有文档
db.col.remove({})
删除集合中第1个匹配的文档
db.col.deleteOne({ "key": "value" })
删除集合中所有匹配的文档
db.col.deleteMany({ "key": "value" })
回收磁盘空间(删除文档不释放空间)
db.repairDatabase()