Filters not working with a specific ID

Hi all,

I have the following example documents in my DB:

> db.commands.find({authorID: {$ne: 127251834153861120}}).limit(2)
{ "_id" : ObjectId("63aa8a72dc9c7ee5bab2e274"), "authorID" : NumberLong("174370290145427457"), "guildID" : NumberLong("1054865604584144946"), "channelID" : NumberLong("1054882549404549152"), "command" : "info", "appCommand" : false, "timestamp" : ISODate("2022-12-27T06:02:24.825Z") }
{ "_id" : ObjectId("63ab7857b478b29cfea6cd53"), "authorID" : NumberLong("266511987738017792"), "guildID" : NumberLong("1054865604584144946"), "channelID" : NumberLong("1054882576164208720"), "command" : "personalizar myanimelist", "appCommand" : true, "timestamp" : ISODate("2022-12-27T22:57:26.003Z") }

When I try to execute a simple find with one of the authorID’s it returns data:

> db.commands.find({"authorID": 266511987738017792}).limit(2) /*This returns data*/

But if I use the other authorID, it does not return any record, even though I see them in the DB:

> db.commands.find({"authorID": 174370290145427457}).limit(2) /*This does not return data*/

Something very interesting is that, when I try to group the data by authorID, it does return the mentioned ID:

> db.commands.aggregate([{$group: {_id: "$authorID", count:{$sum:1}}}])
{ "_id" : NumberLong("266511987738017792"), "count" : 10 }
{ "_id" : NumberLong("127251834153861120"), "count" : 55 }
{ "_id" : NumberLong("174370290145427457"), "count" : 2 }

But If I do a $nin filter, all the ids, except for that specific ID, is not filtered:

> db.commands.find({$and: [ { authorID: { $nin: [127251834153861120, 174370290145427457, 266511987738017792] } } ] }).limit(2)
{ "_id" : ObjectId("63aa8a72dc9c7ee5bab2e274"), "authorID" : NumberLong("174370290145427457"), "guildID" : NumberLong("1054865604584144946"), "channelID" : NumberLong("1054882549404549152"), "command" : "info", "appCommand" : false, "timestamp" : ISODate("2022-12-27T06:02:24.825Z") }
{ "_id" : ObjectId("63ae12b716c1064a474e3b43"), "authorID" : NumberLong("174370290145427457"), "guildID" : NumberLong("1054865604584144946"), "channelID" : NumberLong("1054882618346328074"), "command" : "estadísticas", "appCommand" : false, "timestamp" : ISODate("2022-12-29T22:20:39.436Z") }

I really don’t understand what is happening here, any guidance would be appreciated.

Thanks in advance.

Hi @Aguileitus,

You should add NumberLong() to your filter when you specify the $nin items:

db.collection.find({
  "authorID": {
    "$nin": [
      NumberLong(174370290145427457)
    ]
  }
})

Working example

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.