顺序,注意顺序
cursor, err := mdb.Collection("idols_forum_count").Find(context.TODO(),
bson.M{},
&options.FindOptions{
Sort: bson.D{
{"date", -1},
{"count", -1},
},
Skip: &skip,
Limit: &perPage,
Projection: bson.M{
"_id": 0,
"idol_id": 1,
"count": 1,
"date": 1,
},
},
)
cursor, err := mdb.Collection("idols_forum_count").Find(context.TODO(),
bson.M{},
&options.FindOptions{
Sort: bson.M{
"date": -1,
"count": -1,
},
Skip: &skip,
Limit: &perPage,
Projection: bson.M{
"_id": 0,
"idol_id": 1,
"count": 1,
"date": 1,
},
},
)
你发现这两段代码的不同之处了吗?
Sort: bson.D{
{"date", -1},
{"count", -1},
},
和
Sort: bson.M{
"date": -1,
"count": -1,
},
以一定要牢记 bson.M
是无序的,bson.D
是有序的。排序字段的顺序是会影响结果和性能的。
网友评论