如何使用Mongoose和node.js将文档移动到另一个集合

时间:2019-05-09 20:44:29

标签: javascript node.js mongodb express mongoose

我正在使用w / mongodb设置一个Node.js / Express应用程序。我正在使用猫鼬做所有的mongodb东西。

首先,我想向“ pendingOrders”集合中添加一个新文档,然后将其移至“ activeOrders”集合中。

创建文档->保存在“ pendingOrders”中->处理->将文档移至“ activeOrders”->保存在“ activeOrders”中。

我已经尝试了另一篇文章中的答案。给我这个错误:

DocumentNotFoundError:未找到查询“ {_id:5cd48dba5c618c343a117bd4}”的文档

let ChangeSchema = new Schema({
    title: String,
    version: Number
});

mongoose.model('First', ChangeSchema);
mongoose.model('Second', ChangeSchema);

mongoose.model('First').findOne({ _id: mongoose.Types.ObjectId(id) }, function(err, result) {

    let swap = new (mongoose.model('Second'))(result)
    swap._id = mongoose.Types.ObjectId()

    result.remove()
    swap.save()

    // swap is now in a better place

});

这是我当前的代码,对我来说,它具有完美的意义:

// require mongoose and make connection
const pendingOrder = require('../models/pendingOrder');
const activeOrder = require('../models/activeOrder');

// Create the new pending order with data
const newPendingOrder = new pendingOrder({
        branche: branche,
        stilartType: stilartType,
        animationType: animationType,
        animationHovedrolle: animationHovedrolle,
        animationPlatform: animationPlatform,
        themecolor: themecolor,
        animationCompanyAudience: animationCompanyAudience,
        animationCompanyLink: animationCompanyLink,
        animationCompanyLogo: animationCompanyLogo,
        animationManuscript: animationManuscript,
        orderUserReepayHandle: user.reepayCustomer,
        orderUserEmail: user.email,
        orderTotal: 0,
        animationManuscriptWordcount: wordsCount,
        orderTitle: "Animationsvideo"
    });

// Save the pending order and return the saved order to "pendingOrderSaved"
    newPendingOrder.save().then(pendingOrderSaved => {
        if (pendingOrderSaved) {
// create a new active order from the "pendingOrderSaved" data.
            const newActiveOrder = new activeOrder(pendingOrderSaved);
// save the newActiveOrder
            newActiveOrder.save().then(activeOrderSaved => {
                if (activeOrderSaved) {
// remove the pendingOrderSaved
                    pendingOrderSaved.remove();
                    console.log("Pending Order deleted and inserted into active order.");
                    res.send("Working");
                }
            }).catch(err => console.log(err));
        }
    }).catch(err => console.log(err));

我希望它首先在“ pendingOrders”中创建它,然后立即将其从“ pendingOrders”中删除并将其移至“ activeOrders”。实际发生的是,当尝试将“ activeOrder”保存为“ activeOrders”时,出现错误:

未找到用于查询“ {_id:5cd490561d49de2c46e306f5}的文档

我不理解此错误,因为我只是保存而不查找具有该ID的任何文档。如果我移除pendingOrderSaved.remove()

,仍然会发生错误

编辑我已解决的问题!

我通过将toObject();添加到我从添加响应中返回的未决对象OrderOrderd对象中解决了该问题。

解决的代码如下:

newPendingOrder.save().then(pendingOrderSaved => {
        if (pendingOrderSaved) {
            pendingOrderSaved.remove().then(orderRemoved => {
                newActiveOrder = new activeOrder(orderRemoved.toObject());
                newActiveOrder.save();
            }).catch(err => console.log(err));
        }
    }).catch(err => console.log(err));

0 个答案:

没有答案
相关问题