I wrote the code in Go. I have a mongodb query in mgo that has made me busy for 3 days and still fighting with it.
Though a query with mongo shell works, mongodb query with mgo does not work.
Below query and result are made with mongo shell.
// Query
db.getCollection("TEST").aggregate([
{
"$match": {
"date": {"$gte": new Date("2016-06-28"), "$lte": new Date("2016-06-29")},
}
},
{
"$project": {
"_id": false,
"date": {"$dateToString": {"format": "%Y%m%d", "date":"$date"}},
},
},
]);
// Result
{
"date" : "20160628",
}
{
"date" : "20160629",
}
Below query and result are made with mgo. It doesn't work.
// Query
matchQuery := bson.M{}
matchQuery["date"] = bson.M{
"$gte": time.Date(2016, 6, 28, 0, 0, 0, 0, time.UTC),
"$lte": time.Date(2016, 6, 29, 23, 59, 59, 0, time.UTC),
}
projectQuery := bson.M{
"_id": false,
"date": bson.M{
"$dateToString": bson.M{"format": "%Y%m%d", "date": "$date"},
},
}
pipeline := []bson.M{
{"$match": matchQuery},
{"$project": projectQuery},
}
// Result
{
"date": "0001-01-01T00:00:00Z",
},
{
"date": "0001-01-01T00:00:00Z",
}
How can I work $dateToString correctly?