I get a question when using go mongo operation.
My code is like this:
iter = coll.Find(filter).Sort("-timestamp").Skip(12510).Limit(10).Iter()
for iter.Next(&result){
....
}
I have 12520 documents in collection, but fail to get value with iter.Next()
, if I have not set the index of timestamp
in MongoDB. If I set index of "timestamp", it seems work, and I can get value in result.
So, what happened?
You need to decode your data first then iterate it
here item is your struct of data you get from MongoDB
if err := iter.Decode(&item); err != nil {
return status.Errorf(
codes.Aborted,
fmt.Sprintln(errormsg.ERR_MSG_DATA_CANT_DECODE, err))
}
then do iteration it will works !!!