Implement reversed preloading

v2
Nikita Tokarchuk 6 years ago
parent 2207dc5afd
commit 6e7fd5336c
  1. 10
      mongox/common/common.go

@ -89,11 +89,16 @@ func createAggregateLoad(db *mongox.Database, target interface{}, composed *quer
} }
preloadLimiter := 100 preloadLimiter := 100
preloadReversed := false
if len(preloadData) > 2 { if len(preloadData) > 2 {
stringLimit := strings.TrimSpace(preloadData[2]) stringLimit := strings.TrimSpace(preloadData[2])
intLimit := preloadLimiter intLimit := preloadLimiter
preloadReversed = strings.HasPrefix(stringLimit, "-")
if preloadReversed {
stringLimit = stringLimit[1:]
}
intLimit, err = strconv.Atoi(stringLimit) intLimit, err = strconv.Atoi(stringLimit)
if err == nil { if err == nil {
preloadLimiter = intLimit preloadLimiter = intLimit
@ -120,6 +125,9 @@ func createAggregateLoad(db *mongox.Database, target interface{}, composed *quer
primitive.M{"$match": primitive.M{"$expr": primitive.M{"$eq": primitive.A{"$" + foreignField, "$$selector"}}}}, primitive.M{"$match": primitive.M{"$expr": primitive.M{"$eq": primitive.A{"$" + foreignField, "$$selector"}}}},
} }
if preloadReversed {
lookupPipeline = append(lookupPipeline, primitive.M{"$sort": primitive.M{"_id": -1}})
}
if isSlice && preloadLimiter > 0 { if isSlice && preloadLimiter > 0 {
lookupPipeline = append(lookupPipeline, primitive.M{"$limit": preloadLimiter}) lookupPipeline = append(lookupPipeline, primitive.M{"$limit": preloadLimiter})
} else if !isSlice { } else if !isSlice {

Loading…
Cancel
Save