The difference between the save and insert functions of MongoDB

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.