How to dereference dbref in mgo?
Asked Answered
B

1

10
var (
    type User struct{
        Id bson.ObjectId `bson:"_id"`
        Name string
    }

type Post struct{
    Id bson.ObjectId `bson:"_id"`
    Uid string
    User User
    ref mgo.DBRef
    Title string
    }
)

//try 10000 times inserts

id := bson.NewObjectId()
user := &User{ id, "test"}
db.C("users").insert(user)

post := db.C("Post").insert(&Post{Uid: id.hex(), ref: mgo.DBRef{"ref":"users", "id": id}, Title:"test dbref"})

//first way so dirty -_-!

//mysql: left join users on user.id=post.uid, how to do in mgo ?

posts := new([]User)
db.C("posts").Find(nil).All(posts)

ids := []bson.ObjectId
for _, p := range posts{
  ids = append(ids, p.Uid)
}

users := make([]User, len(ids))
db.C("users").Find(bson.M{"_id": {"$in": ids}}).All(users)

//and then set the User attribute?
for _,u := range users {
   for _, m := range m{
       if m.Uid == u.Id {
          m.User = m
       }
   }
}

secondary way,with ref attribute, but mgo.session will try to findid

for _,m := range posts{
   db.FindRef(m.ref).One(&m.User)
}

//3th way, with mapReduce ??

it's my first golang + mongodb, so what is the best way to archive dbref or joins?

Thx

Bunnie answered 22/8, 2015 at 8:13 Comment(2)
how about this question ? I have same problemCanon
I would think you want m.User = u in that nested loop.Abohm
Y
3

Instead of using DBRef, you can just use manual reference method for connecting two or more related documents. For example your struct can just look as below:

type User struct{
    Id bson.ObjectId `bson:"_id"`
    Name string `json:"name"`
}

type Post struct{
    UserId bson.ObjectId `json:"userid"` // manual ref to User
    Title string
}

You can then use $lookup aggregation stage to perform a left outer join. For example, to find out all posts based on users:

pipeline := []bson.M{ 
        bson.M{"$lookup": bson.M{
                            "from": "posts", 
                            "foreignField":"userid",
                            "localField":"_id",
                            "as":"posts",
                            },
              },
        }

result := []bson.M{}    
err := coll_users.Pipe(pipeline).All(&result)

Example result:

{
  "_id": ObjectId("590ab726f4bab950360c2dbe"),
  "name": "test",
  "posts": [
    {
      "_id": ObjectId("590ab72619663bad7743ff9e"),
      "userid": ObjectId("590ab726f4bab950360c2dbe"),
      "title": "test manual reference"
    }
  ]
}

Alternative to storing users and posts in separate collections, you could also embed/sub-document. See also Data Modelling

Yaupon answered 4/5, 2017 at 5:25 Comment(1)
Note $lookup is Mongo version 3.2+Unhook

© 2022 - 2024 — McMap. All rights reserved.