I can't query a record with _id type numberlong

i’m current using Compass Version 1.41.0, i have a record like this below:

{
  "_id": {
    "$numberLong": "1738455748189356060"
  },
  "conversionConfigs": [
    {
      "_id": {
        "$numberLong": "1738455748189356035"
      },
      "beforeUnit": "BOX",
      "afterUnit": "MT",
      "type": 2,
      "packType": 1,
      "ratio": "0.01236"
    }
  ],
  "parentId": {
    "$numberLong": "0"
  },
  "parentGoodsName": "",
  "rootId": {
    "$numberLong": "0"
  },
  "distributionStatus": "1",
  "tenantId": {
    "$numberLong": "1250716842252255232"
  }
}

i can’t using compass to query this record with simply _id:

db.demo.find({_id:1738455748189356060})

i also tried this:

db.demo.find({_id: NumberLong(1738455748189356060)})

i got nothing returned…why? this really confuse me…Help please~~
and i add another record with _id: 1738455748189356032, when i query with _id: 1738455748189356060, it returned the record with 1738455748189356032, this is insane…

When things fail, it always good to look at the specs.

In

you are using NumberLong incorrectly. You are entering your long number as a number, but long numbers cannot be used as numbers because they are too long. You have to use string when writing long numbers.

The fact that you find a different document sometimes is simply the fact that when you enter the long number as number, the number is rounded or truncated and you get a smaller number which matches the document that has an _id that is not stored as long number. In the shell you can see that directly with

i = 1738455748189356032

So the query should work will be something like:

db.demo.find({_id: NumberLong("1738455748189356060")})
2 Likes

thank you sir, it works with

db.demo.find({_id: NumberLong("1738455748189356060")})
1 Like