【问题标题】:how to copy or clone a document in MongoDB Collection using mongoose and nodeJS如何使用 mongoose 和 nodeJS 在 MongoDB 集合中复制或克隆文档
【发布时间】:2021-09-25 06:42:15
【问题描述】:

我们如何将文档克隆到具有新 _id 的相同或新集合中

这里是来自“doc”(res.query)的查询中找到的数据

[
  {
    _id: new ObjectId("614d4766fb2600340fdb2904"),
    templateName: 'first template',
    _ref: '1632454502ref',
    owner: 'wxTWH8zqSwaIXPAVsjZoRCkvjx73',
    __v: 0
  }
]

不工作:

TemplateInfo.find(req.query).exec(function (err, doc) {
      console.log("document", doc);
      var newdoc = new TemplateInfo(doc[0]);
      delete newdoc._id; //= mongoose.Types.ObjectId();
      newdoc.save();
    });

这可行,但数据现在是一个未正确插入的对象

TemplateInfo.find(req.query).exec(function (err, doc) {
      console.log("document", doc);
      var newdoc = new TemplateInfo(doc);
      newdoc._id = mongoose.Types.ObjectId();
      newdoc.save();
    });

结果错误:

{
    "0": {
        "_id": {
            "$oid": "614d4766fb2600340fdb2904"
        },
        "templateName": "first template",
        "_ref": "1632454502ref",
        "owner": "wxTWH8zqSwaIXPAVsjZoRCkvjx73",
        "__v": 0
    },
    "_id": {
        "$oid": "614e34dbf288909e4713282b"
    },
    "__v": 0
}

工作正常,但是我需要从查询中获取我的数据,而不是手动输入

const docData = {
      //_id: new ObjectId("614d4766fb2600340fdb2904"),
      templateName: "first template",
      _ref: "1632454502ref",
      owner: "wxTWH8zqSwaIXPAVsjZoRCkvjx73",
      __v: 0,
    };

    TemplateInfo.find(req.query).exec(function (err, doc) {
      console.log("document", doc);
      var newdoc = new TemplateInfo(docData);
      newdoc._id = mongoose.Types.ObjectId();
      newdoc.save();
    });

结果正确,但我手动将它们作为变量输入:

{
    "_id": {
        "$oid": "614e378700b5bd9c7ba5ef37"
    },
    "templateName": "first template",
    "_ref": "1632454502ref",
    "owner": "wxTWH8zqSwaIXPAVsjZoRCkvjx73",
    "__v": 0
}

我希望在 mongoDB 集合中有一份具有不同 _id 的文档的副本


  {
    _id: new ObjectId("614d4766fb2600340fdb2904"),
    templateName: 'first template',
    _ref: '1632454502ref',
    owner: 'wxTWH8zqSwaIXPAVsjZoRCkvjx73',
    __v: 0
  }

【问题讨论】:

    标签: javascript node.js mongodb mongoose


    【解决方案1】:

    以下是mongoose中复制/克隆文档的示例。

    const mongoose = require('mongoose');
    
    mongoose.connect('mongodb://localhost:27017/test');
    
    const Cat = mongoose.model('Cat', { name: String });
    
    Cat.find({_id:'614e440b771169c71a34dae9'}).exec(function (err, doc) {
        console.log("document", doc);
        
        doc.forEach(node => insertBatch(node));
      });
    
      function insertBatch( doc) {
        var id;
        
        id = mongoose.Types.ObjectId();
        doc._id =  id;
        console.log("doc", doc);
    
        const kitty = mongoose.model('Cat');
    
        kitty.findOneAndUpdate({_id: id}, doc, {upsert: true}, function(err, doc1) {
            if (err) return res.send(500, {error: err});
            console.log('Succesfully saved.');
        });
      }
      
    

    “614e440b771169c71a34dae9”可以是集合中的任何id

    【讨论】:

    • 完美运行。您的示例按预期工作。希望我为正在寻找与您提供的相同解决方案的其他人解释这个问题
    猜你喜欢
    • 2014-04-02
    • 1970-01-01
    • 2021-12-11
    • 1970-01-01
    • 1970-01-01
    • 2021-10-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多