doushi8599 2018-10-04 01:32
浏览 394

如何在MongoDB Go Go官方驱动程序上查找和比较日期?

I am new to mongodb-go-driver and i am stuck.

I have a date inside a struct like:

type Email struct {
    Date        string            `json:"date"`
}

the Dates on my mongoDB and mapped in my struct have the values like "02/10/2018 11:55:20".

I want to find on my DB the elements that Date are after an other date, i'm trying this but the response is always null.

initDate, _ := time.Parse("02012006", initialDate)
cursor, err := emails.Find(context.Background(), bson.NewDocument(bson.EC.SubDocumentFromElements("date", bson.EC.DateTime("$gt", initDate.Unix()))))

what am i doing wrong?

  • 写回答

1条回答 默认 最新

  • douge3830 2018-10-09 12:21
    关注

    the Dates on my mongoDB and mapped in my struct have the values like "02/10/2018 11:55:20".

    There are a number of ways you could do. The first, as mentioned on the comment, is to convert the string date into an actual date format. See also MongoDB Date. Storing date values in the proper date format is the recommended way for performance.

    If you have a document:

    { "a": 1, "b": ISODate("2018-10-02T11:55:20Z") }
    

    Using mongo-go-driver (current v0.0.15) you can do as below to find and compare using date:

    initDate, err := time.Parse("02/01/2006 15:04:05", "01/10/2018 11:55:20")
    filter := bson.VC.DocumentFromElements(
        bson.EC.SubDocumentFromElements(
            "b",
            bson.EC.Time("$gt", initDate),
        ),
    )
    cursor, err := collection.Find(context.Background(), filter)
    

    Please note the layout value in the example above for time.Parse(). It needs to match the string layout/format.

    An alternative way, without converting the value is to use MongoDB Aggregation Pipeline. You can use $dateFromString operator to convert the string date into date then use $match stage to filter by date.

    For example, given documents:

    { "a": 1, "b": "02/10/2018 11:55:20" }
    { "a": 2, "b": "04/10/2018 10:37:19" }
    

    You can try:

    // Add a new field called 'newdate' to store the converted date value
    addFields := bson.VC.DocumentFromElements(
        bson.EC.SubDocumentFromElements(
            "$addFields",
            bson.EC.SubDocumentFromElements(
                "newdate",
                bson.EC.SubDocumentFromElements(
                    "$dateFromString",
                    bson.EC.String("dateString", "$b"),
                    bson.EC.String("format", "%d/%m/%Y %H:%M:%S"),
                ),
            ),
        ),
    )
    
    initDate, err := time.Parse("02/01/2006 15:04:05", "02/10/2018 11:55:20")
    
    // Filter the newly added field with the date
    match := bson.VC.DocumentFromElements(
        bson.EC.SubDocumentFromElements(
            "$match",
            bson.EC.SubDocumentFromElements(
                "newdate",
                bson.EC.Time("$gt", initDate),
            ),
        ),
    )
    pipeline := bson.NewArray(addFields, match)
    
    cursor, err := collection.Aggregate(context.Background(), pipeline)
    
    评论

报告相同问题?