{"action":"created","issue":{"url":"https://api.github.com/repos/Automattic/mongoose/issues/7166","repository_url":"https://api.github.com/repos/Automattic/mongoose","labels_url":"https://api.github.com/repos/Automattic/mongoose/issues/7166/labels{/name}","comments_url":"https://api.github.com/repos/Automattic/mongoose/issues/7166/comments","events_url":"https://api.github.com/repos/Automattic/mongoose/issues/7166/events","html_url":"https://github.com/Automattic/mongoose/issues/7166","id":372527576,"node_id":"MDU6SXNzdWUzNzI1Mjc1NzY=","number":7166,"title":"query casting attempts to cast comparison operator on nested objects","user":{"login":"lineus","id":6015453,"node_id":"MDQ6VXNlcjYwMTU0NTM=","avatar_url":"https://avatars3.githubusercontent.com/u/6015453?v=4","gravatar_id":"","url":"https://api.github.com/users/lineus","html_url":"https://github.com/lineus","followers_url":"https://api.github.com/users/lineus/followers","following_url":"https://api.github.com/users/lineus/following{/other_user}","gists_url":"https://api.github.com/users/lineus/gists{/gist_id}","starred_url":"https://api.github.com/users/lineus/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/lineus/subscriptions","organizations_url":"https://api.github.com/users/lineus/orgs","repos_url":"https://api.github.com/users/lineus/repos","events_url":"https://api.github.com/users/lineus/events{/privacy}","received_events_url":"https://api.github.com/users/lineus/received_events","type":"User","site_admin":false},"labels":[{"id":28988582,"node_id":"MDU6TGFiZWwyODk4ODU4Mg==","url":"https://api.github.com/repos/Automattic/mongoose/labels/confirmed-bug","name":"confirmed-bug","color":"e10c02","default":false}],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":5,"created_at":"2018-10-22T13:44:41Z","updated_at":"2018-10-24T14:06:05Z","closed_at":null,"author_association":"COLLABORATOR","body":"regarding #2901, a comment by @SachinBhandari [here](https://github.com/Automattic/mongoose/issues/2901#issuecomment-431769767)\r\n\r\n### Repro Script (2901.js)\r\n```js\r\n#!/usr/bin/env node\r\n'use strict';\r\n\r\nconst assert = require('assert');\r\nconst mongoose = require('mongoose');\r\nconst { Schema, connection } = mongoose;\r\nconst DB = '2901';\r\nconst URI = `mongodb://localhost:27017/${DB}`;\r\nconst OPTS = { family: 4, useNewUrlParser: true };\r\n\r\nconst nestedSchema = new Schema({\r\n  to: Number\r\n});\r\n\r\nconst schema = new Schema({\r\n  name: String,\r\n  nested: nestedSchema\r\n});\r\n\r\nconst Test = mongoose.model('test', schema);\r\n\r\nconst test = new Test({ name: 'test1', nested: { to: 42 } });\r\n\r\nasync function run() {\r\n  assert.strictEqual(mongoose.version, '5.3.5');\r\n  await mongoose.connect(URI, OPTS);\r\n  await connection.dropDatabase();\r\n  const doc = await Test.create(test);\r\n  assert.ok(doc);\r\n\r\n  const cond = {\r\n    $or: [\r\n      { nested: { to: { $lt: 50 } } },\r\n      { name: /2$/ }\r\n    ]\r\n  };\r\n\r\n  const found = await Test.findOne(cond).catch(assert.ifError);\r\n  assert.ok(found);\r\n  assert.strictEqual(found.name, 'test1');\r\n  console.log('All Assertions Pass.');\r\n  await connection.close();\r\n}\r\n\r\nrun().catch(error);\r\n\r\nfunction error(e) {\r\n  console.error(e.message);\r\n  return connection.close();\r\n}\r\n\r\n```\r\n### Output:\r\n```\r\nissues: ./2901.js\r\nifError got unwanted exception: Cast to Number failed for value \"{ '$lt': 50 }\" at path \"to\" for model \"test\"\r\nissues:\r\n```"},"comment":{"url":"https://api.github.com/repos/Automattic/mongoose/issues/comments/432671722","html_url":"https://github.com/Automattic/mongoose/issues/7166#issuecomment-432671722","issue_url":"https://api.github.com/repos/Automattic/mongoose/issues/7166","id":432671722,"node_id":"MDEyOklzc3VlQ29tbWVudDQzMjY3MTcyMg==","user":{"login":"SachinBhandari","id":17112076,"node_id":"MDQ6VXNlcjE3MTEyMDc2","avatar_url":"https://avatars0.githubusercontent.com/u/17112076?v=4","gravatar_id":"","url":"https://api.github.com/users/SachinBhandari","html_url":"https://github.com/SachinBhandari","followers_url":"https://api.github.com/users/SachinBhandari/followers","following_url":"https://api.github.com/users/SachinBhandari/following{/other_user}","gists_url":"https://api.github.com/users/SachinBhandari/gists{/gist_id}","starred_url":"https://api.github.com/users/SachinBhandari/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/SachinBhandari/subscriptions","organizations_url":"https://api.github.com/users/SachinBhandari/orgs","repos_url":"https://api.github.com/users/SachinBhandari/repos","events_url":"https://api.github.com/users/SachinBhandari/events{/privacy}","received_events_url":"https://api.github.com/users/SachinBhandari/received_events","type":"User","site_admin":false},"created_at":"2018-10-24T14:06:05Z","updated_at":"2018-10-24T14:06:05Z","author_association":"NONE","body":"@lineus , \r\n    Sorry for the typo. I actually mean $elemMatch solved my problem.\r\n\r\nAbove example does not solved my problem as I want to match `nested.from` and `nested.to` to same \r\nsub document."}}
{"actor":{"display_login":"SachinBhandari"}}