i want to get all document , with all child , and size of array child
i use go + mongodb ,
so i created this aggregation ,
lookupCurrentLoans := bson.D{
{"$lookup",
bson.M{
"from": "Prets",
"let": bson.M{"b_id": "$_id"},
"pipeline": bson.A{
bson.M{
"$match": bson.M{
"$expr": bson.M{
"$and": []interface{}{
bson.M{ "$eq": []string{ "$idborrower", "$$b_id" } },
// bson.M{ "$eq": []bool{ "$is_preted", true } }, // Attached campaign is
active
},
},
},
},
},
"as": "currentLoan",
}},
}
lookupLocalisation := bson.D{
{"$lookup", bson.D{
{"from", "libraries"},
{"localField", "localisation"},
{"foreignField", "_id"},
{"as", "localisation"},
}}}
unwindLocalisation := bson.D{{"$unwind", bson.D{
{"path", "$localisation"},
}}}
lookupStatusBorrowers := bson.D{
{"$lookup", bson.D{
{"from", "Status_Borrowers"},
{"localField", "status_borrowers"},
{"foreignField", "_id"},
{"as", "status_borrowers"},
}}}
unwindStatusBorrower := bson.D{{"$unwind", bson.D{
{"path", "$status_borrowers"},
}}}
lookupCategoriesBorrowers := bson.D{
{"$lookup", bson.D{
{"from", "Categories_Borrowers"},
{"localField", "categories_borrowers"},
{"foreignField", "_id"},
{"as", "categories_borrowers"},
}}}
unwindCategoriesBorrower := bson.D{{"$unwind", bson.D{
{"path", "$categories_borrowers"},
}}}
lookupStaticCodes := bson.D{
{"$lookup", bson.D{
{"from", "CodeStatic"},
{"localField", "static_code"},
{"foreignField", "_id"},
{"as", "static_code"},
}}}
unwindStaticCode := bson.D{{"$unwind", bson.D{
{"path", "$static_code"},
}}}
// current Loans
GroupLoan := bson.D{{
"$project", bson.D{
{"_id", 1},
{"currentLoan", bson.M{"$size": "$currentLoan"}},
}}}
showInfoReservation, err := models.DB.Collection(os.Getenv(db.Borrowers
)).Aggregate(ctx,
mongo.Pipeline{
lookupCurrentLoans,
lookupLocalisation, lookupStatusBorrowers,
lookupCategoriesBorrowers,
lookupStaticCodes,
unwindStaticCode,
unwindCategoriesBorrower,
unwindStatusBorrower,
unwindLocalisation,
GroupLoan,
})
--
You received this message because you are subscribed to the Google Groups
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/golang-nuts/7c439fb2-b780-4bbd-98c3-280e502db800n%40googlegroups.com.