_.merge / _。assign / _。扩展数组中的所有克隆子文档

时间:2015-12-28 00:18:22

标签: arrays node.js mongodb merge mongoose

我在运行MongoDB和Mongoose的NodeJS应用程序中更新文档数组时遇到问题。

我的问题是当我将新文档与旧文档合并时。两者都有变量content,它包含文档和数组。这些文件都是独一无二的,直到我合并它们,然后它们成为第一个的克隆。我如何防止这种情况,我已尝试使用_.merge,_. extend和_.assign,所有三个都提供相同的结果。

名为page的旧文档:

{ _id: 56806efa7dd9c0c81c39738c,
  page: 'index',
  __v: 7,
  content:
   [ { language: 'dansk',
       content: '<h1>Lunorth Turneringen</h1><p>Test.bob.hi.cherry</p>',
       _id: 56806efa7dd9c0c81c39738f },
     { language: 'english',
       content: '<h1>Lunorth Turneringen</h1><p>Test.bob.hi.uncle</p>',
       _id: 56806efa7dd9c0c81c39738f },
     { language: 'deutsch',
       content: '<h1>Lunorth Turneringen</h1><p>Test.bob.hi.lover</p>',
       _id: 56806efa7dd9c0c81c39738f } ] }

名为req.body的新文件:

{ page: 'index',
  __v: 7,
  content:
   [ { language: 'dansk',
       content: '<h1>Lunorth Turneringen</h1><p>Test.bob.hi.cherry..</p>',
       _id: '56806efa7dd9c0c81c39738f' },
     { language: 'english',
       content: '<h1>Lunorth Turneringen</h1><p>Test.bob.hi.uncle</p>',
       _id: '56806efa7dd9c0c81c39738f' },
     { language: 'deutsch',
       content: '<h1>Lunorth Turneringen</h1><p>Test.bob.hi.lover</p>',
       _id: '56806efa7dd9c0c81c39738f' } ] }

_.merge之后的结果:

{ _id: 56806efa7dd9c0c81c39738c,
  page: 'index',
  __v: 7,
  content:
   [ { language: 'dansk',
       content: '<h1>Lunorth Turneringen</h1><p>Test.bob.hi.cherry..</p>',
       _id: 56806efa7dd9c0c81c39738f },
     { language: 'dansk',
       content: '<h1>Lunorth Turneringen</h1><p>Test.bob.hi.cherry..</p>',
       _id: 56806efa7dd9c0c81c39738f },
     { language: 'dansk',
       content: '<h1>Lunorth Turneringen</h1><p>Test.bob.hi.cherry..</p>',
       _id: 56806efa7dd9c0c81c39738f } ] }

_.assign

之后的结果
{ _id: 56806efa7dd9c0c81c39738c,
  page: 'index',
  __v: 7,
  content:
   [ { language: 'dansk',
       content: '<h1>Lunorth Turneringen</h1><p>Test.bob.hi.cherry..</p>',
       _id: 56806efa7dd9c0c81c39738f },
     { language: 'dansk',
       content: '<h1>Lunorth Turneringen</h1><p>Test.bob.hi.cherry..</p>',
       _id: 56806efa7dd9c0c81c39738f },
     { language: 'dansk',
       content: '<h1>Lunorth Turneringen</h1><p>Test.bob.hi.cherry..</p>',
       _id: 56806efa7dd9c0c81c39738f } ] }

_.extend

之后的结果
{ _id: 56806efa7dd9c0c81c39738c,
  page: 'index',
  __v: 7,
  content:
   [ { language: 'dansk',
       content: '<h1>Lunorth Turneringen</h1><p>Test.bob.hi.cherry..</p>',
       _id: 56806efa7dd9c0c81c39738f },
     { language: 'dansk',
       content: '<h1>Lunorth Turneringen</h1><p>Test.bob.hi.cherry..</p>',
       _id: 56806efa7dd9c0c81c39738f },
     { language: 'dansk',
       content: '<h1>Lunorth Turneringen</h1><p>Test.bob.hi.cherry..</p>',
       _id: 56806efa7dd9c0c81c39738f } ] }

0 个答案:

没有答案