2014-10-19 07:55 阅读 180


Wondering what is best way to make sequential queries from Golang for a mongodb. Example lets say you have :

result *bson.M
oids := make([]bson.ObjectId, len(ids))
for i := range ids {
  oids[i] = bson.ObjectIdHex(ids[i])
query := bson.M{"_id": bson.M{"$in": oids}}
error:= c.Find(query).All(&result)

And you want to take the output of the _ids and use it as a query for another table. So is this correct?

query = bson.M{"_id": bson.M{"$in": result}}
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

1条回答 默认 最新

  • 已采纳
    douju1997 douju1997 2014-10-19 15:25

    Here's how to construct a query using the ids of documents returned from some other query.

     var docs []bson.M
     if err := c.Find(query).All(&docs); err != nil {
        // handle error
     docIDs := make([]interface{}, len(docs))
     for i := range docs {
        docIds[i] = docs[i]["_id"]
    query = bson.M{"_id": bson.M{"$in": docIDs}}
    点赞 评论 复制链接分享