Why do Atlas Functions fail to process well-known operators (error: unknown operator)? especially for "$oid"

I am new to App Services and Functions, but learning bit by bit when it comes up to use.

Today is one of them where we were trying to help another forum member using our usual MongoDB query instincts. Compare ObjectId with string

our glorious { "_id": { "$oid": id_string } } query horribly fails with these two lines:

(BadValue) unknown operator: $oid

it is not alone in this that "$convert" also fails with the same error.

I know the following works for the purpose:

const bid = new BSON.ObjectId(id)
const result = await col.find({ "_id":bid})

But why can’t we use these queries as seen in any other areas of MongoDB? especially with the App Services being the closest to the heart.

use the following if you want to try live on Atlas:

  • open App Services and head to Functions in the menu to the left.
  • Create a new function and use this code
  • change db and collection names from your cluster (maybe create a new test database)
  • (un)comment/switch “result” lines with “bid” and “$oid”
  • use _id of a document in “console” tab (see last two line), click run, check “result” tab
exports = async function(id){

  const service= context.services.get("mongodb-atlas")
  const col=service.db("products").collection("current")

  const bid = new BSON.ObjectId(id)
  const result = await col.find({ "_id":bid})
  // const result = await col.find({ "_id":{"$oid":id }})
  return {result:result};
// console
// exports("637f35605b44ae9eff4da7bf")

PS: funny part (for me at least), even the result has “$oid” in it

result (JavaScript):
EJSON.parse(‘{“result”:[{“_id”:{“$oid”:“637f35605b44ae9eff4da7bf”},“name”:“name 1”,“pid”:{“$numberInt”:“123”}}]}’)


I noticed the “Functions” I am mentioning here are not part of “Realm”.

Being new in this part of MongoDB, it is easy to confuse these names. I replaced the title to have “Atlas Functions” and other parts in the text with “App Services”.