因为mongodb基于javascript的特性,为了体验cursor的威力我们不妨利用js的for循环创建记录
先创建文档
db.createCollection("columnsample")
在文档中插入数据
for(var i = 1;i<10001;i++)
{
db.columnsample.insert(
{
_id:i,
name:"陈培昌"+i,
content: "陈培昌打了沙袋"+i+"拳",
respomse:"嗷儿喂~"
}
)
}
这里介绍一种批量插入数据的办法,特此鸣谢燕十八的PHP开发课程mongodb章节,这里的数据范例来自相关课件
其格式为db.collection.insert([{记录1},{记录2},{记录3}])
db.goods.insert(
[
{"goods_id":13,"cat_id":3,"goods_name":"\u8bfa\u57fa\u4e9a5320 XpressMusic","goods_number":8,"click_count":13,"shop_price":1311.00,"add_time":1241967762},
{"goods_id":14,"cat_id":4,"goods_name":"\u8bfa\u57fa\u4e9a5800XM","goods_number":1,"click_count":6,"shop_price":2625.00,"add_time":1241968492},
{"goods_id":15,"cat_id":3,"goods_name":"\u6469\u6258\u7f57\u62c9A810","goods_number":3,"click_count":8,"shop_price":788.00,"add_time":1241968703},
{"goods_id":16,"cat_id":2,"goods_name":"\u6052\u57fa\u4f1f\u4e1aG101","goods_number":0,"click_count":3,"shop_price":823.33,"add_time":1241968949},
{"goods_id":17,"cat_id":3,"goods_name":"\u590f\u65b0N7","goods_number":1,"click_count":2,"shop_price":2300.00,"add_time":1241969394},
{"goods_id":18,"cat_id":4,"goods_name":"\u590f\u65b0T5","goods_number":1,"click_count":0,"shop_price":2878.00,"add_time":1241969533},
{"goods_id":19,"cat_id":3,"goods_name":"\u4e09\u661fSGH-F258","goods_number":12,"click_count":7,"shop_price":858.00,"add_time":1241970139},
{"goods_id":20,"cat_id":3,"goods_name":"\u4e09\u661fBC01","goods_number":12,"click_count":14,"shop_price":280.00,"add_time":1241970417},
{"goods_id":21,"cat_id":3,"goods_name":"\u91d1\u7acb A30","goods_number":40,"click_count":4,"shop_price":2000.00,"add_time":1241970634},
{"goods_id":22,"cat_id":3,"goods_name":"\u591a\u666e\u8fbeTouch HD","goods_number":1,"click_count":15,"shop_price":5999.00,"add_time":1241971076}
]
)
大功告成!.......oh,shit!字段名根本就拼错了,response应该是!
那么,应该如此操作
db.columnsample.updateMany(
{},
{
"$rename":{"remponse":"response"}
}
)