MongoDB批量插入大資料文件的簡單方法一例

chenfeng發表於2016-08-18
插入10萬條記錄舉例:
C:\Users\duansf>mongo
MongoDB shell version: 2.6.6
connecting to: test
> use test
switched to db test
> for(i=0;i<100000;i++){
...  db.chenfeng.insert({name:'chenfeng',age:i})  --備註:db.chenfeng.save命令也可以
... }
WriteResult({ "nInserted" : 1 })


>db.chenfeng.count()  --count可以查集合總共文件數
100000

> db.chenfeng.find()
{ "_id" : ObjectId("57b57b144ed9d65819f02b2e"), "name" : "chenfeng", "age" : 0 }
{ "_id" : ObjectId("57b57b144ed9d65819f02b2f"), "name" : "chenfeng", "age" : 1 }
{ "_id" : ObjectId("57b57b144ed9d65819f02b30"), "name" : "chenfeng", "age" : 2 }
{ "_id" : ObjectId("57b57b144ed9d65819f02b31"), "name" : "chenfeng", "age" : 3 }
{ "_id" : ObjectId("57b57b144ed9d65819f02b32"), "name" : "chenfeng", "age" : 4 }
{ "_id" : ObjectId("57b57b144ed9d65819f02b33"), "name" : "chenfeng", "age" : 5 }
{ "_id" : ObjectId("57b57b144ed9d65819f02b34"), "name" : "chenfeng", "age" : 6 }
{ "_id" : ObjectId("57b57b144ed9d65819f02b35"), "name" : "chenfeng", "age" : 7 }
{ "_id" : ObjectId("57b57b144ed9d65819f02b36"), "name" : "chenfeng", "age" : 8 }
{ "_id" : ObjectId("57b57b144ed9d65819f02b37"), "name" : "chenfeng", "age" : 9 }
{ "_id" : ObjectId("57b57b144ed9d65819f02b38"), "name" : "chenfeng", "age" : 10 }
{ "_id" : ObjectId("57b57b144ed9d65819f02b39"), "name" : "chenfeng", "age" : 11 }
{ "_id" : ObjectId("57b57b144ed9d65819f02b3a"), "name" : "chenfeng", "age" : 12 }
{ "_id" : ObjectId("57b57b144ed9d65819f02b3b"), "name" : "chenfeng", "age" : 13 }
{ "_id" : ObjectId("57b57b144ed9d65819f02b3c"), "name" : "chenfeng", "age" : 14 }
{ "_id" : ObjectId("57b57b144ed9d65819f02b3d"), "name" : "chenfeng", "age" : 15 }
{ "_id" : ObjectId("57b57b144ed9d65819f02b3e"), "name" : "chenfeng", "age" : 16 }
Type "it" for more

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/15498/viewspace-2123682/,如需轉載,請註明出處,否則將追究法律責任。

相關文章