我想从mongo集合
中检索id列表(类型为long)ids: = [] int64
if count >= 5 {
err = collection.Find(query).Select(bson.M {
"_id": 1
}).Skip(rand.Intn(count - 4)).Limit(4).All(ids)
}
我收到一条错误,指出http:panic serving [:: 1]:62322:result参数必须是切片地址
我尝试使用make来获取切片,这导致了相同的错误
ids: = make([]int64, 0, 4)
if count >= 5 {
err = collection.Find(query).Select(bson.M {
"_id": 1
}).Skip(rand.Intn(count - 4)).Limit(4).All(ids)
}
答案 0 :(得分:4)
将指向切片的指针传递给All
:
ids: = []int64
if count >= 5 {
err = collection.Find(query).
Select(bson.M{"_id": 1}).
Skip(rand.Intn(count - 4)).
Limit(4).
All(&ids) // <-- change is here
}