我想user_id: 1用我在下面给出的代码将那些放在用户下面,但结果总是空的。
我没有收到任何错误,但我不完全理解我在哪里犯错误:/
*此外;
什么是bson.M{}什么bson.D{}。我不完全明白它们之间有什么区别?
type Project struct {
ID string `json:"id"`
ProjectName string `json:"project_name"`
Tags []ProjectTags `json:"tags"`
Type int `json:"type"`
Constituent string `json:"constituent"`
CoverPhoto string `json:"cover_photo"`
Ratio string `json:"ratio"`
Width string `json:"width"`
Height string `json:"height"`
User []ProjectUsers `json:"users"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
}
type ProjectTags struct {
TagName string `json:"tag_name"`
Order int `json:"order"`
}
type ProjectUsers struct {
UserID string `json:"user_id"`
}
import (
"context"
"net/http"
"github.com/gin-gonic/gin"
"go.mongodb.org/mongo-driver/bson"
)
type projectListResponse struct {
Status int `json:"status"`
Description string `json:"description"`
DatabaseMessage string `json:"database_message"`
Projects []Project `json:"projects"`
}
func ProjectList(c *gin.Context) {
projects := []Project{}
cursor, err := (context.TODO(), bson.M{"users": bson.M{"$elemMatch": bson.M{"user_id": "1"}}})
if err != nil {
c.JSON(http.StatusInternalServerError, &projectListResponse{
Status: http.StatusInternalServerError,
Description: "There is problems with listing projects",
DatabaseMessage: err.Error(),
Projects: projects,
})
return
}
}
慕桂英546537
冉冉说
相关分类