Both the Save and insert functions of MONGODB can insert data into collection, but there are two differences between the two:
First, using the Save function, if the original object does not exist, then they can be inserted into the collection data, if it already exists, save will call the update updates inside the record, and insert will ignore the operation
Insertcan be inserted into a list at a time, without traversing, high efficiency, save will need to traverse the list, insert.
Look at the two functions of the prototype is clear, directly enter the function name can see the prototype, the following is the red part of the implementation of the loop, for the remote call, is a sex of the entire list post to let MongoDB go to their own processing, efficiency will be higher
>Db.user.insert
function (obj, _allow_dot) {
if (!obj) {
throw "No object passed to insert!";
}
if (!_allow_dot) {
this._validateforstorage (obj);
}
if (typeof obj._id = = "undefined" &&! Array.isarray (obj)) {
var tmp = obj;
obj = {_id:new ObjectId};
for (var key in tmp) {
Obj[key] = Tmp[key];
}
}
this._db._initextrainfo ();
This._mongo.insert (this._fullname, obj);
This._lastid = obj._id;
this._db._getextrainfo ("Inserted");
}
> db.user.save
function (obj) {
if (obj = = NULL | | typeof obj = = "undefined") {
throw "can ' t save a null";
}
if (typeof obj = = "number" | | typeof obj = = "string") {
throw "can ' t save a number or string";
}
if (typeof obj._id = = "undefined") {
obj._id = new ObjectId;
return This.insert (obj);
} else {
return this.update ({_id:obj._id}, obj, true);
}
}
Here is the code in Python that implements inserting data into MONGO
Import Pymong
Logitems =[]
Logitems.append ({"url": http://ww1.site.com/"," Time ": 0.2})
Logitems.append ({"url": http://ww2.site.com/"," Time ": 0.12})
Logitems.append ({"url": http://ww3.site.com/"," Time ": 0.24})
def Addlogtomongo (Db,logitems):
#建立一个到mongo数据库的连接
Con = Pymongo. Mongoclient (db,27017)
#连接到指定数据库
db = Con.my_collection
#直接插入数据, Logitems is a list variable that can be inserted directly into MongoDB at once using INSERT, and if you use save, you need to loop through the insert with a for, not high efficiency
Db.logDetail.insert (Logitems)
‘‘‘
For URL in Logitems:
Print (str (URL))
Db.logDetail.save (URL)
‘‘‘
The difference between the save and insert functions of MongoDB