我有一份文件" item"看起来像这样:
{
"_id" : ObjectId("5a146ce6cca59f21e897589b"),
"platform" : "example_platform",
"mp_id" : "example_marketplace_id",
"category" : {
"platform" : "example_platform",
"id" : 999,
"name" : "example_category_name"
},
"image_urls" : [
"http://example.com/image.jpg"
],
"title" : "example_title",
"seller" : {
"username" : "example_username",
"platform" : "example_platform",
},
"quantity_sold" : 100,
"sales" : [
{
"_id" : ObjectId("5a146cf3cca59f21e8975951"),
"time" : ISODate("2017-09-09T04:07:36.000Z"),
"amount" : 31.4500007629395,
"currency" : "USD",
"buyer" : {
"username" : "example_username",
"platform" : "example_platform",
},
"item_id" : ObjectId("5a146ce6cca59f21e897589b")
},
{
"_id" : ObjectId("5a146cf3cca59f21e8975952"),
"time" : ISODate("2017-11-16T01:24:10.000Z"),
"amount" : 27.0900001525879,
"currency" : "USD",
"buyer" : {
"username" : "example_username",
"platform" : "example_platform",
},
"item_id" : ObjectId("5a146ce6cca59f21e897589b")
}
]
}
我想要做的是能够查询数据库,按照在给定时间范围内销售商品的次数对商品进行排序,并使用跳过和限制(或其他一些)对这些结果进行分页法)。
这是我的聚合查询/管道目前的样子:
func (this SellerItemsQuery) MakePipeline() []bson.M{
var pipeline = []bson.M{
{"$match": bson.M{
"seller.username": this.Username,
"seller.platform": this.Platform,
}}}
if !this.SalesFromDate.Equal(time.Time{}) && !this.SalesToDate.Equal(time.Time{}) {
pipeline = append(pipeline, bson.M{
"$addFields": bson.M{
"sales": bson.M{
"$filter": bson.M{
"input": "$sales",
"as": "sale",
"cond": bson.M{"$and": []bson.M{{"$gte": []interface{}{"$$sale.time", this.SalesFromDate}}, {"$lte": []interface{}{"$$sale.time", this.SalesToDate}}}},
},
},
},
})
}
pipeline = append(pipeline, bson.M{
"$addFields": bson.M{
"num_sales": bson.M{
"$size": bson.M{
"$ifNull": []interface{}{
"$sales", []interface{}{},
},
},
},
},
})
pipeline = append(pipeline, bson.M{
"$sort": bson.M{"num_sales": -1,
"_id": 1},
}, bson.M{
"$skip": this.Skip,
}, bson.M{
"$limit": this.Limit,
})
return pipeline
目前的问题是它返回非常不一致的结果。当一次对25个项目进行分页时(skip = 0& limit = 25,skip = 25& limit = 25等)。第一个结果集通常是正确的,最佳销售项目在给定时间段出现在顶部,销售数量按预期减少,但是通过第2,第3或第4组25,结果几乎是完全随机的。集合中的项目销售数量将突然降至零,而下一集合将包括3个销售等项目。完全出现故障,同时出现重复项目,即使我已经包含了一个类别通过{_id:1}在管道中,我认为可以解决这个问题。
答案 0 :(得分:2)
问题是bson.M结构是无序的,所以有时它会按" _id"排序。在排序之前的字段" num_sales"字段,导致无序结果集。转而使用bson.D有序对:
bson.M{
"$sort": bson.M{"num_sales": -1,
"_id": 1},
},
变成了:
bson.M{
"$sort": bson.D{{"num_sales", -1},
{"_id", 1}},
},