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)