Mongoose 返回具有 ‘someField’ 存在的文档,其中 someField 不存在。

huangapple go评论74阅读模式
英文:

With 'someField': { $exists: true } Mongoose returns a document for which someField doesn't exist

问题

以下是代码部分的翻译:

for await (const expression of Expression.find({ 'definition': { $exists: true } })) {
 console.log(Utils.stringize(expression))
}

你运行的 Typescript 代码出现了一些奇怪的情况。日志返回了下面的文档,其中字段 definition 不存在:

{
  "_id": "63cc466d2c2338ef26205618",
  "labels": [
    "⊥"
  ],
  "author": {
    "userId": "63cc466c2c2338ef26205609",
    "username": "webmaster"
  },
  "timestampCreation": "2023-01-21T20:09:17.027Z",
  "tags": [
    "native symbol",
    "logic",
    "mathematics",
    "false"
  ],
  "nbrViews": 1,
  "nbrUsesInExpressions": 2,
  "nbrUsesInTruths": 0,
  "idsUsedExpressions": [],
  "idsUsedExtraAxioms": [],
  "type": {
    "sort": "P"
  },
  "parameters": [],
  "__v": 0
}

如果有所帮助,这是集合的 Mongoose 架构:

const expressionSchema = new Schema<IExpression>({
  labels: {
    required: true,
    type: [String],
    trim: true
  },
  author: {
    type: Schema.Types.Mixed,
    required: true,
  },
  timestampCreation: {
    type: Date,
    required: true,
  },
  tags: {
    required: true,
    type: [String],
    trim: true
  },
  nbrViews: {
    required: true,
    type: Number,
  },
  nbrUsesInExpressions: {
    required: true,
    type: Number,
  },
  nbrUsesInTruths: {
    required: true,
    type: Number,
  },
  idsUsedExpressions: {
    type: [Schema.Types.ObjectId],
  },
  idsUsedExtraAxioms: {
    required: true,
    type: [Schema.Types.ObjectId],
  },
  type: {
    required: true,
    type: Schema.Types.Mixed
  },
  parameters: {
    type: Array
  },
  definition: {
    type: Schema.Types.Mixed
  },
  idInstantiatedTruth: {
    type: Schema.Types.ObjectId,
  },
  idResultingTruth: {
    type: Schema.Types.ObjectId,
  }
})

你已经尝试了 Expression.find({ 'definition': { $ne: null } })Expression.find().where('definition').exists(true)),还尝试了添加和删除一些引号,但结果仍然相同。

有人能理解这个问题吗?

英文:

Something strange is happening to me. I run the following Typescript code:

for await (const expression of Expression.find({&#39;definiton&#39;: { $exists: true }}))
{
 console.log(Utils.stringize(expression))
}

And the logging returns me the following document, in which the field definition doesn't exist:

{
  &quot;_id&quot;: &quot;63cc466d2c2338ef26205618&quot;,
  &quot;labels&quot;: [
    &quot;⊥&quot;
  ],
  &quot;author&quot;: {
    &quot;userId&quot;: &quot;63cc466c2c2338ef26205609&quot;,
    &quot;username&quot;: &quot;webmaster&quot;
  },
  &quot;timestampCreation&quot;: &quot;2023-01-21T20:09:17.027Z&quot;,
  &quot;tags&quot;: [
    &quot;native symbol&quot;,
    &quot;logic&quot;,
    &quot;mathematics&quot;,
    &quot;false&quot;
  ],
  &quot;nbrViews&quot;: 1,
  &quot;nbrUsesInExpressions&quot;: 2,
  &quot;nbrUsesInTruths&quot;: 0,
  &quot;idsUsedExpressions&quot;: [],
  &quot;idsUsedExtraAxioms&quot;: [],
  &quot;type&quot;: {
    &quot;sort&quot;: &quot;P&quot;
  },
  &quot;parameters&quot;: [],
  &quot;__v&quot;: 0
}

If it can be of some help, here is the Mongoose schema of the collection:

const expressionSchema = new Schema&lt;IExpression&gt;({
  labels: {
    required: true,
    type: [String],
    //unique: false,
    trim: true
  },
  author: {
    type: Schema.Types.Mixed, //Author,
    required: true,
  },
  //timestampCreation: Date,
  timestampCreation: {
    type: Date,
    required: true,
  },
  tags: {
    required: true,
    type: [String],
    trim: true
  },
  nbrViews: {
    required: true,
    type: Number,
  },
  nbrUsesInExpressions: {
    required: true,
    type: Number,
  },
  nbrUsesInTruths: {
    required: true,
    type: Number,
  },
  idsUsedExpressions: {
    type: [Schema.Types.ObjectId],
  },
  idsUsedExtraAxioms: {
    required: true,
    type: [Schema.Types.ObjectId],
  },
  type: {
    required: true,
    type: Schema.Types.Mixed //Sort|UnknownType|CompoundType
  },
  parameters: {
    type: Array
  },
  definition: {
    type: Schema.Types.Mixed //StatementBoundVariables,
  },
  idInstantiatedTruth: {
    type: Schema.Types.ObjectId,
  },
  idResultingTruth: {
    type: Schema.Types.ObjectId,
  }
})

And I've tried with &#39;definiton&#39;: { $ne: null } and Expression.find().where(&#39;definiton&#39;).exists(true)), I've removed and added some quotes, but the result remains the same.

Anyone to understand this?

答案1

得分: 1

你对"definition"的拼写不一致。

在你的模式文件中,你正确地将其定义为"definition",但在你的示例查询中,你使用了"definiton"(请注意最后两个字符前缺少字母"i")。

英文:

Your spelling of 'definition' is inconsistent.

In your schema file you correctly define it as 'definition' but in your example query you use 'definiton' ( note the missing 'i' before the last two characters )

huangapple
  • 本文由 发表于 2023年3月7日 05:26:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/75656000.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定