Search code examples
typescriptmongodbmongoose

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


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

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

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

{
  "_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
}

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

const expressionSchema = new Schema<IExpression>({
  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 'definiton': { $ne: null } and Expression.find().where('definiton').exists(true)), I've removed and added some quotes, but the result remains the same.

Anyone to understand this?


Solution

  • 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 )