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 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料