Query DBRef in Realm Web in React

I’ve got a collection where items look as follows:

{"_id":{"$oid":"5ec7cfa44aead20ddc9b785e"}, "name":"Course 1", "users":[ {"$ref":"user","$id":{"$oid":"5eb0d50b564ded7137aa5472"}}, {"$ref":"user","$id":{"$oid":"5ffefrf9735a14d92480c46"}}, {"$ref":"user","$id":{"$oid":"626dedfrg556d48db6d1b95"}} ]}

How do I query for this item by users’ user id (oid)? I’m using collection.find() but no combination of queries seems to return anything. Finding by the name works perfectly (eg. collection.find("name": "Course 1").

Any help would be much appreciated.

Hi,

You should query it like ObjectId:

db.collection.find({
  "users.$id": ObjectId("5eb0d50b564ded7137aa5472")
})

Working example

Thanks for the reply. I had seen that syntax before, but ObjectId isn’t defined in Realm Web.

That said, you helped me figure it out. It’s Realm.BSON.ObjectId().

1 Like

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