You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Flask-MongoDB报错FieldDoesNotExist:'__v'字段不存在于WorkflowExecutions文档

解决MongoEngine报错FieldDoesNotExist: The fields "{'__v'}" do not exist on the document "WorkflowExecutions"

问题原因

Node.js的Mongoose默认会给文档添加__v版本键(用于乐观锁机制),而你的Flask-MongoEngine模型WorkflowExecutions未定义该字段。当MongoEngine读取MongoDB中带有__v字段的文档时,会因模型与文档字段不匹配抛出错误。即使删除集合,NodeJS应用写入新文档时仍会自动生成__v,导致问题重复出现。

解决方案

方案1:在MongoEngine模型中添加__v字段

在Flask的WorkflowExecutions模型中添加__v字段,让模型兼容Mongoose生成的文档:

from mongoengine import IntField

class WorkflowExecutions(Document):
    __v = IntField(default=0) # 添加版本字段
    name = StringField(max_length=255)
    workflow_id = ObjectIdField(required=True, max_length=255)
    nodes = ListField()
    edges = ListField()
    status = StringField(max_length=255)
    createdAt = DateTimeField(default=datetime.utcnow)
    updatedAt = DateTimeField(default=datetime.utcnow)

方案2:禁用Mongoose的版本键

修改NodeJS的Schema配置,关闭__v字段的自动生成:

const execution_schema: Schema = new Schema({
  name: {type: String},
  workflow_id: { type: mongoose.Schema.ObjectId, ref: 'WorkFlow' },
  nodes: [{ type: Schema.Types.Mixed }],
  edges: [{type: Schema.Types.Mixed}], 
  status: {type: String}
},{
  timestamps: true,
  versionKey: false // 禁用__v字段
});

方案3:让MongoEngine忽略未知字段

在MongoEngine模型中设置strict=False,允许模型忽略未定义的字段:

class WorkflowExecutions(Document):
    meta = {'strict': False} # 忽略未知字段
    name = StringField(max_length=255)
    workflow_id = ObjectIdField(required=True, max_length=255)
    nodes = ListField()
    edges = ListField()
    status = StringField(max_length=255)
    createdAt = DateTimeField(default=datetime.utcnow)
    updatedAt = DateTimeField(default=datetime.utcnow)

方案对比

  • 方案1:保留Mongoose的乐观锁功能,适合需要版本控制的场景。
  • 方案2:彻底消除跨应用的字段冲突,适合无需版本控制的场景。
  • 方案3:灵活兼容多应用共享集合的场景,但可能隐藏字段定义不一致的问题。

内容的提问来源于stack exchange,提问作者Veera Silamban

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.08 04:05:55