Search code examples
node.jsmongodbmongoose

Getting CastError: Cast to date failed for value "{ '$exists': false }"


I am trying to find and update documents that don't have the end_date field. This is the code.

export const updatePlayData = async (gameId, updatedPlayDataKey, updatedPlayData) => {
    try {
        const query = {
            _id: gameId,
            end_date: {"$exists": false}
        };

        console.log(query);
    
        const update = {
            $set: Object.keys(updatedPlayData).reduce((accumulator, key) => {
                accumulator[`play_data.${updatedPlayDataKey}.${key}`] = updatedPlayData[key];
                return accumulator;
            }, {})
        };
    
        const doc = await GameData.findOneAndUpdate(query, update, { upsert: true, new: true });
        
        if (doc === null) {
            return { type: "failure", message: doc._id + " could not be updated." };
        } else {
            return { type: "success", message: doc._id + " Game Instance has been updated." };
        }

    } catch (err) {
        logger.error(err.name + ": " + err.message);
        return { type: "error", message: "Internal Server Error. Contact administrator." }
    }
};

This is the error I'm getting: CastError: Cast to date failed for value "{ '$exists': false }" (type Object) at path "end_date" for model "gameData".

The query is not getting modified so I'm sure this is not an issue with the query. If I remove the end_date check, the update happens correctly.

Any help will be appreciated.

EDIT: The Schema

const gameDataSchema = mongoose.Schema({
    project_id: {
        type: String,
        required: true
    },
    platform: {
        type: String,
        required: true
    },
    display_size: {
        type: String,
        required: true
    },
    country: {
        type: String,
        required: true
    },
    total_play_time: {
        type: Number
    },
    start_date: {
        type: Date,
        default: Date.now
    },
    end_date: {
        type: Date
    },
    sessions: {
        type: Number,
        default: 0
    },
    sessions_length: [
    ],
    multiple_ids: {
        type: Boolean,
        default: false
    },
    filled_form: {
        type: Boolean,
        default: false
    },
    ending: {
        type: String
    },
    form_data: {
        overall: {
            type: Number,
            min: 0
        },
        ease: {
            type: Number,
            min: 0
        },
        gameplay: {
            type: Number,
            min: 0
        },
        story: {
            type: Number,
            min: 0
        },
        graphics: {
            type: Number,
            min: 0
        },
        sound: {
            type: Number,
            min: 0
        },
        email: {
            type: String
        },
        extra_questions: {
            type: mongoose.Schema.Types.Mixed
        }
    },
    relationship_data: {
        type: mongoose.Schema.Types.Mixed
    },
    choice_data: {
        type: mongoose.Schema.Types.Mixed
    },
    play_data: {
        type: mongoose.Schema.Types.Mixed
    },
    end_data: {
        type: mongoose.Schema.Types.Mixed
    }
});

Solution

  • sanitizeFiler was set to true. mongoose.set('sanitizeFilter', true);

    Using mongoose.trusted fixed it.

            const query = {
                _id: gameId,
                end_date: mongoose.trusted({$exists: false})
            };