Updating Mongoose Object

JMPAUL Development

I have 2 MongoDB Collections: crystals & cleanses

Crystal Schema:

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
        }
    }
});

Cleanse Scheme:

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
        },
    }
});

I'm trying to update the crystalInfo object to include any cleansing tip data to have the following output:

{
    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
}

Current function:

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);
    });

Any assistances would be much appreciated.

JMPAUL Development

Updated the return Object.assign() to get the correct results:

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

関連記事

分類Dev

moment js not updating in mongoose

分類Dev

Object type in mongoose

分類Dev

Object type in mongoose

分類Dev

Object type in mongoose

分類Dev

mongoose not added it in same object

分類Dev

Mongoose find with reference to object

分類Dev

Object in array is not updating in JavaScript

分類Dev

Updating an object in an array if the object exists

分類Dev

mongoose findByIdAndUpdate updating only one field in the document

分類Dev

Mongoose check if user owns document before updating

分類Dev

Mongoose querying/checking document before updating

分類Dev

Mongoose ignores $ne while updating document

分類Dev

Cannot push object to array in mongoose

分類Dev

Angular 2 Updating Array object

分類Dev

Updating an object attribute from a method

分類Dev

Rotating object AABBs updating incorrectly

分類Dev

Django: Object attribute not saving/updating

分類Dev

Trouble updating rect object pygame

分類Dev

updating an object with seraph or seraph model

分類Dev

Updating managed object in background thread

分類Dev

how to insert an item to an array that is inside an object in mongoose?

分類Dev

Mongoose return data inside _doc object

分類Dev

Add key/value pair to returned mongoose object

分類Dev

How to get a specific object in mongoose with mongoDB?

分類Dev

find object by id which is in array mongoose

分類Dev

How to create nested object with Mongoose ref connection?

分類Dev

django creates new object instead of updating

分類Dev

React not updating state with setState with Array of object

分類Dev

Remove or Find an object with Mongoose when it is in an array by the object property value

Related 関連記事

ホットタグ

アーカイブ