Use first element in a tag as a local relation field

v2
Nikita Tokarchuk 6 years ago
parent f8e84df81e
commit e973a52a88
  1. 11
      mongox/common/common.go

@ -78,9 +78,9 @@ func createAggregateLoad(db *mongox.Database, target interface{}, composed *quer
panic("there is no foreign field") panic("there is no foreign field")
} }
preloadName := strings.TrimSpace(preloadData[0]) localField := strings.TrimSpace(preloadData[0])
if len(preloadName) == 0 { if len(localField) == 0 {
preloadName = jsonName localField = "_id"
} }
foreignField := strings.TrimSpace(preloadData[1]) foreignField := strings.TrimSpace(preloadData[1])
@ -106,7 +106,7 @@ func createAggregateLoad(db *mongox.Database, target interface{}, composed *quer
} }
for _, preload := range preloads { for _, preload := range preloads {
if preload != preloadName { if preload != jsonName {
continue continue
} }
@ -119,9 +119,8 @@ func createAggregateLoad(db *mongox.Database, target interface{}, composed *quer
typ := el.Field(i).Type() typ := el.Field(i).Type()
lookupCollection := db.GetCollectionOf(reflect.Zero(typ).Interface()) lookupCollection := db.GetCollectionOf(reflect.Zero(typ).Interface())
lookupVars := primitive.M{"selector": "$_id"} lookupVars := primitive.M{"selector": "$" + localField}
lookupPipeline := primitive.A{ lookupPipeline := primitive.A{
// todo: make match from composed query
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"}}}},
} }

Loading…
Cancel
Save