1 回答
TA贡献1893条经验 获得超10个赞
下面的代码应该为您解决问题
UID := "id-1" // Change this variable to your `match` value
matchStage := bson.D{
{"$match", bson.D{
{"$or", bson.A{
bson.D{{"uid1", UID}},
bson.D{{"uid2", UID}},
}},
}},
}
groupStage := bson.D{
{"$group", bson.D{
{"_id", nil},
{"uids1", bson.D{
{"$addToSet", "$uid1"},
}},
{"uids2", bson.D{
{"$addToSet", "$uid2"},
}},
}},
}
projectStage := bson.D{
{"$project", bson.D{
{"uids", bson.D{
{"$filter", bson.D{
{"input", bson.D{
{"$concatArrays", bson.A{"$uids1", "$uids2"}},
}},
{"as", "arrayElem"},
{"cond", bson.D{
{"$ne", bson.A{"$$arrayElem", UID}},
}},
}},
}},
}},
}
cursor, err := collection.Aggregate(
ctx,
mongo.Pipeline{matchStage, groupStage, projectStage},
options.Aggregate().SetAllowDiskUse(true),
)
if err != nil {
panic(err)
}
var cursorResult []bson.M
err = cursor.All(ctx, &cursorResult)
if err != nil {
panic(err)
}
fmt.Println("Cursor Result: ", cursorResult[0]["uids"])
- 1 回答
- 0 关注
- 142 浏览
添加回答
举报