这是我的mongodb数据库:
"_id" : ObjectId("58808d735ba19c2797f486ca"),
"userid" : ObjectId("58808d735ba19c2797f486c9"),
"history" : [
{
"floorId" : "309cf96f-1812-44f6-8d94-d5ce2b8839be",
"time" : ISODate("2017-01-19T09:57:34.572Z"),
"position" : {
"latitude" : 48.815267598833806,
"longitude" : 2.3630101271630677
},
"pointcoordinates" : {
"pointX" : 503.82333,
"pointY" : 339.00385
}
},
{
"floorId" : "309cf96f-1812-44f6-8d94-d5ce2b8839be",
"time" : ISODate("2017-01-19T09:57:34.574Z"),
"position" : {
"latitude" : 48.815267598833806,
"longitude" : 2.3630101271630677
},
"pointcoordinates" : {
"pointX" : 503.82333,
"pointY" : 339.00385
}
}, ... This array is very huge !
我想从日期范围的“历史记录”中检索一些值。
首先,我需要选择我想访问的“用户ID”对象,然后选择“历史记录”。 然后获取我知道的日期范围内的值。
我用mgo.v2(mongodb)驱动程序使用Golang。
这是我的代码:
id := queryValues.Get("id")
startTime := queryValues.Get("startTime")
endTime := queryValues.Get("endTime")
//Here I get times in forme time.Time
t_startTime, err := time.Parse(time.RFC3339Nano, startTime)
t_endTime, err := time.Parse(time.RFC3339Nano, endTime)
oid := bson.ObjectIdHex(id)
//I select the range of time what I want
selector := bson.M{"history": bson.M{"time": bson.M{"$gte": t_startTime, "$lte": t_endTime}}}
if err := uc.session.DB("TEST").C("history").Find(bson.M{"userid": oid}).Select(selector).All(&history); err != nil {
fmt.Println(err)
SendError(w, "GetHistory", "Error retrieving history")
} else {
spew.Dump(history)
}
我遇到了一个错误:
Can not can canonicalize query:BadValue Unsupported projection option:history:{time:{$ gte:new Date(1484819854576),$ lte:new Date(1484819854576)}}
有人可以帮忙吗?