What is the "support" for DBRef in NodeJS driver?

I want to use DBRef and I can see this page states that mongodb driver for nodejs supports dbref. What is this support? Is there a method with which I can resolve dbrefs? Ideally it would be something like this:

    queryResults.dbRefField // automatically resolves cursor to dbref

or

    import { resolveDbRef } from 'mongodb'
    await resolveDbRef(queryResults.dbRefField)

but I couldn’t find any of these in documentation and I don’t want to use $lookup because I need basic syntax, not aggregation. Is this class is the only reason docs says dbrefs are “supported” by nodejs?

I tried to format code but it doesn’t seem to work, also I tried to edit my post but there is no edit button :confused:
I spent so much time trying to just login into forums because it kept redirecting me somewhere on 404 page. I encountered so many bugs in mongodb for last few months, which I can’t report because JIRA site is glitchy and doesn’t let me post bug report, I’m thinking about moving back to mysql or to something else…

Welcome to the MongoDB Community Forums @111731 !

Yes. I would avoid using the legacy DBRef type and instead use manual referencing. There is limited support for DBRefs in modern drivers, tools, and aggregation queries and the documentation should have stronger discouragement (I’ll raise an issue).

You can use GitHub-style code fences (```); I added these to your first post. See Formatting code and log snippets in posts.

We’ve unfortunately had to adjust the edit limit for new users due to spammers who sign up for accounts and repeatedly edit to try to bypass detection. As you spend a bit more time on the forums you there are increased Trust levels and forum privileges.

I’m not aware of any specific issue affecting JIRA or forum logins. Can you share more details on the sort of glitches you are experiencing?

Login issues are more common if you have very restricted cookie settings. What browser version are you using? Do you have any additional ad blockers or privacy extensions enabled?

Regards,
Stennie

1 Like

Hi,
despite the disencouragement, I read this

If you have documents in a single collection that relate to documents in more than one collection, you may need to consider using DBRefs

So what should actually be the proper way to perform references among documents with other collection’s documents?

And how the query in order to know if the object is created and then use that document’s _id or it does not yet exist so in need to be created first and afterwards put that new _id as the document reference.

Thanks!
km

Hi @kevin_Morte_i_Piferrer,

Proper approach is a matter of opinion, but I would narrow this use case down to: if you have a single field that relates to multiple collections you may want to consider using DBRefs or an equivalent subdocument format.

However, I’d still be strongly inclined to use manual references (i.e. an equivalent subdocument format) since the DBRef BSON type will be more difficult to work with in server-side queries.

The full DBRef includes an _id ($id), collection name ($ref), and optional database name ($db).

Borrowing the docs example:

  "creator" : {
                  "$ref" : "creators",
                  "$id" : ObjectId("5126bc054aed4daf9e2ab772"),
                  "$db" : "users"
               }

A manual reference might look like:

  "creator" : {
      "coll" : "creators",
      "_id" :  ObjectId("5126bc054aed4daf9e2ab772"),
       "db" : "users"
   }

If the related collection is in the same database, you could simplify to:

  "creator_id": ObjectId("5126bc054aed4daf9e2ab772")

You would have to construct appropriate queries if you want to find or combine related data, but that is still the case with DBRefs. DBRefs have limited server-side support, and have to be resolved using additional queries to return the referenced documents.

Overall DBRefs are a legacy convention best avoided in modern applications using MongoDB.

Regards,
Stennie

I think this should be reflected in the documentation… especially since it is 2023 and I am experiencing the same thing as @kevin_Morte_i_Piferrer, with regards to the relating of documents to documents in more than one collection.