Add support elements to a pipeline

v2
Nikita Tokarchuk 6 years ago
parent b7e92a5055
commit a86455df2d
  1. 22
      mongox/common/common.go

@ -30,8 +30,20 @@ func createAggregateLoad(db *mongox.Database, target interface{}, composed *quer
collection := db.GetCollectionOf(target) collection := db.GetCollectionOf(target)
opts := options.Aggregate() opts := options.Aggregate()
pipelineHead := primitive.A{primitive.M{"$match": composed.M()}} pipeline := primitive.A{}
pipelineTail := primitive.A{}
if !composed.Empty() {
pipeline = append(pipeline, primitive.M{"$match": primitive.M{"$expr": composed.M()}})
}
if composed.Sorter() != nil {
pipeline = append(pipeline, primitive.M{"$sort": composed.Sorter()})
}
if composed.Skipper() != nil {
pipeline = append(pipeline, primitive.M{"$skip": *composed.Skipper()})
}
if composed.Limiter() != nil {
pipeline = append(pipeline, primitive.M{"$limit": *composed.Limiter()})
}
el := reflect.ValueOf(target).Elem() el := reflect.ValueOf(target).Elem()
elType := el.Type() elType := el.Type()
@ -114,7 +126,7 @@ func createAggregateLoad(db *mongox.Database, target interface{}, composed *quer
lookupPipeline = append(lookupPipeline, primitive.M{"$limit": 1}) lookupPipeline = append(lookupPipeline, primitive.M{"$limit": 1})
} }
pipelineTail = append(pipelineTail, primitive.M{ pipeline = append(pipeline, primitive.M{
"$lookup": primitive.M{ "$lookup": primitive.M{
"from": lookupCollection.Name(), "from": lookupCollection.Name(),
"let": lookupVars, "let": lookupVars,
@ -127,7 +139,7 @@ func createAggregateLoad(db *mongox.Database, target interface{}, composed *quer
continue continue
} }
pipelineTail = append(pipelineTail, primitive.M{ pipeline = append(pipeline, primitive.M{
"$unwind": primitive.M{ "$unwind": primitive.M{
"preserveNullAndEmptyArrays": true, "preserveNullAndEmptyArrays": true,
"path": "$" + jsonName, "path": "$" + jsonName,
@ -136,5 +148,5 @@ func createAggregateLoad(db *mongox.Database, target interface{}, composed *quer
} }
} }
return collection.Aggregate(db.Context(), append(pipelineHead, pipelineTail...), opts) return collection.Aggregate(db.Context(), pipeline, opts)
} }

Loading…
Cancel
Save