我是戈蘭語和蒙古語的初學者,我使用聚合組 mongodb 刪除嵌套字段時遇到了問題。我剛剛閱讀了mongodb文檔,我認為刪除字段可以用mongodb$project但是我找不到這樣做的例子。這是查詢的結果{ "data": [ { "_id": "60db0920a2f13ba5037c90f5", "email": "[email protected]", "is_verified": false, "password": "...", "username": "jodi" } ], "page": 1, "per_page": 3, "total": 1}如何刪除密碼字段?我應該如何處理此代碼?q := c.Query("q")perPage, err := strconv.Atoi(c.Query("per_page"))if err != nil || perPage < 1 { perPage = 10}page, err := strconv.Atoi(c.Query("page"))if err != nil || page < 1 { page = 1}startIndex, err := strconv.Atoi(c.Query("page"))if err != nil || startIndex < 1 { startIndex = 0} else if startIndex > 0 && page < 1 { startIndex = (startIndex * perPage) - perPage} else { startIndex = 0}matchStage := bson.D{{"$match", bson.D{{}}}}if q != "" { matchStage = bson.D{ {"$match", bson.D{ {"username", q}, }}, }}groupStage := bson.D{ {"$group", bson.D{ {"_id", bson.D{{"_id", "null"}}}, {"total", bson.D{{"$sum", 1}}}, {"data", bson.D{{"$push", "$$ROOT"}}}, {"page", bson.D{{"$first", page}}}, {"per_page", bson.D{{"$first", perPage}}}, }}}projectStage := bson.D{ {"$project", bson.D{ {"_id", 0}, {"total", 1}, {"page", 1}, {"per_page", 1}, {"data", bson.D{{"$slice", []interface{}{"$data", startIndex, perPage}}}}, }}}result, err := userCollection.Aggregate(ctx, mongo.Pipeline{ matchStage, groupStage, projectStage, },)
如何刪除 golang mongodb 組聚合中的嵌套字段?
慕蓋茨4494581
2022-09-12 16:27:40