1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
| // 多个文档的插入(insertMany方法,传入的是一个list) db.comment.insertMany([ { "_id":1, "articleid":"10001", "content":"今天天气真好,阳光明媚", "userid":"1001", "nickname":"Rose", "createdatetime":new Date(), "likenum":NumberInt(10), "state":null, "flag":1 }, { "_id":2, "articleid":"10002", "content":"今天天气真好,阳光明媚", "userid":"1001", "nickname":"Rose", "createdatetime":new Date(), "likenum":NumberInt(10), "state":null, "flag":1 }, { "_id":3, "articleid":"10003", "content":"今天天气真好,阳光明媚", "userid":"1001", "nickname":"Rose", "createdatetime":new Date(), "likenum":NumberInt(10), "state":null, "flag":1 } ])
// 批量插入中如何中间某一条出现错误,已经插入的数据不会进行回滚,所以在插入的时候可以采用这种写法 try{ db.comment.insertMany([
{ "_id":1, "articleid":"10001", "content":"今天天气真好,阳光明媚", "userid":"1001", "nickname":"Rose", "createdatetime":new Date(), "likenum":NumberInt(10), "state":null, "flag":1 }, { "_id":2, "articleid":"10002", "content":"今天天气真好,阳光明媚", "userid":"1001", "nickname":"Rose", "createdatetime":new Date(), "likenum":NumberInt(10), "state":null, "flag":1 }, { "_id":3, "articleid":"10003", "content":"今天天气真好,阳光明媚", "userid":"1001", "nickname":"Rose", "createdatetime":new Date(), "likenum":NumberInt(10), "state":null, "flag":1 } ]) }catch(e){ print(e) }
|