更新猫鼬对象

JMPAUL开发

我有2个MongoDB的类别:crystalscleanses

水晶架构:

const crystalSchema = mongoose.Schema({
    crystalInfo: {
        title: {
            type: String,
            required: [true, 'Crystal title is required']
        },
        subtitle: {
            type: String
        },
        description: {
            type: String
        },
        category: {
            type: String
        },
        chakras: {
            type: String
        },
        astroSign: {
            type: String
        },
        cleaningTips: {
            type: String
        },
        thumbnail: {
            type: String
        },
        largeImage: {
            type: String
        },
        publishedDate: {
            type: Date,
            default: Date.now
        },
        affiliateLink: {
            type: String,
            require: [true, 'Affiliate link is required']
        },
        etsyLink: {
            type: String
        },
        psychicTree: {
            type: String
        }
    }
});

清洗方案:

const cleanseSchema = mongoose.Schema({
    cleanseInfo:{
        title:{
            type: String,
            required: [true, 'Cleanse title is required']
        },
        subtitle:{
            type: String
        },
        description:{
            type: String
        },
        duration: {
            type: String
        },
        useFor: {
            type: String
        },
        dontUse: {
            type: String
        },
        icon:{
            type: String
        },
        publishedDate:{
            type: Date,
            default: Date.now
        },
    }
});

我正在尝试更新crystalInfo对象以包括任何清洁提示数据以具有以下输出:

{
    crystalInfo:
    {
        title: 'Abalone Shell',
        subtitle: ...,
        description: ...,
        category: ...,
        chakras: ...,
        astroSign: ...,
        cleaningTips: 'running_water',
        thumbnail: ...,
        largeImage: ...,
        affiliateLink: ...,
        etsyLink: ...,
        psychicTree: ...,
        publishedDate: 2020 - 10 - 06 T18: 38: 30.025 Z,
        {
            cleanseInfo:
            {
                title: 'Running Water',
                subtitle: ...,
                description: ...,
                duration: ...,
                useFor: ...,
                dontUse: ...,
                icon: 'running_water',
                publishedDate: 2020 - 10 - 06 T18: 42: 34.627 Z
            },
            _id: 5 f6fa3a20b9f544d44a3ee7c,
            __v: 0
        }
    },
    _id: 5e17 c0db78fd3a1589289ddb,
    __v: 0
}

当前功能:

Crystal.find().exec().then(crystals => {
        let crystalTips = crystals.map(crystal => {
            return crystal.crystalInfo.cleaningTips;
        });

        return Promise.all([
            crystals,
            Cleanse.find({
                'cleanseInfo.icon': {
                    $in: crystalTips
                }
            }).exec()
        ]);
    }).then(results => {
        let crystals = results[0];
        let cleansingTips = results[1];

        crystals.forEach(crystal => {
            crystal.crystalInfo.cleanse = cleansingTips.filter(tip => {
                if(tip.cleanseInfo.icon === crystal.crystalInfo.cleaningTips){
                    return Object.assign(tip, crystal.crystalInfo); //This is the correct cleansing data but object is not updated?
                }
            });
        });

        console.log(crystals) //Object not updated

        //return crystals;
    }).catch(err => {
        console.log(err);
    });

任何帮助将不胜感激。

JMPAUL开发

更新了返回Object.assign()以获得正确的结果:

crystals.forEach(crystal => {
            crystal.crystalInfo.cleanse = cleansingTips.filter(tip => {
                if(tip.cleanseInfo.icon === crystal.crystalInfo.cleaningTips){
                    return crystal._doc.crystalInfo = Object.assign({cleanse: tip.cleanseInfo}, crystal._doc.crystalInfo)
                }
            });
        });

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章