Mongoose updateOne()无法传入选项

nvbavucw  于 11个月前  发布在  Go
关注(0)|答案(1)|浏览(109)

在mongoose中更新数据时,我似乎无法传入选项,我只是得到一个错误,说Expected 0-2 arguments, but got 3.ts(2554)
我使用的代码和文档几乎一样,我使用的是mongoose的最新版本(8.0.3)。

const guild = await guildSchema.findOne({ id: interaction.guildId })
await guild?.updateOne(
        {
            id: interaction.user.id 
        },
        {
            $set: {
                "some.value": true
            }
        },
        {
            arrayFilters: [
                {
                    "i.recipe": "Fried rice",
                    "i.item": { $not: { $regex: "oil" } },
                }
            ]
        }
    )

字符串
让我如此沮丧的是,根据文档(https://mongoosejs.com/docs/api/query.html#Query.prototype.updateOne()),这段代码很好,但它仍然抛出错误。
根据文档,这个方法甚至应该有4个参数:

Parameters:

    [filter] «Object»
    [update] «Object|Array» the update command

    [options] «Object»
        [options.multipleCastError] «Boolean» by default, mongoose only returns the first error that occurred in casting the query. Turn on this option to aggregate all the cast errors.

        [options.strict] «Boolean|String» overwrites the schemas strict mode option

        [options.upsert=false] «Boolean» if true, and no documents found, insert a new document

        [options.writeConcern=null] «Object» sets the write concern for replica sets. Overrides the schema-level write concern

        [options.timestamps=null] «Boolean» If set to false and schema-level timestamps are enabled, skip timestamps for this update. Note that this allows you to overwrite timestamps. Does nothing if schema-level timestamps are not set.

        [options.translateAliases=null] «Boolean» If set to true, translates any schema-defined aliases in filter, projection, update, and distinct. Throws an error if there are any conflicts where both alias and raw property are defined on the same object.

    [callback] «Function» params are (error, writeOpResult)

ig9co6j1

ig9co6j11#

mongoose中的ModelDocumentQuery类之间存在差异。但是,Model类是Document类的子类。
认为你正在使用Query.updateOne(),它需要3个参数和一个回调。
实际上使用的是Document.updateOne(),它只接受2个参数和一个回调。
举例说明:

const guild = await guildSchema.findOne({ id: interaction.guildId })
// guild instanceof mongoose.Model; // true
// guild instanceof mongoose.Document; // true
// guild is now a instance of the Document class because you have executed the query
// and assigned the return value from the promise to a variable
await guild?.updateOne();
// This is now Document.updateOne()

字符串
我认为您应该只使用Model.findOneAndUpdate(),其中Guild是使用const Guild = mongoose.model('Guild', guildSchema);创建的Model的示例

const guild = await Guild.findOneAndUpdate(
        {
            id: interaction.user.id 
        },
        {
            $set: {
                "some.value": true
            }
        },
        {
            arrayFilters: [
                {
                    "i.recipe": "Fried rice",
                    "i.item": { $not: { $regex: "oil" } },
                }
            ],
            new: true
        }
    )

相关问题