反向引用MongoDB
这是我在完成Dotinstall的MongoDB入门教程时所做的笔记。
1. 数据库相关操作
1.1. 我想查看数据库列表
> show dbs
1.2. 我想建立一个数据库
> use mydb
1.3. 我想将当前使用的数据库切换到另一个数据库。
> use mydb
1.4. 我想查看有关数据库的总体信息。
> db.stats()
{
"db" : "local",
"collections" : 1,
"objects" : 2,
"avgObjSize" : 1423,
"dataSize" : 2846,
"storageSize" : 32768,
"numExtents" : 0,
"indexes" : 1,
"indexSize" : 32768,
"ok" : 1
}
1.5. 我想删除数据库。 (Wǒ .)
> db.dropDatabase()
{ "dropped" : "mydb", "ok" : 1 }
2. 关于集合的操作
想查看Collection的列表
> show collections
users
我想创建一个Collection。
> db.createCollection("users")
{ "ok" : 1 }
2.3. 我想要修改Collection的名称。
> db.users.renameCollection("customers")
{ "ok" : 1 }
2.4. 我想删除Collection。
> db.customers.drop()
true
3. 关于文档的操作
3.1. 创建操作
我想创建一个文档。
> db.users.insert({name: "inaba", score: 100})
WriteResult({ "nInserted" : 1 })
> db.users.insert({name: "taguchi", score: 100, tags: ["web", "mobile"]})
WriteResult({ "nInserted" : 1 })
我想用3.1.2版本的for循环来创建Document。
> for (var i = 0; i < 10; i++) {
... db.users.insert({name: "inaba", score: Math.random()})
... }
WriteResult({ "nInserted" : 1 })
3.2. 阅读操作
我想查看文档的列表。
> db.users.find()
{ "_id" : ObjectId("56a620e956bad693a0943b6f"), "name" : "inaba", "score" : 100 }
{ "_id" : ObjectId("56a6212d56bad693a0943b70"), "name" : "taguchi", "score" : 100, "tags" : [ "web", "mobile" ] }
3.2.2. 我想知道文档的数量
> db.users.count()
22
想要根据条件提取文档的需求
3.2.3.1. 相等/不相等条件
> db.users.find({name: "taguchi"})
{ "_id" : ObjectId("56a6261d56bad693a0943b8f"), "name" : "taguchi", "score" : 100, "tags" : [ "web", "mobile" ] }
> db.users.find({score: {$eq: 0.7505969204990847}})
{ "_id" : ObjectId("56a625ec56bad693a0943b88"), "name" : "inaba", "score" : 0.7505969204990847 }
> db.users.find({score: {$ne: 0.7505969204990847}})
{ "_id" : ObjectId("56a625ec56bad693a0943b85"), "name" : "inaba", "score" : 0.696594451078292 }
{ "_id" : ObjectId("56a625ec56bad693a0943b86"), "name" : "inaba", "score" : 0.05682618331619349 }
{ "_id" : ObjectId("56a625ec56bad693a0943b87"), "name" : "inaba", "score" : 0.8607307464013195 }
{ "_id" : ObjectId("56a625ec56bad693a0943b89"), "name" : "inaba", "score" : 0.6928152829564784 }
{ "_id" : ObjectId("56a625ec56bad693a0943b8a"), "name" : "inaba", "score" : 0.12730769462146607 }
{ "_id" : ObjectId("56a625ec56bad693a0943b8b"), "name" : "inaba", "score" : 0.6896075949404165 }
{ "_id" : ObjectId("56a625ec56bad693a0943b8c"), "name" : "inaba", "score" : 0.7224980348563089 }
{ "_id" : ObjectId("56a625ec56bad693a0943b8d"), "name" : "inaba", "score" : 0.6146229528346954 }
{ "_id" : ObjectId("56a625ec56bad693a0943b8e"), "name" : "inaba", "score" : 0.6281710749925821 }
{ "_id" : ObjectId("56a6261d56bad693a0943b8f"), "name" : "taguchi", "score" : 100, "tags" : [ "web", "mobile" ] }
3.2.3.2. 少于 / 少于等于 / 大于 / 大于等于 条件
> db.users.find({score: {$gte: 0.5}})
{ "_id" : ObjectId("56a625ec56bad693a0943b85"), "name" : "inaba", "score" : 0.696594451078292 }
{ "_id" : ObjectId("56a625ec56bad693a0943b87"), "name" : "inaba", "score" : 0.8607307464013195 }
{ "_id" : ObjectId("56a625ec56bad693a0943b88"), "name" : "inaba", "score" : 0.7505969204990847 }
{ "_id" : ObjectId("56a625ec56bad693a0943b89"), "name" : "inaba", "score" : 0.6928152829564784 }
{ "_id" : ObjectId("56a625ec56bad693a0943b8b"), "name" : "inaba", "score" : 0.6896075949404165 }
{ "_id" : ObjectId("56a625ec56bad693a0943b8c"), "name" : "inaba", "score" : 0.7224980348563089 }
{ "_id" : ObjectId("56a625ec56bad693a0943b8d"), "name" : "inaba", "score" : 0.6146229528346954 }
{ "_id" : ObjectId("56a625ec56bad693a0943b8e"), "name" : "inaba", "score" : 0.6281710749925821 }
{ "_id" : ObjectId("56a6261d56bad693a0943b8f"), "name" : "taguchi", "score" : 100, "tags" : [ "web", "mobile" ] }
> db.users.find({score: {$lte: 0.5}})
{ "_id" : ObjectId("56a625ec56bad693a0943b86"), "name" : "inaba", "score" : 0.05682618331619349 }
{ "_id" : ObjectId("56a625ec56bad693a0943b8a"), "name" : "inaba", "score" : 0.12730769462146607 }
> db.users.find({score: {$lt: 0.5}})
{ "_id" : ObjectId("56a625ec56bad693a0943b86"), "name" : "inaba", "score" : 0.05682618331619349 }
{ "_id" : ObjectId("56a625ec56bad693a0943b8a"), "name" : "inaba", "score" : 0.12730769462146607 }
> db.users.find({score: {$gt: 0.5}})
{ "_id" : ObjectId("56a625ec56bad693a0943b85"), "name" : "inaba", "score" : 0.696594451078292 }
{ "_id" : ObjectId("56a625ec56bad693a0943b87"), "name" : "inaba", "score" : 0.8607307464013195 }
{ "_id" : ObjectId("56a625ec56bad693a0943b88"), "name" : "inaba", "score" : 0.7505969204990847 }
{ "_id" : ObjectId("56a625ec56bad693a0943b89"), "name" : "inaba", "score" : 0.6928152829564784 }
{ "_id" : ObjectId("56a625ec56bad693a0943b8b"), "name" : "inaba", "score" : 0.6896075949404165 }
{ "_id" : ObjectId("56a625ec56bad693a0943b8c"), "name" : "inaba", "score" : 0.7224980348563089 }
{ "_id" : ObjectId("56a625ec56bad693a0943b8d"), "name" : "inaba", "score" : 0.6146229528346954 }
{ "_id" : ObjectId("56a625ec56bad693a0943b8e"), "name" : "inaba", "score" : 0.6281710749925821 }
{ "_id" : ObjectId("56a6261d56bad693a0943b8f"), "name" : "taguchi", "score" : 100, "tags" : [ "web", "mobile" ] }
3.2.3.3. 使用正则表达式来指定条件
> db.users.find({name: /^.+chi$/})
{ "_id" : ObjectId("56a6261d56bad693a0943b8f"), "name" : "taguchi", "score" : 100, "tags" : [ "web", "mobile" ] }
3.2.3.4. 我想要通过And条件来指定多个条件提取文档。
> db.users.find({name: /^i.+a$/, score: {$gte: 0.5}})
{ "_id" : ObjectId("56a625ec56bad693a0943b85"), "name" : "inaba", "score" : 0.696594451078292 }
{ "_id" : ObjectId("56a625ec56bad693a0943b87"), "name" : "inaba", "score" : 0.8607307464013195 }
{ "_id" : ObjectId("56a625ec56bad693a0943b88"), "name" : "inaba", "score" : 0.7505969204990847 }
{ "_id" : ObjectId("56a625ec56bad693a0943b89"), "name" : "inaba", "score" : 0.6928152829564784 }
{ "_id" : ObjectId("56a625ec56bad693a0943b8b"), "name" : "inaba", "score" : 0.6896075949404165 }
{ "_id" : ObjectId("56a625ec56bad693a0943b8c"), "name" : "inaba", "score" : 0.7224980348563089 }
{ "_id" : ObjectId("56a625ec56bad693a0943b8d"), "name" : "inaba", "score" : 0.6146229528346954 }
{ "_id" : ObjectId("56a625ec56bad693a0943b8e"), "name" : "inaba", "score" : 0.6281710749925821 }
想要使用Or条件指定多个条件来提取Document。
> db.users.find({$or: [{name: /^i.+a$/}, {score: {$gte: 0.5}}]})
{ "_id" : ObjectId("56a625ec56bad693a0943b85"), "name" : "inaba", "score" : 0.696594451078292 }
{ "_id" : ObjectId("56a625ec56bad693a0943b86"), "name" : "inaba", "score" : 0.05682618331619349 }
{ "_id" : ObjectId("56a625ec56bad693a0943b87"), "name" : "inaba", "score" : 0.8607307464013195 }
{ "_id" : ObjectId("56a625ec56bad693a0943b88"), "name" : "inaba", "score" : 0.7505969204990847 }
{ "_id" : ObjectId("56a625ec56bad693a0943b89"), "name" : "inaba", "score" : 0.6928152829564784 }
{ "_id" : ObjectId("56a625ec56bad693a0943b8a"), "name" : "inaba", "score" : 0.12730769462146607 }
{ "_id" : ObjectId("56a625ec56bad693a0943b8b"), "name" : "inaba", "score" : 0.6896075949404165 }
{ "_id" : ObjectId("56a625ec56bad693a0943b8c"), "name" : "inaba", "score" : 0.7224980348563089 }
{ "_id" : ObjectId("56a625ec56bad693a0943b8d"), "name" : "inaba", "score" : 0.6146229528346954 }
{ "_id" : ObjectId("56a625ec56bad693a0943b8e"), "name" : "inaba", "score" : 0.6281710749925821 }
{ "_id" : ObjectId("56a6261d56bad693a0943b8f"), "name" : "taguchi", "score" : 100, "tags" : [ "web", "mobile" ] }
想要使用IN句从Document中提取数据。
> db.users.find({score: {$in: [0.696594451078292, 0.6896075949404165]}})
{ "_id" : ObjectId("56a625ec56bad693a0943b85"), "name" : "inaba", "score" : 0.696594451078292 }
{ "_id" : ObjectId("56a625ec56bad693a0943b8b"), "name" : "inaba", "score" : 0.6896075949404165 }
我想提取一些具有特定字段的文档。
> db.users.find({age: {$exists: true}})
{ "_id" : ObjectId("56a638ce56bad693a0943b90"), "name" : "tanaka", "score" : 52, "age" : 23 }
我想查看字段中包含的值的列表。 (Wǒ de zhí de .)
> db.users.distinct("name")
[ "inaba", "taguchi" ]
3.2.5. 我想通过指定字段来提取文档。
> db.users.find({}, {name: true})
{ "_id" : ObjectId("56a625ec56bad693a0943b85"), "name" : "inaba" }
{ "_id" : ObjectId("56a625ec56bad693a0943b86"), "name" : "inaba" }
{ "_id" : ObjectId("56a625ec56bad693a0943b87"), "name" : "inaba" }
{ "_id" : ObjectId("56a625ec56bad693a0943b88"), "name" : "inaba" }
{ "_id" : ObjectId("56a625ec56bad693a0943b89"), "name" : "inaba" }
{ "_id" : ObjectId("56a625ec56bad693a0943b8a"), "name" : "inaba" }
{ "_id" : ObjectId("56a625ec56bad693a0943b8b"), "name" : "inaba" }
{ "_id" : ObjectId("56a625ec56bad693a0943b8c"), "name" : "inaba" }
{ "_id" : ObjectId("56a625ec56bad693a0943b8d"), "name" : "inaba" }
{ "_id" : ObjectId("56a625ec56bad693a0943b8e"), "name" : "inaba" }
{ "_id" : ObjectId("56a6261d56bad693a0943b8f"), "name" : "taguchi" }
{ "_id" : ObjectId("56a638ce56bad693a0943b90"), "name" : "tanaka" }
> db.users.find({age: {$exists: true}}, {name: 1})
{ "_id" : ObjectId("56a638ce56bad693a0943b90"), "name" : "tanaka" }
> db.users.find({age: {$exists: true}}, {name: 0})
{ "_id" : ObjectId("56a638ce56bad693a0943b90"), "score" : 52, "age" : 23 }
> db.users.find({age: {$exists: true}}, {name: 1, _id: 0})
{ "name" : "tanaka" }
3.2.6. 我想要对提取的结果进行排序
> db.users.find({}, {_id: false}).sort({score: 1})
{ "name" : "inaba", "score" : 0.05682618331619349 }
{ "name" : "inaba", "score" : 0.12730769462146607 }
{ "name" : "inaba", "score" : 0.6146229528346954 }
{ "name" : "inaba", "score" : 0.6281710749925821 }
{ "name" : "inaba", "score" : 0.6896075949404165 }
{ "name" : "inaba", "score" : 0.6928152829564784 }
{ "name" : "inaba", "score" : 0.696594451078292 }
{ "name" : "inaba", "score" : 0.7224980348563089 }
{ "name" : "inaba", "score" : 0.7505969204990847 }
{ "name" : "inaba", "score" : 0.8607307464013195 }
{ "name" : "tanaka", "score" : 52, "age" : 23 }
{ "name" : "taguchi", "score" : 100, "tags" : [ "web", "mobile" ] }
> db.users.find({}, {_id: false}).sort({score: -1})
{ "name" : "taguchi", "score" : 100, "tags" : [ "web", "mobile" ] }
{ "name" : "tanaka", "score" : 52, "age" : 23 }
{ "name" : "inaba", "score" : 0.8607307464013195 }
{ "name" : "inaba", "score" : 0.7505969204990847 }
{ "name" : "inaba", "score" : 0.7224980348563089 }
{ "name" : "inaba", "score" : 0.696594451078292 }
{ "name" : "inaba", "score" : 0.6928152829564784 }
{ "name" : "inaba", "score" : 0.6896075949404165 }
{ "name" : "inaba", "score" : 0.6281710749925821 }
{ "name" : "inaba", "score" : 0.6146229528346954 }
{ "name" : "inaba", "score" : 0.12730769462146607 }
{ "name" : "inaba", "score" : 0.05682618331619349 }
3.2.7. 想要限制提取结果的数量
想要从提取结果中提取出前N个项目。
> db.users.find({}, {_id: false}).sort({score: -1}).limit(3)
{ "name" : "taguchi", "score" : 100, "tags" : [ "web", "mobile" ] }
{ "name" : "tanaka", "score" : 52, "age" : 23 }
{ "name" : "inaba", "score" : 0.8607307464013195 }
3.2.7.2. 只想要提取结果中的第一条
> db.users.findOne({}, {_id: 0})
{ "name" : "inaba", "score" : 0.696594451078292 }
3.2.7.3. 想要跳过前N项并抽取结果中的其余项目。
> db.users.find({}, {_id: 0}).skip(7)
{ "name" : "inaba", "score" : 0.7224980348563089 }
{ "name" : "inaba", "score" : 0.6146229528346954 }
{ "name" : "inaba", "score" : 0.6281710749925821 }
{ "name" : "taguchi", "score" : 100, "tags" : [ "web", "mobile" ] }
{ "name" : "tanaka", "score" : 52, "age" : 23 }
3.3. 更新操作
想要更新与搜索条件匹配的第一个Document中的特定字段。
> db.users.find({name: "taguchi"}, {_id: 0})
{ "name" : "taguchi", "score" : 100, "tags" : [ "web", "mobile" ] }
> db.users.update({name: "taguchi"}, {$set: {score: 80}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.users.find({name: "taguchi"}, {_id: 0})
{ "name" : "taguchi", "score" : 80, "tags" : [ "web", "mobile" ] }
> db.users.update({name: "taguchi"}, {$set: {score: 80, tag: "new tag"}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.users.find({name: "taguchi"}, {_id: 0})
{ "name" : "taguchi", "score" : 80, "tags" : [ "web", "mobile" ], "tag" : "new tag" }
想要更新与搜索条件匹配的第一个文件的整个文件。
> db.users.update({name: "taguchi"}, {name: "taguchi"})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.users.find({name: "taguchi"}, {_id: 0})
{ "name" : "taguchi" }
要更新符合搜索条件的所有文档的特定字段。
> db.users.find({name: "inaba"}, {_id: 0})
{ "name" : "inaba", "score" : 0.696594451078292 }
{ "name" : "inaba", "score" : 0.05682618331619349 }
{ "name" : "inaba", "score" : 0.8607307464013195 }
{ "name" : "inaba", "score" : 0.7505969204990847 }
{ "name" : "inaba", "score" : 0.6928152829564784 }
{ "name" : "inaba", "score" : 0.12730769462146607 }
{ "name" : "inaba", "score" : 0.6896075949404165 }
{ "name" : "inaba", "score" : 0.7224980348563089 }
{ "name" : "inaba", "score" : 0.6146229528346954 }
{ "name" : "inaba", "score" : 0.6281710749925821 }
> db.users.update({name: "inaba"}, {$set: {score: 80}}, {multi: true})
WriteResult({ "nMatched" : 10, "nUpserted" : 0, "nModified" : 10 })
> db.users.find({name: "inaba"}, {_id: 0})
{ "name" : "inaba", "score" : 80 }
{ "name" : "inaba", "score" : 80 }
{ "name" : "inaba", "score" : 80 }
{ "name" : "inaba", "score" : 80 }
{ "name" : "inaba", "score" : 80 }
{ "name" : "inaba", "score" : 80 }
{ "name" : "inaba", "score" : 80 }
{ "name" : "inaba", "score" : 80 }
{ "name" : "inaba", "score" : 80 }
{ "name" : "inaba", "score" : 80 }
想要将搜索条件匹配到的第一个文档中的特定字段值加上N。
> db.users.update({name: "inaba"}, {$inc: {score: 5}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.users.find({name: "inaba"}, {_id: 0})
{ "name" : "inaba", "score" : 85 }
{ "name" : "inaba", "score" : 80 }
{ "name" : "inaba", "score" : 80 }
{ "name" : "inaba", "score" : 80 }
{ "name" : "inaba", "score" : 80 }
{ "name" : "inaba", "score" : 80 }
{ "name" : "inaba", "score" : 80 }
{ "name" : "inaba", "score" : 80 }
{ "name" : "inaba", "score" : 80 }
{ "name" : "inaba", "score" : 80 }
3.3.5. 我想在满足搜索条件的第一个文档中的特定字段上乘以N。
> db.users.update({name: "inaba"}, {$mul: {score: 5}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.users.find({name: "inaba"}, {_id: 0})
{ "name" : "inaba", "score" : 425 }
{ "name" : "inaba", "score" : 80 }
{ "name" : "inaba", "score" : 80 }
{ "name" : "inaba", "score" : 80 }
{ "name" : "inaba", "score" : 80 }
{ "name" : "inaba", "score" : 80 }
{ "name" : "inaba", "score" : 80 }
{ "name" : "inaba", "score" : 80 }
{ "name" : "inaba", "score" : 80 }
{ "name" : "inaba", "score" : 80 }
想要将符合搜索条件的第一个文档中特定字段的名称进行更改。
> db.users.update({name: "inaba"}, {$rename: {score: "point"}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.users.find({name: "inaba"}, {_id: 0})
{ "name" : "inaba", "point" : 2135 }
{ "name" : "inaba", "score" : 85 }
{ "name" : "inaba", "score" : 85 }
{ "name" : "inaba", "score" : 85 }
{ "name" : "inaba", "score" : 85 }
{ "name" : "inaba", "score" : 85 }
{ "name" : "inaba", "score" : 85 }
{ "name" : "inaba", "score" : 85 }
{ "name" : "inaba", "score" : 85 }
{ "name" : "inaba", "score" : 85 }
3.3.7. 我想要为符合搜索条件的第一篇文档添加字段。
> db.users.update({name: "inaba"}, {$set: {team: "team-4"}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.users.find({name: "inaba"}, {_id: 0})
{ "name" : "inaba", "point" : 2135, "team" : "team-4" }
{ "name" : "inaba", "score" : 85 }
{ "name" : "inaba", "score" : 85 }
{ "name" : "inaba", "score" : 85 }
{ "name" : "inaba", "score" : 85 }
{ "name" : "inaba", "score" : 85 }
{ "name" : "inaba", "score" : 85 }
{ "name" : "inaba", "score" : 85 }
{ "name" : "inaba", "score" : 85 }
{ "name" : "inaba", "score" : 85 }
3.3.8. 我想要删除与搜索条件匹配的第一个文档的字段。
> db.users.update({name: "inaba"}, {$unset: {point: ""}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.users.find({name: "inaba"}, {_id: 0})
{ "name" : "inaba", "team" : "team-4" }
{ "name" : "inaba", "score" : 85 }
{ "name" : "inaba", "score" : 85 }
{ "name" : "inaba", "score" : 85 }
{ "name" : "inaba", "score" : 85 }
{ "name" : "inaba", "score" : 85 }
{ "name" : "inaba", "score" : 85 }
{ "name" : "inaba", "score" : 85 }
{ "name" : "inaba", "score" : 85 }
{ "name" : "inaba", "score" : 85 }
如果存在与搜索条件匹配的文档,则更新第一份文档,如果没有,则创建新文档。
> db.users.find({name: "nakata"}, {_id: 0})
>
> db.users.update({name: "nakata"}, {name: "nakata", score: 99}, {upsert: true})
WriteResult({
"nMatched" : 0,
"nUpserted" : 1,
"nModified" : 0,
"_id" : ObjectId("56a8b8358be74315594831c3")
})
> db.users.find({name: "nakata"}, {_id: 0})
{ "name" : "nakata", "score" : 99 }
> db.users.update({name: "nakata"}, {name: "nakata", score: 90}, {upsert: true})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.users.find({name: "nakata"}, {_id: 0})
{ "name" : "nakata", "score" : 90 }
3.4. 删除操作
想要删除所有文件。
> db.users.remove({})
WriteResult({ "nRemoved" : 22 })
我想删除与搜索条件匹配的所有文档。
> db.users.remove({name: "inaba"})
WriteResult({ "nRemoved" : 10 })
4. 关于索引的操作。
我想知道Collection中定义的索引是什么。
> db.users.getIndexes()
[
{
"v" : 1,
"key" : {
"_id" : 1
},
"name" : "_id_",
"ns" : "mydb.users"
}
]
想要创建4.2. Index
> db.users.createIndex({score: -1})
{
"createdCollectionAutomatically" : false,
"numIndexesBefore" : 1,
"numIndexesAfter" : 2,
"ok" : 1
}
> db.users.getIndexes()
[
{
"v" : 1,
"key" : {
"_id" : 1
},
"name" : "_id_",
"ns" : "mydb.users"
},
{
"v" : 1,
"key" : {
"score" : -1
},
"name" : "score_-1",
"ns" : "mydb.users"
}
]
4.3. 我想要删除索引
> db.users.dropIndex("score_-1")
{ "nIndexesWas" : 2, "ok" : 1 }
> db.users.getIndexes()
[
{
"v" : 1,
"key" : {
"_id" : 1
},
"name" : "_id_",
"ns" : "mydb.users"
}
]
我想创建一个独特的索引。
> db.users.createIndex({name: 1}, {unique: true})
{
"createdCollectionAutomatically" : false,
"numIndexesBefore" : 1,
"numIndexesAfter" : 2,
"ok" : 1
}
> db.users.getIndexes()
[
{
"v" : 1,
"key" : {
"_id" : 1
},
"name" : "_id_",
"ns" : "mydb.users"
},
{
"v" : 1,
"unique" : true,
"key" : {
"name" : 1
},
"name" : "name_1",
"ns" : "mydb.users"
}
]
> db.users.insert({name: "nakata"})
WriteResult({
"nInserted" : 0,
"writeError" : {
"code" : 11000,
"errmsg" : "E11000 duplicate key error collection: mydb.users index: name_1 dup key: { : \"nakata\" }"
}
})
我想要进行数据库的备份和恢复。
我想备份数据库。
$ mongodump -d mydb
2016-01-27T21:57:21.642+0900 writing mydb.users to
2016-01-27T21:57:21.643+0900 done dumping mydb.users (3 documents)
$ ls
total 0
drwxr-xr-x 3 inaba staff 102 Jan 27 21:57 .
drwxr-xr-x+ 74 inaba staff 2516 Jan 27 21:57 ..
drwxr-xr-x 3 inaba staff 102 Jan 27 21:57 dump
5.2. 我想要恢复数据库。
$ ls
total 0
drwxr-xr-x 3 inaba staff 102 Jan 27 21:57 .
drwxr-xr-x+ 74 inaba staff 2516 Jan 27 21:57 ..
drwxr-xr-x 3 inaba staff 102 Jan 27 21:57 dump
$ mongorestore --drop
2016-01-27T21:58:22.413+0900 using default 'dump' directory
2016-01-27T21:58:22.413+0900 building a list of dbs and collections to restore from dump dir
2016-01-27T21:58:22.414+0900 reading metadata for mydb.users from dump/mydb/users.metadata.json
2016-01-27T21:58:22.414+0900 restoring mydb.users from dump/mydb/users.bson
2016-01-27T21:58:22.480+0900 restoring indexes for collection mydb.users from metadata
2016-01-27T21:58:22.518+0900 finished restoring mydb.users (3 documents)
2016-01-27T21:58:22.518+0900 done