mongoose 获取CastError:值“{ '$exists':错误}”

kq0g1dla  于 2023-10-19  发布在  Go
关注(0)|答案(1)|浏览(87)

我正在尝试查找和更新没有end_date字段的文档。这是密码

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." }
    }
};

这就是我得到的错误:CastError: Cast to date failed for value "{ '$exists': false }" (type Object) at path "end_date" for model "gameData"
查询没有被修改,所以我确信这不是查询的问题。如果我删除end_date检查,更新将正确发生。
任何帮助将不胜感激。
曲名: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
    }
});
vwkv1x7d

vwkv1x7d1#

sanitizeFiler设置为true。mongoose.set('sanitizeFilter', true);
使用mongoose.trusted修复了它。

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

相关问题