1. db.first.insert({ })
插入数据,但是不能批量
2. shell下mongo是不支持批量插入的
想要批量操作,可以使用mongo的应用驱动或是shell的for循环
3. db.first.save({_id:1,name:"first"})
如果集合中有 _id为1 的数据,则更新这条数据。如果没有 执行insert操作
insert的情况下, 有_id为1 的数据,则报错
4. db.first.updat({name:"first"},{name:"two"},true)
如果查询器查找不到,则插入,查找到,则更新
5. db.first.update({name:"first"},
{$set:{name:"two",age:"17"}},false,true)
批量更新
6.db.first.update({age:12},
{$inc:{age:x,name:"add"}},false,true)
age+x x为数值 批量操作 , 如果 没有name的key 则添加。
7.db.first.update({age:12},{$unset:{age:12,name:"add"}},false,true)
查找到age为12的数据,删除 age 和name的key和value, 批量
8. db.first.update({age:12},{$push:{books:"one"}})
查找到 age为12的数据,向books的数组中添加 one 如果没有books数组,
则新建books数组
9. db.first.update({age:12},{$pushAll:{books:["two","threee"]}})
查找到 age为12 的数据,向books的数组中添加 two和three,如果没有,新建
10. db.first.update({age:12},{$addToSet:{books:"two"}})
查找到 age 为 12 的数据,books数组中如果有 “two” 则不插入,如果没有,插入
类似于 JAVA 中的 SET集合
11. db.first.update({age:12},{$pop:{books:1}})
删除 books数组的最后一个值 正数 删除第一个值, 负数删除组后一个
12. db.first.update({name:"x"},{$pull:{books:"one"}})
删除 数组中的 "one"
13. db.first.update({name:"x"},{$pullAll:{books:"one","two"]}})
删除数组中的 one 和two
插入数据,但是不能批量
2. shell下mongo是不支持批量插入的
想要批量操作,可以使用mongo的应用驱动或是shell的for循环
3. db.first.save({_id:1,name:"first"})
如果集合中有 _id为1 的数据,则更新这条数据。如果没有 执行insert操作
insert的情况下, 有_id为1 的数据,则报错
4. db.first.updat({name:"first"},{name:"two"},true)
如果查询器查找不到,则插入,查找到,则更新
5. db.first.update({name:"first"},
{$set:{name:"two",age:"17"}},false,true)
批量更新
6.db.first.update({age:12},
{$inc:{age:x,name:"add"}},false,true)
age+x x为数值 批量操作 , 如果 没有name的key 则添加。
7.db.first.update({age:12},{$unset:{age:12,name:"add"}},false,true)
查找到age为12的数据,删除 age 和name的key和value, 批量
8. db.first.update({age:12},{$push:{books:"one"}})
查找到 age为12的数据,向books的数组中添加 one 如果没有books数组,
则新建books数组
9. db.first.update({age:12},{$pushAll:{books:["two","threee"]}})
查找到 age为12 的数据,向books的数组中添加 two和three,如果没有,新建
10. db.first.update({age:12},{$addToSet:{books:"two"}})
查找到 age 为 12 的数据,books数组中如果有 “two” 则不插入,如果没有,插入
类似于 JAVA 中的 SET集合
11. db.first.update({age:12},{$pop:{books:1}})
删除 books数组的最后一个值 正数 删除第一个值, 负数删除组后一个
12. db.first.update({name:"x"},{$pull:{books:"one"}})
删除 数组中的 "one"
13. db.first.update({name:"x"},{$pullAll:{books:"one","two"]}})
删除数组中的 one 和two