Mongo创建、修改、删除详讲
1.创建
方法名称 | 描述 |
---|---|
insertOne | 插入一个文档 |
insertMany | 插入一个文档数组 |
insert | 可以插入一个文档也可以插入一个数组 |
db.Books.insert({"name":"战争与和平"});
db.Books.insert([{"name":"斗罗大陆"},{"name":"斗罗大陆·绝世唐门"}]);
db.Books.insertOne({"name":"星辰变"});
db.Books.insertMany([{"name":"莽荒纪"},{"name":"兽血沸腾"}]);
db.Books.find();
db.Books.remove({});
2.删除
方法名称 | 描述 |
---|---|
deleteOne | 只删除一个匹配的文档 |
deleteMany | 删除所有匹配的文档 |
remove | 可以删除一个匹配文档也可以删除一组匹配的文档 |
db.Books.insert([
{"name":"斗罗大陆"},
{"name":"斗罗大陆·绝世唐门"},
{"name":"斗破苍穹"}]);
db.Books.find();
db.Books.deleteOne({"name":/斗/});
db.Books.find();
db.Books.deleteMany({"name":/斗/});
db.Books.find();
db.Books.insert([
{"name":"九星毒奶"},
{"name":"神元星"},
{"name":"星之誓约者"}]);
db.Books.find();
db.Books.remove({"name":/星/},{justOne:true});
db.Books.find();
db.Books.remove({"name":/星/})
db.Books.find();
3.修改
方法名称 | 描述 |
---|---|
updateOne | 只更新一个匹配到的文档 |
updateMany | 更新一组匹配到的文档 |
replaceOne | 只替换一个匹配到的文档 |
update | 更新或替换匹配到的单个文档或者多个文档 |
updateOne
db.Books.insert([
{"name":"斗罗大陆"},
{"name":"斗罗大陆·绝世唐门"},
{"name":"斗破苍穹"}]);
db.Books.updateOne({"name":/斗/},{$set:{"name":"校花的贴身保镖","price":99.8}});
db.Books.find();
db.Books.remove({});
updateMany
db.Books.insert([
{"name":"斗罗大陆"},
{"name":"斗罗大陆·绝世唐门"},
{"name":"斗破苍穹"}]);
db.Books.updateMany({"name":/斗/},{$set:{"name":"校花的贴身保镖","price":99.8}});
db.Books.find();
db.Books.remove({});
replaceOne
db.Books.insert([
{"name":"斗罗大陆"},
{"name":"斗罗大陆·绝世唐门"},
{"name":"斗破苍穹"}]);
db.Books.replaceOne({"name":/斗/},{"name":"校花的贴身保镖","price":99.8});
db.Books.find();
db.Books.remove({});
update
默认只更新一个文档,如果需要更新多个则需要设置multi=true
db.Books.insert([
{"name":"斗罗大陆"},
{"name":"斗罗大陆·绝世唐门"},
{"name":"斗破苍穹"}]);
db.Books.update({"name":/斗/},{$set:{"name":"校花的贴身保镖","price":99.8}});
db.Books.find();
db.Books.update({"name":/斗/},{$set:{"name":"极品家丁","price":99.8}},{multi:true});
db.Books.find();
db.Books.remove({});
4.修改方式详讲
名称 | 描述 |
---|---|
$currentDate | 设置字段的值为当前的时间,类型可以是日期和时间戳 |
$inc | 指定一个值递增一个字段 |
$min | 如果文档内的字段比$min设置的字段大,把$min设置的字段更新到文档去,如果没有这个字段,则会新建这个字段并且设置成这个值 |
$max | 如果文档内的字段比$max设置的字段小,把$max设置的字段更新到文档去如果没有这个字段,则会新建这个字段并且设置成这个值 |
$mul | 指定一个值乘以一个字段 |
$rename | 重命名一个字段 |
$set | 更新一个字段到文档里面,如果这个字段不在,则新建 |
$setOnInsert | 更新一个字段到文档里面,如果这个字段不在,则新建,但必须update设置了upsert:true之后这个项才会运行 |
$unset | 删除字段 |
$currentDate
db.Books.insert([
{"name":"斗罗大陆","lastReadDate":Date(),"lastReadTimeSpan":123},
{"name":"斗罗大陆·绝世唐门","lastReadDate":Date(),"lastReadTimeSpan":123},
{"name":"斗破苍穹","lastReadDate":Date(),"lastReadTimeSpan":123}]);
db.Books.update({"name":"斗罗大陆"},{$currentDate:{"lastReadDate":true,"lastReadTimeSpan":{$type:"timestamp"}}});
db.Books.update({"name":"斗罗大陆·绝世唐门"},{$currentDate:{"lastReadDate":true,"lastReadTimeSpan":{$type:"date"}}});
db.Books.find();
db.Books.remove({});
$inc
db.Books.insert([
{"name":"斗罗大陆","recordCount":200},
{"name":"斗罗大陆·绝世唐门","recordCount":1},
{"name":"斗破苍穹","recordCount":1}]);
db.Books.update({"name":"斗罗大陆"},{$inc:{"recordCount":1}});
db.Books.find();
db.Books.update({"name":"斗罗大陆"},{$inc:{"recordCount":10}});
db.Books.find();
db.Books.remove({});
$min
db.Books.insert([
{"name":"斗罗大陆","price":200},
{"name":"斗罗大陆·绝世唐门","price":200},
{"name":"斗破苍穹","price":200}]);
db.Books.update({"name":"斗罗大陆"},{$min:{"price":99.8}});
db.Books.find();
db.Books.update({"name":"斗罗大陆"},{$min:{"price":99.8}});
db.Books.find();
db.Books.remove({});
$max
db.Books.insert([
{"name":"斗罗大陆","price":99.8},
{"name":"斗罗大陆·绝世唐门","price":99.8},
{"name":"斗破苍穹","price":99.8}]);
db.Books.update({"name":"斗罗大陆"},{$max:{"price":200}});
db.Books.find();
db.Books.update({"name":"斗罗大陆"},{$max:{"price":99.8}});
db.Books.find();
db.Books.remove({});
$mul
db.Books.insert([
{"name":"斗罗大陆","price":99.8},
{"name":"斗罗大陆·绝世唐门","price":99.8},
{"name":"斗破苍穹","price":99.8}]);
db.Books.update({"name":"斗罗大陆"},{$mul:{"price":0.98}});
db.Books.find();
db.Books.remove({});
$rename
db.Books.insert([
{"nmae":"斗罗大陆","price":99.8},
{"nmae":"斗罗大陆·绝世唐门","price":99.8},
{"nmae":"斗破苍穹","price":99.8}]);
db.Books.updateMany({},{$rename:{"nmae":"name"}});
db.Books.find();
db.Books.remove({});
$set
db.Books.insert([
{"name":"斗罗大陆"},
{"name":"斗罗大陆·绝世唐门"},
{"name":"斗破苍穹"}]);
db.Books.updateMany({"name":"斗罗大陆"},{$set:{"price":99.8}});
db.Books.updateMany({"name":"斗罗大陆"},{$set:{"recordCount":1}});
db.Books.find();
db.Books.remove({});
$setOnInsert
db.Books.insert([
{"name":"斗罗大陆"}]);
db.Books.update({"name":"斗罗大陆"},{$setOnInsert:{"price":99.8}});
db.Books.find();
db.Books.update({"name":"斗罗大陆·绝世唐门"},{$setOnInsert:{"price":99.8}},{upsert:true});
db.Books.find();
db.Books.remove({});
$unset
db.Books.insert([
{"name":"草稿"}]);
db.Books.update({"name":"草稿"},{$unset:{"name":""}});
db.Books.find();
db.Books.remove({});