Func (this *mongodb) storerecords (dbname, collection, Storedbname, tmpcollection string, QueryMap, Selecter Bson. M) (Err Error) {session: = this. GetSession () Defer session. Close () ITER: = Session. DB (dbname). C (collection). Find (QueryMap). Select (Selecter). Iter () Onemap: = Make (Bson. M) Historymapslice: = Make ([]interface{}, 0, Max_bulk_write_file_len) flag: = False for ITER. Next (&onemap) {tmpmap: = Deepcopy (onemap)//go deep copy historymapslice = append (Historymapslice, Tmpmap. ( Bson. M) If Len (historymapslice) >= Max_bulk_write_file_len {//write maximum length this. Bulkinsert (Storedbname, Tmpcollection, historymapslice) Historymapslice = make ([]interface{}, 0, MAX_BULK_WRITE_FILE_ LEN) flag = True}} If Err = iter. Close (); Err!= Nil {log. Error ("Storerecords,iter.")
Close () fail, DB: ", dbname,", Collection: ", Collection,", Querycondition: ", QueryMap,", selector: ", Selecter, ", Errinfo:", err)} if 0 = Len (historymapslice) &&!flag {err = errors. NEW ("Not Found Storerecords data") return} this. Bulkinsert (Storedbname, Tmpcollection, Historymapslice) return} func (this *mongodb) Bulkinsert (DataBase, Collection St Ring, Allrecords []interface{}] (err error) {if 0 = = Len (allrecords) {log. Info ("bulkinsert:0 = Len (allrecords)!" DB: ", DataBase," collection: ", collection) return} Bulkinsert: = Func (c *mgo. Collection) Error {bulk: = C.bulk () bulk. Unordered () bulk.
Insert (Allrecords ...) _, Err: = bulk. Run () return err} err = this. Witchcollection (DataBase, collection, Bulkinsert) If Err!= nil {log. Error ("MongoDB Bulkinsert, Bulkinsert Failed, DB:", DataBase, ", Collection:", Collection, ", Erri NFO: ", Err)} return}