刚开始学 mongodb, 只知道几个命令, insert 插入也只能一条一条插入, 而在实际情况下数据一般都非常多, 刚开始想直接上传 json 文件, 网上搜了 n 多方法发现这种方法不好弄, 然后就想着要么一下子把多条数据插进去不就好了
insertMany 方法可以插入多条数据, 但写法也是有要求的, 在 shell 中这样写插入
- try {
- db.products.insertMany( [{ item: "card", qty: 15},
- { item: "envelope", qty: 20 },
- { item: "stamps" , qty: 30 }
- ] );
- }
- catch (e) {
- print (e);
- }
最终插入成功
来源: http://www.bubuko.com/infodetail-2544234.html