mongodb insertmany和save的相同点和区别

&& &使用insert或save方法想目标集合插入一个文档:
& & db.person.insert({"name":"ryan","age":30});
& & 使用batchInsert方法实现批量插入,它与insert方法非常类似,只是它接受的是一个文档数组作为参数。一次发送数十,数百乃至数千个文档会明显提高插入的速度。
& & db.person.batchInsert([{"name":"ryan","age":30},{"name":"pitaya","age":2}]);
& & 如果在批量插入的过程中有一个文档插入失败,那么在这个文档之前的所有文档都会成功插入到集合中,而这个文档以及之后的所有文档全部插入失败。如果希望batchInsert忽略错误并且继续执行后续插入,可以使用continueOnError选项。shell并不支持这个选项,但所有的驱动程序都支持。&
& & 使用update方法来更新集合中的数据。update有四个参数,前两个参数是必须的。
& & db.person.update({"name":"ryan"},{"$set":{"age":35}},true,true);
& & 第一个参数:查询文档,用于定位需要更新的目标文档。
& & 第二个参数:修改器文档,用于说明要对找到的文档进行哪些修改。
& & 第三个参数:true表示要使用upsert,即如果没有找到符合更新条件的文档,就会以这个条件和更新文档为基础创建一个新的文档。如果找到了匹配的文档,则正常更新。
& & 第四个参数:true表示符合条件的所有文档,都要执行更新。
& & 修改器:
& & $set:用来指定一个字段的值。如果这个字段不存在,则创建它。对于更新而言,对符合更新条件的文档,修改执行的字段,不需要全部覆盖。
& &&db.person.update({"name":"ryan"},{"$set":{"age":35}},true,true);
& & $inc:用来增加已有键的值,或者该键不存在就创建一个。对于投票等有变化数值的场景,这个会非常方便。
& & db.person.update({"name":"ryan"},{"$inc":{"age":2}},true,true);//对符合name等于ryan的文档,age字段加2。
& & $push:向已有数组末尾加入一个元素。
& & db.person.update({"name":"ryan"},{"$set":{"language":["chinese"]}},true,true);//对符合name等于ryan的文档,添加一个language的数组
& & db.person.update({"name":"ryan"},{"$push":{"language":"english"}},true,true);//给数组的末尾添加一个值。
& & $addToSet:避免向数组插入重复的值。
& & db.person.update({"name":"ryan"},{"$addToSet":{"language":"english"}},true,true);
& & $each:与$push和$addToSet结合,一次给数组添加多个值。
& &&db.person.update({"name":"ryan"},{"$push":{"language":{"$each":["Japanese","Portuguese"]}}},true,true);
& &&db.person.update({"name":"ryan"},{"$addToSet":{"language":{"$each":["Japanese","Portuguese"]}}},true,true);
& & $pop:可以从数组的任何一端删除元素。
& &&db.person.update({"name":"ryan"},{"$pop":{"language":1}},true,true);//从数组的末尾删除一个元素
& &&db.person.update({"name":"ryan"},{"$pop":{"language":-1}},true,true);//从数组的头部删除一个元素
& & $pull:删除数组对应的值。全部删除。
& & db.person.update({"name":"ryan"},{"$pull":{"language":"english"}},true,true);
& & 使用remove方法删除集合中的数据。它可以接受一个查询文档作为可选参数。给定这个参数以后,只有符合条件的文档才能被删除。(删除数据是永久性的,不能撤销,也不能恢复)。
& & db.person.remove({"name":"ryan"});//删除person集合中name字段的值等于ryan的所有文档。
& & db.person.remove();//删除person集合中所有的文档。
& & 使用drop方法代替remove方法,可以大幅度提高删除数据的速度。但是这个方法不能指定任何限定条件。而且整个集合都会被删除,包括索引等信息,甚用!!
& & db.person.drop();
& & MongoDB中使用find方法来进行查询。查询就是返回一个集合中文档的子集,子集的范围从0个文档到整个集合。find方法接受两个参数。
& & 第一个参数决定了要返回哪些文档,参数的内容是查询的条件。
& & 第二个参数来指定想要的键(字段)。第二个参数存在的情况:键的值为1代表要显示,为0代表不显示。&_id&默认显示,其他默认不显示。第二个参数不存在的情况:所有字段默认显示。
& & db.person.find({"name":"ryan"},{"name":1});
& & 查询条件:
& & $lt、$lte、$gt、$gte这四个,就是全部的比较操作符(没有$eq这个操作符),分别对应&、&=、&、&=。
& &&db.person.find({"age":{"$lt":10}});
& & 这里提供一段小脚本,插入10万条数据,做之后的测试用。
1 for(var i=0;i&100000;i++){
db.person.insert({"name":"ryan"+i,"age":i});
& & $in、$nin,用来查询一个键的多个值。
& & db.person.find({"age":{"$in":[1,3]}});//查询age等于1或3的文档。
& & db.person.find({"age":{"$nin":[1,3]}});//查询age不等于1或3的文档。
& & $or,用来查询多个键的多个值。可以和$in等配合使用。
& & db.person.find({"$or":[{"name":"ryan2"},{"age":3}]});//查询name等于ryan2 & 或者 & age等于3的文档。
& & $exists,查询的键对应是值是null的,默认会返回null和键不存在的文档。可以通过$exists来判断该键是否存在。
& & db.person.find({"age":{"$in":[null],"$exists":true}});//查询age等于null,并且键是存在的文档。
& & $where,用它可以在查询中执行任意的javascript,这样就能在查询中做(几乎)任何事情。为了安全起见,应该严格限制或者消除"$where"语句的使用。
& & db.person.find({"$where":function(){
& & & & ...;//这里可以是任意的javascript语句。
& & 游标:利用游标可以限制结果的数量,略过部分结果,根据任意键按任意顺序的组合对结果进行各种排序,或者是执行其他的一些强大的操作。
1 var cursor = db.person.find();
2 while(cursor.hasNext()){
obj = cursor.next();
...;//这里可以做任何事情。
& & 常用的shell:
& & limit:只返回前面多少个结果。
& & db.person.find().limit(2);//查询符合条件的文档,显示前两个文档。
& & skip:跳过多少个结果后显示剩余的。
& & db.person.find().skip(2);//查询符合条件的文档,显示跳过2个文档后剩余的所有文档。&
& & sort:用于排序。接受一个对象(一组键值对)作为参数,键对应文档的键名,值代表排序的方向。排序的方向可以是1(升序)或者-1(降序)。如果指定了多个键,则按照这些键被指定的顺序逐个排序。
& & db.person.find().sort({"name":1,"age":-1});//查询的结果,按照name升序,age降序来排序显示。
阅读(...) 评论()mongodb insert&save - 简书
mongodb insert&save
拥有两种方法一旦数据中包含_id,insert不插入相同的值,save则更新数据
& db.student.insert({"_id": 1, "name":"zhangsan", "age": 28})
WriteResult({ "nInserted" : 1 })
& db.student.find()
{ "_id" : 1, "name" : "zhangsan", "age" : 28 }
& db.student.insert({"_id": 1, "name":"zhangsan", "age": 27})
WriteResult({
"nInserted" : 0,
"writeError" : {
"code" : 11000,
"errmsg" : "E11000 duplicate key error collection: zyhdb.student index: _id_ dup key: { : 1.0 }"
& db.student.find()
{ "_id" : 1, "name" : "zhangsan", "age" : 28 }
& db.student.save({"_id": 1, "name":"zhangsan", "age": 27})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
& db.student.find()
{ "_id" : 1, "name" : "zhangsan", "age" : 27 }
& db.student.insert([{"_id": 2, "name": "lisi"},{"_id": 3, "name": "wangwu"}, {"_id": 4, "name": "zhaoliu", "age": 28}])
BulkWriteResult({
"writeErrors" : [ ],
"writeConcernErrors" : [ ],
"nInserted" : 3,
"nUpserted" : 0,
"nMatched" : 0,
"nModified" : 0,
"nRemoved" : 0,
"upserted" : [ ]
& db.student.find()
{ "_id" : 1, "name" : "zhangsan", "age" : 27 }
{ "_id" : 2, "name" : "lisi" }
{ "_id" : 3, "name" : "wangwu" }
{ "_id" : 4, "name" : "zhaoliu", "age" : 28 }
& for(var i=0; i&10; i++){db.fortest.insert({num: i})}
WriteResult({ "nInserted" : 1 })
& db.fortest.find()
{ "_id" : ObjectId("cea1d9aeabab5"), "num" : 0 }
{ "_id" : ObjectId("cea1d9aeabab6"), "num" : 1 }
{ "_id" : ObjectId("cea1d9aeabab7"), "num" : 2 }
{ "_id" : ObjectId("cea1d9aeabab8"), "num" : 3 }
{ "_id" : ObjectId("cea1d9aeabab9"), "num" : 4 }
{ "_id" : ObjectId("cea1d9aeababa"), "num" : 5 }
{ "_id" : ObjectId("cea1d9aeababb"), "num" : 6 }
{ "_id" : ObjectId("cea1d9aeababc"), "num" : 7 }
{ "_id" : ObjectId("cea1d9aeababd"), "num" : 8 }
{ "_id" : ObjectId("cea1d9aeababe"), "num" : 9 }MongoDB:插入记录(&Insert&)
原文:http://francs3./blog/static/1/
相比关系型数据库的 INSERT 操作, MongoDB 的插入操作虽然别扭,但依然简单,
以下练习了 MongoDB 的插入操作。
插入方式一:
--1.1 插入数据
&& x={id:1};x={id:1};
{ "id" : 1 }
& y={id:2};y={id:2};
{ "id" : 2 }
& db.test_1.save(x);&
db.test_1.save(y);
& db.test_1.find();
{ "_id" : ObjectId("50a0d35ceb825d827b0c3f99"), "id" : 1 }
{ "_id" : ObjectId("50a0d375eb825d827b0c3f9a"), "id" : 2 }
& 备注:这里通过先创建对像 x,y ,然后将它们保存在 test_1 中。
--1.2 显示
collections
system.indexes
& 备注:test_1 已自动创建,这也说明 MongoDB 不需要预先定义
collections ,在第一次插入数据
&后,collections 会自动的创建。
二. 插入方式二:
-- 2.1 插入数据
&& db.test_2.save({id:1,name:'francs'}
& db.test_2.save({id:2,name:'fpZhou'} );
& db.test_2.save({id:3,name:'tutu'} );
& db.test_2.save({id:4,name:'am',address:'zhoushan'} );
--2.2 查询数据
&& db.test_2.find();
{ "_id" : ObjectId("50a0d46ceb825d827b0c3f9b"), "id" : 1, "name" :
"francs" }
{ "_id" : ObjectId("50a0d46ceb825d827b0c3f9c"), "id" : 2, "name" :
"fpZhou" }
{ "_id" : ObjectId("50a0d46ceb825d827b0c3f9d"), "id" : 3, "name" :
{ "_id" : ObjectId("50a0d46deb825d827b0c3f9e"), "id" : 4, "name" :
"am", "address" : "zhoushan"
相比方法一,插入数据时没有预先通过创建对像x,y; 而且不同的 documents 可以有
&&&&&&&&&&&
&不同的字段(如第四条 documents 多了 "address"
字段)。&&&&
循环插入数据&&&&&&&&
&& for (var i = 1; i &= 30; i++)
db.things.save({id : i, name : 'aaa'});
--2.4 查询 things
&& db.things.find();
{ "_id" : ObjectId("50a0d637eb825d827b0c3f9f"), "id" : 1, "name" :
{ "_id" : ObjectId("50a0d637eb825d827b0c3fa0"), "id" : 2, "name" :
{ "_id" : ObjectId("50a0d637eb825d827b0c3fa1"), "id" : 3, "name" :
{ "_id" : ObjectId("50a0d637eb825d827b0c3fa2"), "id" : 4, "name" :
{ "_id" : ObjectId("50a0d637eb825d827b0c3fa3"), "id" : 5, "name" :
{ "_id" : ObjectId("50a0d637eb825d827b0c3fa4"), "id" : 6, "name" :
{ "_id" : ObjectId("50a0d637eb825d827b0c3fa5"), "id" : 7, "name" :
{ "_id" : ObjectId("50a0d637eb825d827b0c3fa6"), "id" : 8, "name" :
{ "_id" : ObjectId("50a0d637eb825d827b0c3fa7"), "id" : 9, "name" :
{ "_id" : ObjectId("50a0d637eb825d827b0c3fa8"), "id" : 10, "name" :
{ "_id" : ObjectId("50a0d637eb825d827b0c3fa9"), "id" : 11, "name" :
{ "_id" : ObjectId("50a0d637eb825d827b0c3faa"), "id" : 12, "name" :
{ "_id" : ObjectId("50a0d637eb825d827b0c3fab"), "id" : 13, "name" :
{ "_id" : ObjectId("50a0d637eb825d827b0c3fac"), "id" : 14, "name" :
{ "_id" : ObjectId("50a0d637eb825d827b0c3fad"), "id" : 15, "name" :
{ "_id" : ObjectId("50a0d637eb825d827b0c3fae"), "id" : 16, "name" :
{ "_id" : ObjectId("50a0d637eb825d827b0c3faf"), "id" : 17, "name" :
{ "_id" : ObjectId("50a0d637eb825d827b0c3fb0"), "id" : 18, "name" :
{ "_id" : ObjectId("50a0d637eb825d827b0c3fb1"), "id" : 19, "name" :
{ "_id" : ObjectId("50a0d637eb825d827b0c3fb2"), "id" : 20, "name" :
Type "it" for
{ "_id" : ObjectId("50a0d637eb825d827b0c3fb3"), "id" : 21, "name" :
{ "_id" : ObjectId("50a0d637eb825d827b0c3fb4"), "id" : 22, "name" :
{ "_id" : ObjectId("50a0d637eb825d827b0c3fb5"), "id" : 23, "name" :
{ "_id" : ObjectId("50a0d637eb825d827b0c3fb6"), "id" : 24, "name" :
{ "_id" : ObjectId("50a0d637eb825d827b0c3fb7"), "id" : 25, "name" :
{ "_id" : ObjectId("50a0d637eb825d827b0c3fb8"), "id" : 26, "name" :
{ "_id" : ObjectId("50a0d637eb825d827b0c3fb9"), "id" : 27, "name" :
{ "_id" : ObjectId("50a0d637eb825d827b0c3fba"), "id" : 28, "name" :
{ "_id" : ObjectId("50a0d637eb825d827b0c3fbb"), "id" : 29, "name" :
{ "_id" : ObjectId("50a0d637eb825d827b0c3fbc"), "id" : 30, "name" :
& 备注:默认只显示 20 条记录, 如需浏览更多记录,键入"it" 命令就可以了。
通过以上练习,有以下几点需要总结:
& 1 MongoDB 不需要预先定义 collections,在数据第一次插入时
collections 被自动创建。
& 2 在一个 collections 中不同的 documents 可以有不同的字段。
& 3 在 collections 中的每个 documents 在插入时会分配一个不同的
ObjectId,字段为 "_id"。
以上网友发言只代表其个人观点,不代表新浪网的观点或立场。MongoDB的save和update速度那个快? - 知乎5被浏览821分享邀请回答0添加评论分享收藏感谢收起03 条评论分享收藏感谢收起

我要回帖

更多关于 mongodb insertmany 的文章

 

随机推荐