doujing5726 2014-10-17 15:22 采纳率: 0%
浏览 160
已采纳

使用存储在数组中的_id从golang查询mongodb

So here is my question. I have an array which are stored the _ids of mongodbs objects. Whats the right way to retrieve them all in one query using the mgo and bson package?

So if the array is like that: ids:=["543d171c5b2c12420dd016","543d171c5b2dd016"]

How we make the query ? I tried that but I know its wrong.

query := bson.M{"_id": bson.M{"$in": ids}}
c.Find(query).All()

Thanks in advance

  • 写回答

1条回答 默认 最新

  • doubao6464 2014-10-17 16:01
    关注

    If the documents are stored with string ids, then the code looks correct.

    The ids look like hex encoded object ids. If the object identifiers are object ids, then you need to the convert the hex strings to object ids:

    oids := make([]bson.ObjectId, len(ids))
    for i := range ids {
      oids[i] = bson.ObjectIdHex(ids[i])
    }
    query := bson.M{"_id": bson.M{"$in": oids}}
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料