查询时间范围内的 ObjectId

huangapple go评论81阅读模式
英文:

mgo: Query ObjectId for range of time values

问题

好的,以下是翻译的内容:

假设你有一些帖子:

type Post struct {
    Id bson.ObjectId `bson:"_id,omitempty"`
}

每个帖子都有一个在特定时间创建的唯一ID。

你可以使用post.Id.Time()来获取时间值。

但是,如何查询例如2015年的帖子呢?

以及如何查询2014年1月1日至2015年12月31日之间的帖子呢?

我猜想我需要遍历结果,检查post.Id.Time()是否在2014年1月1日和2015年12月31日期间,并将其添加到一个帖子切片中。

在使用mgo驱动程序时,是否有更简单的方法来搜索在特定范围内或特定日期创建的帖子?

如果没有,我将接受"否"作为答案。如果有,我将接受并展示如何使用代码示例的答案。

我在Stackoverflow上找到了这篇帖子:1

然而,我不知道如何将其应用于bson.ObjectId,因为它的类型不是time.Time,而是bson.ObjectId。

英文:

Ok, say you have a number of posts

type Post struct {
    Id bson.ObjectId `bson:"_id,omitempty"`
}

and each post of course has a unique id that was created at a certain time.

I can get the time value with post.Id.Time().

However how do I query for posts from let's say the year 2015?

And how would I do a range query for posts since 01.01.2014-31.12.2015?

I would assume that I need to iterate over results, check if post.Id.Time() is between 01.01.2014 and 31.12.2015 and if it is add it to a posts slice.

Is there a less complicated way to search for posts made between certain ranges or at a certain date using the mgo driver?

If there isn't I will accept No as an answer. If there is I will accept and answer that shows how, with code example.

I have found this post on Stackoverflow:1

However I don't know how this would apply to a bson.ObjectId since they type isn't time.Time but bson.ObjectId.

答案1

得分: 2

这是如何操作的。

  • 组合fromDate和toDate。
  • 使用bson.NewObjectIdWithTime()创建bson.ObjectId
  • 查询日期范围。

示例:查询创建于2015年的帖子

year := 2015
fromDate := time.Date(year, time.January, 1, 0, 0, 0, 0, time.UTC)
toDate := time.Date(year+1, time.January, 1, 0, 0, 0, 0, time.UTC)
fromId := bson.NewObjectIdWithTime(fromDate)
toId := bson.NewObjectIdWithTime(toDate)
posts := []*Post{}
if e := cPost.Find(bson.M{"_id": bson.M{"$gte": fromId, "$lt": toId}}).All(&posts); e != nil {
}

注意:由于ObjectId不是ISODate,需要从ISODate组合ObjectId

英文:

Here's how you do it.

  • Assemble fromDate and toDate.
  • Create bson.ObjectId with bson.NewObjectIdWithTime()
  • Query for date range

Example: Query for posts created 2015

year := 2015
fromDate := time.Date(year, time.January, 1, 0, 0, 0, 0, time.UTC)
toDate := time.Date(year+1, time.January, 1, 0, 0, 0, 0, time.UTC)
fromId := bson.NewObjectIdWithTime(fromDate)
toId := bson.NewObjectIdWithTime(toDate)
posts := []*Post{}
if e := cPost.Find(bson.M{"_id": bson.M{"$gte": fromId, "$lt": toId}}).All(&posts); e != nil {
}

note: Because ObjectId isn't ISODate assemble ObjectId from ISODate

答案2

得分: 0

你应该在文档中添加一个date字段,通过该字段可以进行查询。这是最简单的选项。

type Post struct {
    Id bson.ObjectId `bson:"_id"`
    date time.Time `bson:"date"`
}

然后,你可以使用date对象编写查询语句。

英文:

You should add a date field into the documents, over which you can query. That is the easiest option.

type Post struct {
    Id bson.ObjectId `bson:"_id"`
    date time.Time `bson:"date"`
}

Then you can write your queries over the date object.

huangapple
  • 本文由 发表于 2015年7月19日 22:22:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/31502195.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定