白猪掌柜的
下面的代码应该为您解决问题UID := "id-1" // Change this variable to your `match` valuematchStage := 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.Merr = cursor.All(ctx, &cursorResult)if err != nil { panic(err)}fmt.Println("Cursor Result: ", cursorResult[0]["uids"])