简体   繁体   English

MongoDB将子文档的元素移动到另一个子文档

[英]MongoDB move element of sub-document to another sub-document

I need to change my schema 我需要更改架构

const dataSchema = new Schema({
  trackingPrice: [{
    timeCheck: { type: Date, default: Date.now },
    lowestPrice: Number,
    salePrice: Number,
    saleRank: Number
  }]
})

to this 对此

const dataSchema = new Schema({
  trackingPrice: [{
    timeCheck: { type: Date, default: Date.now },
    lowestPrice: Number,
    salePrice: Number
  }],
  trackingRank: [{
    timeCheck: { type: Date, default: Date.now },
    saleRank: Number
  }]
})

How can I transfer my data from one to another under document and then delete "saleRank"? 如何在文档下将数据从一个转移到另一个,然后删除“ saleRank”?

The best way I see is to find all of them, and foreach one create a new one 我认为最好的方法是找到所有这些,然后每一个创建一个新的

I'm using mongoose by the way 我顺便用猫鼬

    dataSchema.find({}, (err,all) => {
          var array = [];
          all.foreach( (ds) => {
            array.push({
              trackingPrice: ds.trackingPrice,
              trackingRank: //whatever way you want to update the old data
           })
           dataSchema.remove({}).exec(() => {
             array.foreach((a) => {
               var toadd = new dataSchema({
                   trackingPrice:a.trackingPrice,
                   trackingRank:a.trackingRank});
               toadd.save();                     
             })             
          })
       })}
    ); 

Based on this very good answer , the cursor in your case would be derived from running the aggregate pipeline: 基于这个很好的答案 ,您的情况下的游标将来自运行聚合管道:

const pipeline = [
    {
        "$project": {
            "trackingPrice": {
                "$map": {
                    "input": "$trackingPrice",
                    "as": "el",
                    "in": { 
                        "timeCheck": "$$el.timeCheck",
                        "lowestPrice": "$$el.timeCheck",
                        "salePrice": "$$el.salePrice"
                    }
                }
            },
            "trackingRank": {
                "$map": {
                    "input": "$trackingPrice",
                    "as": "el",
                    "in": { 
                        "timeCheck": "$$el.timeCheck",
                        "saleRank": "$$el.saleRank"
                    }
                }
            }
        }
    }
];

const cursor = Data.aggregate(pipeline).exec();

Running the bulk update: 运行批量更新:

let bulkUpdateOps = [];

cursor.then(results => {
    results.forEach(doc => {
        const { _id, trackingPrice, trackingRank } = doc;
        bulkUpdateOps.push({
            "updateOne": {
               "filter": { _id },
               "update": { "$set": { trackingPrice, trackingRank } },
               "upsert": true
            }
        });
    }); 

    if (bulkUpdateOps.length === 1000) {
        bulkUpdateOps = [];
        return Data.bulkWrite(bulkUpdateOps);           
    }       

}).then(console.log).catch(console.error);

if (bulkUpdateOps.length > 0) {
    Data.bulkWrite(bulkUpdateOps).then(console.log).catch(console.error);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM