向mongodb里插入10万条记录的代码怎么写
郑州海宁皮革城
2024-06-28 09:13:20
最佳回答
起初打算用node.js去做这个性能 测试,碰了几次壁后,改用python顺利完成任务,现在回过头来再看这个问题,发现里面似乎有坑。先放上一段node.js的插入10万条记录的代码:var mongoclient = require('mongodb').mongoclient;var url = 'mongodb://127.0.0.1:27017/testdb';var datal**t = []var globalcount = 0console.time('insert,10w');mongoclient.connect(url, {server: {poolsize: 50}},function(err, db) { if(err) throw(err) for(var i=0; i<100000; i++){ db.collection('test1').insert({ count:i }, function(err, r) {if(err) throw(err)globalcount++if(globalcount>=100000){console.timeend('insert,10w');process.exit(1);} }); } });测试结果慢的令人发指,node.js版本是最新的0.12.2e:\nodejs>node mongodb_error2.jsinsert,10w: 9791ms我用同样的python脚本去做:import pymongoimport jsonimport datetime,timeimport copyimport sys, osdef gettimestampfromdatetime(d=none):if d ** none:d = datetime.datetime.now()return time.mktime(d.timetuple())if __name__ == '__main__':start = gettimestampfromdatetime()client = pymongo.mongoclient("localhost", 27017, max_pool_size=50)db = client.testdbs**edata = []for i in range(0, 100000):s**edata.append({'count':i})db.test2.insert(s**edata)end = gettimestampfromdatetime()print('time: {0}s'.format(end-start))测试结果如下:e:\python>python s**e_10w.pytime: 1.0我在想是不是node.js代码写的太垃圾,于是我找了mongodb包的另外一个insertmany方法,但是结果却报错了:e:\nodejs\node_modules\mongodb\lib\utils.js:97process.nexttick(function() { throw err; });^errorat object.<anonymous> (e:\nodejs\node_modules\mongodb\node_modules\mongodb-core\lib\error.js:42:24)at module._compile (module.js:460:26)at object.module._extensions..js (module.js:478:10)at module.load (module.js:355:32)at function.module._load (module.js:310:12)at module.require (module.js:365:17)at require (module.js:384:17)at object.<anonymous> (e:\nodejs\node_modules\mongodb\node_modules\mongodb-core\index.js:2:17)at module._compile (module.js:460:26)at object.module._extensions..js (module.js:478:10) 20210311