for 循环修改 model.Schema 实体的某个属性值(这个属性是一个索引,不可重复),批量插入数据库

发布于 2022-09-05 06:29:52 字数 9195 浏览 12 评论 0

在写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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

倾城°AllureLove 2022-09-12 06:29:52

不仅是NAME全一样,你整个push了同一个对象50遍啊50遍。

push的对象是一个引用啊,你改了这个对象NAME然后push,改了又push,最后不是50个一样才怪了呢。

复制一遍就好了啦。


啊我去,删了刚写的示例代码。特么装逼装错了。


类似这样,复制一个新数组。

for (let i = 0; i < 50; i++){
    
    let newArr = Object.assign({}, epsonl_1);
    newArr.NAME = 'test' + i;
    docs.push(newArr);
}
一张白纸 2022-09-12 06:29:52

mongoose的API我不是特别熟悉,不过你的问题应该每次给新的_id就可以了,试下:

for (let i = 0;i<50;i++){
    epsonl_1._id = new ObjectId();    // 替换_id
    epsonl_1.NAME = 'test'+i;
    console.log(epsonl_1.NAME);
    docs.push(epsonl_1);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文