def upsert_multi ( db , collection , object , match_params = None ) : if isinstance ( object , list ) and len ( object ) > 0 : return str ( db [ collection ] . insert_many ( object ) . inserted_ids ) elif isinstance ( object , dict ) : return str ( db [ collection ] . update_many ( match_params , { "$set" : object } , upsert = False ) . upserted_id )
bulk insert into mongodb collection in python
Wrapper for pymongo . insert_many () and update_many () : param db : db connection : param collection : collection to update : param object : the modifications to apply : param match_params : a query that matches the documents to update : return : ids of inserted / updated document
def upsert_multi(db, collection, object, match_params=None):
        """
        Wrapper for pymongo.insert_many() and update_many()
        :param db: db connection
        :param collection: collection to update
        :param object: the modifications to apply
        :param match_params: a query that matches the documents to update
        :return: ids of inserted/updated document
        """
        if isinstance(object, list) and len(object) > 0:
            return str(db[collection].insert_many(object).inserted_ids)
        elif isinstance(object, dict):
            return str(db[collection].update_many(match_params, {"$set": object}, upsert=False).upserted_id)