javascript - 重复的 Mongo 文档在保存回调函数中没有 _id

标签 javascript node.js mongodb mongoose

我是 mongoose 和 mongo 的新手,我需要一些帮助。我正在尝试使用 node.js 和 mongoose 复制 Mongo 中的现有文档。

我的流程是:

  1. 找到要复制的文档。
  2. 创建一个新的架构对象,传入在第一步中检索到的第一个文档的架构对象。
  3. 删除第二步中创建的对象的“_id”属性。
  4. 保存新对象。

这很好用。问题是我需要另一种文档类型,它包含一个 id 作为对此类型文档的引用。在成功回调中,传入的对象的“_id”字段没有值。这是我试图将其保存在其他文档中的地方。但是,当我检查数据库时,文档已成功保存,并且实际上有一个“_id”。

有人可以阐明这里发生的事情,或者我可能误解了这个过程。

这是我的代码: 在 Controller 中:

          async.each(cards, function(card, nextCard) {¬
¬
            var newCard = new Card(card);¬
            newCard.story_id = story._id;¬
            newCard.linkedElements = [];¬
¬
            newCard._id = null;¬
            delete newCard._id;¬
            // The id has been removed to avoid¬
            // duplicates in mongo.¬
¬
            newCard.save(function(err, savedCard) {¬
¬
              if (err) {¬
                console.log('err', err);¬
                process.exit();¬
              }¬
              // There were no errors.¬
¬
              // The current card was duplicated.¬
¬
              Element¬
                .find({'card_id': card._id}, function(err, elements) {¬
¬
                if (err) {¬
                  console.log('err', err);¬
                  process.exit();¬
                }¬
                // There were no errors.¬
¬
                async.each(elements, function(element, nextElement) {¬
¬
                  var newElement = new Element();¬
¬
                  console.log('New card : ', newCard);¬
                  console.log('Saved card : ', savedCard);¬
¬
                  newElement.card_id = savedCard._id;¬
                  ** I BELIEVE THIS IS THE PROBLEM! **
¬
                  newElement._id = null;¬
                  delete newElement._id;¬
                  // The id has been removed to avoid¬
                  // duplicates in mongo.

控制台日志和 Node 的输出:

New card :  { story_id: 53f0e8f3c45582e00927a501,
  rank: 1,
  published: 'true',
  __v: 0,
  _id: null,
  linkedElements: [] }
Saved card :  { story_id: 53f0e8f3c45582e00927a501,
  rank: 1,
  published: 'true',
  __v: 0,
  _id: null,
  linkedElements: [] }
POST /admin/stories/duplicate/53f0e4c1c973f73b08942591 200 18ms - 277b
err { [VersionError: No matching document found.] message: 'No matching document found.', name: 'VersionError' }

最佳答案

我想知道为什么它会起作用。当您在 mongoose 文档上调用 .save() 时,它会发出 update 操作,仅将更新的字段发送到 MongoDB。它更新使用 MongoDB save 命令,仅 insertupdate

可能,mongoose 只检查 insert/update 操作的状态。

我可以为您提供两种选择。

1.在调用new Card构造函数之前删除_id:

delete card._id;
var newCard = new Card(card);

或者,如果 cardmongoose 文档:

card_data = card.toObject();
delete card_data._id;
var newCard = new Card(card_data);

2. 设置新的 _id 字段而不是删除它:

newCard._id = new mongoose.Types.ObjectId();

关于javascript - 重复的 Mongo 文档在保存回调函数中没有 _id,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25352009/

相关文章:

javascript - 在保持宽高比的同时使用背景图像缩放 div 大小

javascript - 如何检索从桌面应用程序发送到 Rails API 的图像

javascript - 隐藏其他元素后将图像移动到第一个位置

sql-server - 无法使用 Tedious 和 Node JS 连接到本地 SQL Server 数据库

mongodb - 如何更新文档数组中的多个嵌套字段

javascript - 将 HTML 插入 iframe 在 Firefox/Chrome/Opera 中有效,但在 IE7/8 中无效

node.js - 按日期排序包含多个日期的列表

javascript - StrongLoop:子表的 ACL

javascript - 引用错误: user is not defined at server. js Passportjs

带有 MongoDB 的 Java 堆空间