Example For Using DBRef

180 views
Skip to first unread message

AKIN KAPLANOĞLU

unread,
Dec 9, 2019, 12:51:10 PM12/9/19
to mongodb-go-driver
Hi all,
Is there any example to use DBRef with mongo-go-driver ? I could not find any example.

Divjot Arora

unread,
Dec 11, 2019, 10:25:38 AM12/11/19
to mongodb-go-driver
Hi Akin,

I believe you can use primitive.DBPointer for this.

Divjot Arora

unread,
Dec 11, 2019, 10:49:36 AM12/11/19
to mongodb-go-driver
My previous response was incorrect as DBRef and DBPointer are not the same thing. There is no defined DBRef data type in the driver, but you can use any document type (e.g. bson.D/bson.M) or define a struct with the appropriate struct tags to create a DBRef document.

AKIN KAPLANOĞLU

unread,
Dec 11, 2019, 11:09:58 AM12/11/19
to mongodb-...@googlegroups.com
Hi Arora,
What do you mean by saying "appropriate tags" ? Do you have a good example?

11 Ara 2019 Çar 18:49 tarihinde 'Divjot Arora' via mongodb-go-driver <mongodb-...@googlegroups.com> şunu yazdı:
--
You received this message because you are subscribed to the Google Groups "mongodb-go-driver" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mongodb-go-dri...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/mongodb-go-driver/19fa1435-dbbf-4ce3-9d38-8014d68cecda%40googlegroups.com.

Divjot Arora

unread,
Dec 11, 2019, 12:00:54 PM12/11/19
to mongodb-go-driver
A DBRef is a document in the form {$ref: <value>, $id: <value>, $db: <value>}. You can accomplish this in Go with the following struct:

type DBRef struct {
   
Ref interface{} `bson:"$ref"`
   ID  
interface{} `bson:"$id"`
   DB  
interface `bson:"$db"`
}

The BSON tags will handle the conversion between the struct field names and the DBRef document keys.


On Wednesday, December 11, 2019 at 11:09:58 AM UTC-5, AKIN KAPLANOĞLU wrote:
Hi Arora,
What do you mean by saying "appropriate tags" ? Do you have a good example?

11 Ara 2019 Çar 18:49 tarihinde 'Divjot Arora' via mongodb-go-driver <mongodb-...@googlegroups.com> şunu yazdı:
My previous response was incorrect as DBRef and DBPointer are not the same thing. There is no defined DBRef data type in the driver, but you can use any document type (e.g. bson.D/bson.M) or define a struct with the appropriate struct tags to create a DBRef document.

On Wednesday, December 11, 2019 at 10:25:38 AM UTC-5, Divjot Arora wrote:
Hi Akin,

I believe you can use primitive.DBPointer for this.

On Monday, December 9, 2019 at 12:51:10 PM UTC-5, AKIN KAPLANOĞLU wrote:
Hi all,
Is there any example to use DBRef with mongo-go-driver ? I could not find any example.

--
You received this message because you are subscribed to the Google Groups "mongodb-go-driver" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mongodb-...@googlegroups.com.

AKIN KAPLANOĞLU

unread,
Dec 11, 2019, 12:24:40 PM12/11/19
to mongodb-...@googlegroups.com
Thank you so much.

11 Ara 2019 Çar 20:00 tarihinde 'Divjot Arora' via mongodb-go-driver <mongodb-...@googlegroups.com> şunu yazdı:
To unsubscribe from this group and stop receiving emails from it, send an email to mongodb-go-dri...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/mongodb-go-driver/41f3f118-d7bf-4fbd-9851-2c43b0ca4b9d%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages