for 循环修改 model.Schema 实体的某个属性值(这个属性是一个索引,不可重复),批量插入数据库
在写demo的时候,我需要使用for循环修改一个model 实体的索引的值,伪造数据批量插入Collection。
var epsonl_1 = new Epsonl({
NAME: '',
CONTENT: false,
NUMBER: 12,
DIRECTION: false,
COLOR: false,
PATH: 'c:\file.docx',
size: 0
});
这里的Epsonl是model Schema,那么epsonl_1就是这个Schema的一个实体对吧?我现在需要修改这个实体的NAME属性的值,我使用for循环修改,代码:
for (let i = 0;i<50;i++){
epsonl_1.NAME = 'test'+i;
console.log(epsonl_1.NAME);
docs.push(epsonl_1);
}
使用一个docs数组保存修改后的实体
var docs = [];
然后,我启动程序,发现数据库只插入了一条文档,并且 他的NAME属性的值为 test49 ,而我在for循环内部使用console.log(epsonl_1.NAME);
查看时,发现
test0
test1
test2
test3
test4
test5
test6
test7
test8
test9
test10
test11
test12
test13
test14
test15
test16
test17
test18
test19
test20
test21
test22
test23
test24
test25
test26
test27
test28
test29
test30
test31
test32
test33
test34
test35
test36
test37
test38
test39
test40
test41
test42
test43
test44
test45
test46
test47
test48
test49
于是我又输出了数组docs的值:
[ { NAME: 'test49',
CONTENT: false,
NUMBER: 12,
DIRECTION: false,
COLOR: false,
PATH: 'c:\file.docx',
_id: 59758b259117550bb4fdd635 },
{ NAME: 'test49',
CONTENT: false,
NUMBER: 12,
DIRECTION: false,
COLOR: false,
PATH: 'c:\file.docx',
_id: 59758b259117550bb4fdd635 },
{ NAME: 'test49',
CONTENT: false,
NUMBER: 12,
DIRECTION: false,
COLOR: false,
PATH: 'c:\file.docx',
_id: 59758b259117550bb4fdd635 },
{ NAME: 'test49',
CONTENT: false,
NUMBER: 12,
DIRECTION: false,
COLOR: false,
PATH: 'c:\file.docx',
_id: 59758b259117550bb4fdd635 },
{ NAME: 'test49',
CONTENT: false,
NUMBER: 12,
DIRECTION: false,
COLOR: false,
PATH: 'c:\file.docx',
_id: 59758b259117550bb4fdd635 },
{ NAME: 'test49',
CONTENT: false,
NUMBER: 12,
DIRECTION: false,
COLOR: false,
PATH: 'c:\file.docx',
_id: 59758b259117550bb4fdd635 },
{ NAME: 'test49',
CONTENT: false,
NUMBER: 12,
DIRECTION: false,
COLOR: false,
PATH: 'c:\file.docx',
_id: 59758b259117550bb4fdd635 },
{ NAME: 'test49',
CONTENT: false,
NUMBER: 12,
DIRECTION: false,
COLOR: false,
PATH: 'c:\file.docx',
_id: 59758b259117550bb4fdd635 },
{ NAME: 'test49',
CONTENT: false,
NUMBER: 12,
DIRECTION: false,
COLOR: false,
PATH: 'c:\file.docx',
_id: 59758b259117550bb4fdd635 },
{ NAME: 'test49',
CONTENT: false,
NUMBER: 12,
DIRECTION: false,
COLOR: false,
PATH: 'c:\file.docx',
_id: 59758b259117550bb4fdd635 },
{ NAME: 'test49',
CONTENT: false,
NUMBER: 12,
DIRECTION: false,
COLOR: false,
PATH: 'c:\file.docx',
_id: 59758b259117550bb4fdd635 },
{ NAME: 'test49',
CONTENT: false,
NUMBER: 12,
DIRECTION: false,
COLOR: false,
PATH: 'c:\file.docx',
_id: 59758b259117550bb4fdd635 },
{ NAME: 'test49',
CONTENT: false,
NUMBER: 12,
DIRECTION: false,
COLOR: false,
PATH: 'c:\file.docx',
_id: 59758b259117550bb4fdd635 },
{ NAME: 'test49',
CONTENT: false,
NUMBER: 12,
DIRECTION: false,
COLOR: false,
PATH: 'c:\file.docx',
_id: 59758b259117550bb4fdd635 },
{ NAME: 'test49',
CONTENT: false,
NUMBER: 12,
DIRECTION: false,
COLOR: false,
PATH: 'c:\file.docx',
_id: 59758b259117550bb4fdd635 },
{ NAME: 'test49',
CONTENT: false,
NUMBER: 12,
DIRECTION: false,
COLOR: false,
PATH: 'c:\file.docx',
_id: 59758b259117550bb4fdd635 },
{ NAME: 'test49',
CONTENT: false,
NUMBER: 12,
DIRECTION: false,
COLOR: false,
PATH: 'c:\file.docx',
_id: 59758b259117550bb4fdd635 },
{ NAME: 'test49',
CONTENT: false,
NUMBER: 12,
DIRECTION: false,
COLOR: false,
PATH: 'c:\file.docx',
_id: 59758b259117550bb4fdd635 },
{ NAME: 'test49',
CONTENT: false,
NUMBER: 12,
DIRECTION: false,
COLOR: false,
PATH: 'c:\file.docx',
_id: 59758b259117550bb4fdd635 },
{ NAME: 'test49',
CONTENT: false,
NUMBER: 12,
DIRECTION: false,
COLOR: false,
PATH: 'c:\file.docx',
_id: 59758b259117550bb4fdd635 },
{ NAME: 'test49',
CONTENT: false,
NUMBER: 12,
DIRECTION: false,
COLOR: false,
PATH: 'c:\file.docx',
_id: 59758b259117550bb4fdd635 },
{ NAME: 'test49',
CONTENT: false,
NUMBER: 12,
DIRECTION: false,
COLOR: false,
PATH: 'c:\file.docx',
_id: 59758b259117550bb4fdd635 },
{ NAME: 'test49',
CONTENT: false,
NUMBER: 12,
DIRECTION: false,
COLOR: false,
PATH: 'c:\file.docx',
_id: 59758b259117550bb4fdd635 },
{ NAME: 'test49',
CONTENT: false,
NUMBER: 12,
DIRECTION: false,
COLOR: false,
PATH: 'c:\file.docx',
_id: 59758b259117550bb4fdd635 },
{ NAME: 'test49',
CONTENT: false,
NUMBER: 12,
DIRECTION: false,
COLOR: false,
PATH: 'c:\file.docx',
_id: 59758b259117550bb4fdd635 },
{ NAME: 'test49',
CONTENT: false,
NUMBER: 12,
DIRECTION: false,
COLOR: false,
PATH: 'c:\file.docx',
_id: 59758b259117550bb4fdd635 },
{ NAME: 'test49',
CONTENT: false,
NUMBER: 12,
DIRECTION: false,
COLOR: false,
PATH: 'c:\file.docx',
_id: 59758b259117550bb4fdd635 },
{ NAME: 'test49',
CONTENT: false,
NUMBER: 12,
DIRECTION: false,
COLOR: false,
PATH: 'c:\file.docx',
_id: 59758b259117550bb4fdd635 },
{ NAME: 'test49',
CONTENT: false,
NUMBER: 12,
DIRECTION: false,
COLOR: false,
PATH: 'c:\file.docx',
_id: 59758b259117550bb4fdd635 },
{ NAME: 'test49',
CONTENT: false,
NUMBER: 12,
DIRECTION: false,
COLOR: false,
PATH: 'c:\file.docx',
_id: 59758b259117550bb4fdd635 },
{ NAME: 'test49',
CONTENT: false,
NUMBER: 12,
DIRECTION: false,
COLOR: false,
PATH: 'c:\file.docx',
_id: 59758b259117550bb4fdd635 },
{ NAME: 'test49',
CONTENT: false,
NUMBER: 12,
DIRECTION: false,
COLOR: false,
PATH: 'c:\file.docx',
_id: 59758b259117550bb4fdd635 },
{ NAME: 'test49',
CONTENT: false,
NUMBER: 12,
DIRECTION: false,
COLOR: false,
PATH: 'c:\file.docx',
_id: 59758b259117550bb4fdd635 },
{ NAME: 'test49',
CONTENT: false,
NUMBER: 12,
DIRECTION: false,
COLOR: false,
PATH: 'c:\file.docx',
_id: 59758b259117550bb4fdd635 },
{ NAME: 'test49',
CONTENT: false,
NUMBER: 12,
DIRECTION: false,
COLOR: false,
PATH: 'c:\file.docx',
_id: 59758b259117550bb4fdd635 },
{ NAME: 'test49',
CONTENT: false,
NUMBER: 12,
DIRECTION: false,
COLOR: false,
PATH: 'c:\file.docx',
_id: 59758b259117550bb4fdd635 },
{ NAME: 'test49',
CONTENT: false,
NUMBER: 12,
DIRECTION: false,
COLOR: false,
PATH: 'c:\file.docx',
_id: 59758b259117550bb4fdd635 },
{ NAME: 'test49',
CONTENT: false,
NUMBER: 12,
DIRECTION: false,
COLOR: false,
PATH: 'c:\file.docx',
_id: 59758b259117550bb4fdd635 },
{ NAME: 'test49',
CONTENT: false,
NUMBER: 12,
DIRECTION: false,
COLOR: false,
PATH: 'c:\file.docx',
_id: 59758b259117550bb4fdd635 },
{ NAME: 'test49',
CONTENT: false,
NUMBER: 12,
DIRECTION: false,
COLOR: false,
PATH: 'c:\file.docx',
_id: 59758b259117550bb4fdd635 },
{ NAME: 'test49',
CONTENT: false,
NUMBER: 12,
DIRECTION: false,
COLOR: false,
PATH: 'c:\file.docx',
_id: 59758b259117550bb4fdd635 },
{ NAME: 'test49',
CONTENT: false,
NUMBER: 12,
DIRECTION: false,
COLOR: false,
PATH: 'c:\file.docx',
_id: 59758b259117550bb4fdd635 },
{ NAME: 'test49',
CONTENT: false,
NUMBER: 12,
DIRECTION: false,
COLOR: false,
PATH: 'c:\file.docx',
_id: 59758b259117550bb4fdd635 },
{ NAME: 'test49',
CONTENT: false,
NUMBER: 12,
DIRECTION: false,
COLOR: false,
PATH: 'c:\file.docx',
_id: 59758b259117550bb4fdd635 },
{ NAME: 'test49',
CONTENT: false,
NUMBER: 12,
DIRECTION: false,
COLOR: false,
PATH: 'c:\file.docx',
_id: 59758b259117550bb4fdd635 },
{ NAME: 'test49',
CONTENT: false,
NUMBER: 12,
DIRECTION: false,
COLOR: false,
PATH: 'c:\file.docx',
_id: 59758b259117550bb4fdd635 },
{ NAME: 'test49',
CONTENT: false,
NUMBER: 12,
DIRECTION: false,
COLOR: false,
PATH: 'c:\file.docx',
_id: 59758b259117550bb4fdd635 },
{ NAME: 'test49',
CONTENT: false,
NUMBER: 12,
DIRECTION: false,
COLOR: false,
PATH: 'c:\file.docx',
_id: 59758b259117550bb4fdd635 },
{ NAME: 'test49',
CONTENT: false,
NUMBER: 12,
DIRECTION: false,
COLOR: false,
PATH: 'c:\file.docx',
_id: 59758b259117550bb4fdd635 },
{ NAME: 'test49',
CONTENT: false,
NUMBER: 12,
DIRECTION: false,
COLOR: false,
PATH: 'c:\file.docx',
_id: 59758b259117550bb4fdd635 } ]
...
NAME属性的值全部都是test49,这样我就晕了。请问各位大佬怎么解决这个问题,顺便麻烦解释一下其中的原因。
插入到集合只有一个文档,这我理解,NAME属性的值不可重复。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不仅是NAME全一样,你整个push了同一个对象50遍啊50遍。
push的对象是一个引用啊,你改了这个对象NAME然后push,改了又push,最后不是50个一样才怪了呢。
复制一遍就好了啦。
啊我去,删了刚写的示例代码。特么装逼装错了。
类似这样,复制一个新数组。
mongoose的API我不是特别熟悉,不过你的问题应该每次给新的_id就可以了,试下: