2018-12-17 13:50
浏览 122

Golang mongo-go-driver Beta 1,使用大于运算符

I have been trying to get records greater than an _id provided The code is below

filter = bson.M{"_id": bson.M{"$gt": "5c1760b4bd421c09e0f3140c"}}
cur, err := collection.Find(ctx, filter, &options)

But iam always getting null values. I think i need to convert that id to object id But iam not sure how to do it in latest release There is a bson.TypeObjectID shown in predictions . Can someone please provide some details to do this.? Thanks

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dspym82000 2018-12-17 13:54

    You need to compare ObjectID to ObjectID. What you are doing is comparing an ObjectID to a string.

    objectID, _ := primitive.ObjectIDFromHex("5c1760b4bd421c09e0f3140c")
    filter = bson.M{"_id": bson.M{"$gt": objectID}}
    cur, err := collection.Find(ctx, filter, &options)
    解决 无用
    打赏 举报

相关推荐 更多相似问题