Querying Long() data returning wrong results

I have a document that is storing the _id field as: Long(“12345”).

When querying that document using find({_id: 12345})

it’s returning incorrect results.

I’ve tested by wrapping the find logic in a Long and NumberLong function and that returns the correct row.
Examples below work:

  • find({_id: Long("12345")})
  • find({_id: NumberLong("12345")})

I’m mostly wondering that once a document is storing a field as Long(“12345”), Mongo must not be able to implicitly convert the find logic to get the correct row, unless you wrap it in the Long or NumberLong function?

Hello @mongo_maas,

Welcome back to the MongoDB Community forums :sparkles:

Apologies for the late response.

I tested it in my environment using mongosh in MongoDB 6.0.5 after inserting {_id: Long("12345")} and it worked fine for me. Sharing the command snippet for your reference:

test> db.new_coll.insertOne({ _id: Long("12345")})
{ acknowledged: true, insertedId: Long("12345") }
test> db.new_coll.findOne()
{ _id: Long("12345") }
test> db.new_coll.findOne({_id: 12345})
{ _id: Long("12345") }
test> db.new_coll.findOne({_id: Long("12345")})
{ _id: Long("12345") }
test> db.new_coll.findOne({_id: NumberLong("12345")})
{ _id: Long("12345") }

test> db.new_coll.insertOne({ _id: Long("1234554344343353")})
{ acknowledged: true, insertedId: Long("1234554344343353") }
test> db.new_coll.findOne({_id: 1234554344343353})
{ _id: Long("1234554344343353") }

Could you please share how you executed those commands and which specific MongoDB version you are using? Also, let me know if you have any further questions or doubts.

Regards,
Kushagra