如果数组包含匹配的值,则查找mgo文档。

huangapple go评论83阅读模式
英文:

Find mgo document if array contains matching value

问题

我在我的API中有以下函数,用于检查用户是否拥有相关文档:

type User struct {
    Id          bson.ObjectId   `bson:"_id,omitempty" json:"id"`
    Name        string          `form:"name" bson:"name,omitempty" json:"name,omitempty"`
    Password    string          `form:"password" bson:"password,omitempty" json:"-" binding:"required"`
    Email       string          `form:"email" bson:"email,omitempty" json:"email" binding:"required"`
    Artists     []bson.ObjectId `form:"artists" bson:"artists,omitempty" json:"artists" inline`
    ContentFeed []bson.ObjectId `form:"content_feed" bson:"content_feed,omitempty" json:"content_feed" inline`
    Location    string          `form:"user_location" bson:"user_location,omitempty" json:"user_location,omitempty"`
    TopTracks   []bson.ObjectId `form:"top_tracks" bson:"top_tracks" json:"top_tracks" inline`
    Avatar      string          `form:"avatar" bson:"avatar,omitempty" json:"avatar,omitempty"`
    BgImg       string          `form:"bg_img" bson:"bg_img,omitempty" json:"bg_img,omitempty"`
}

// Get artist
// This doesn't actually get the full artist object, this just checks that
// the artist id given is stored against the given user's list of artists
func (repo *UserRepo) GetArtist(user string, artist string) (bool, error) {
    userData := &User{}
    fmt.Println(user)
    err := repo.collection.Find(bson.M{"_id": user, "artists": bson.M{"$in": []bson.ObjectId{bson.ObjectIdHex(artist)}}}).One(&userData)

    if err != nil {
        fmt.Println(err)
        return false, err
    }

    return true, err
}

然而,尽管我给它两个肯定存在且与之相关的ID,它返回一个打印了'not found'的错误,当我检查给定用户的艺术家ID列表时。

英文:

I have the following function in my API in order to check a user owns a related document

type User struct {
	Id          bson.ObjectId   `bson:"_id,omitempty" json:"id"`
	Name        string          `form:"name" bson:"name,omitempty" json:"name,omitempty"`
	Password    string          `form:"password" bson:"password,omitempty" json:"-" binding:"required"`
	Email       string          `form:"email" bson:"email,omitempty" json:"email" binding:"required"`
	Artists     []bson.ObjectId `form:"artists" bson:"artists,omitempty" json:"artists" inline`
	ContentFeed []bson.ObjectId `form:"content_feed" bson:"content_feed,omitempty" json:"content_feed" inline`
	Location    string          `form:"user_location" bson:"user_location,omitempty" json:"user_location,omitempty"`
	TopTracks   []bson.ObjectId `form:"top_tracks" bson:"top_tracks" json:"top_tracks" inline`
	Avatar      string          `form:"avatar" bson:"avatar,omitempty" json:"avatar,omitempty"`
	BgImg       string          `form:"bg_img" bson:"bg_img,omitempty" json:"bg_img,omitempty"`
}

// Get artist
// This doesn't actual get the full artist object, this just checks that
// the artist id given is stores against the given users list of artists
func (repo *UserRepo) GetArtist(user string, artist string) (bool, error) {
	userData := &User{}
	fmt.Println(user)
	err := repo.collection.Find(bson.M{"_id": user, "artists": bson.M{"$in": []bson.ObjectId{bson.ObjectIdHex(artist)}}}).One(&userData)

	if err != nil {
		fmt.Println(err)
		return false, err
	}

	return true, err
}

However it returns an error which prints 'not found', despite giving it two ID's which definitely exist and are related when I inspect the list of artist id's for that given user.

答案1

得分: 4

也许我错了,Id 被定义为 bson.ObjectId,而你正在将其作为字符串进行查询。尝试将以下代码替换:

err := repo.collection.Find(bson.M{"_id": user, "artists": bson.M{"$in": []bson.ObjectId{bson.ObjectIdHex(artist)}}}).One(&userData)

替换为:

err := repo.collection.Find(bson.M{"_id": bson.ObjectIdHex(user), "artists": bson.M{"$in": []bson.ObjectId{bson.ObjectIdHex(artist)}}}).One(&userData)
英文:

Maybe I'm wrong but Id is defined as bson.ObjectId and you are querying it as string. Try to replace

err := repo.collection.Find(bson.M{"_id": user, "artists": bson.M{"$in": []bson.ObjectId{bson.ObjectIdHex(artist)}}}).One(&userData)

with

err := repo.collection.Find(bson.M{"_id": bson.ObjectIdHex(user), "artists": bson.M{"$in": []bson.ObjectId{bson.ObjectIdHex(artist)}}}).One(&userData)

huangapple
  • 本文由 发表于 2016年2月21日 23:33:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/35538037.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定