doubeishuai6598 2014-10-19 07:55
浏览 275
已采纳

使用golang和mongodb的顺序查询

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

result *bson.M
ids:=["543d171c5b2c12420dd016","543d171c5b2dd016"]
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 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}}
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 宇视监控服务器无法登录
  • ¥15 PADS Logic 原理图
  • ¥15 PADS Logic 图标
  • ¥15 电脑和power bi环境都是英文如何将日期层次结构转换成英文
  • ¥15 DruidDataSource一直closing
  • ¥20 气象站点数据求取中~
  • ¥15 如何获取APP内弹出的网址链接
  • ¥15 wifi 图标不见了 不知道怎么办 上不了网 变成小地球了
  • ¥50 STM32单片机传感器读取错误
  • ¥50 power BI 从Mysql服务器导入数据,但连接进去后显示表无数据