用户选择变量

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

Variable user selection

问题

我需要为用户创建一些变化,这样他就可以选择只有那些他指定类别的用户(按类别搜索),或者那些在数组中没有相同类别的用户。我查阅了文档并找到了这个答案:$ne操作符

但是它不起作用,我得到了所有用户的列表。

func (r *Mongo) User(ctx context.Context, query *domain.Query) ([]*User, error) {
    var filter interface{}
    if query.Query != "" {
        filter = bson.D{primitive.E{Key: "status", Value: true}, primitive.E{Key: "category", Value: query.Query}}
    } else {
        filter = bson.D{primitive.E{Key: "status", Value: true}}
    } - 这个是有效的

    if query.OtherCategory {
        category := []string{"it", "medical", "sport"}
        filter = bson.M{"status": true, "category": bson.M{"$ne": category}}
    } - 这个是无效的

    cursor, err := r.col.Find(ctx, filter, opts)

    var results []*domain.User
    if err = cursor.All(ctx, &results); err != nil {
        r.logger.Error().Err(err).Msg("failed find")
        return nil, err
    }

    return results, nil
}

如何查询获取那些在数组中没有这些类别,并且状态为true的用户列表?

英文:

I need to create some variation for the user, so that he can select only those users for whom he specifies a category (search by category) or those who do not have the same categories as in the array (in the code you can see the array).
I used the documentation and found this answer: operator $ne

But it is not works, I get a list of all users

func (r *Mongo) User(ctx context.Context, query *domain.Query) ([]*User, error) {
	var filter interface{}
	if query.Query != "" {
		filter = bson.D{primitive.E{Key: "status", Value: true}, primitive.E{Key: "category", Value: query.Query}}
	} else {
		filter = bson.D{primitive.E{Key: "status", Value: true}}
	} - this works

	if query.OtherCategory {
		category := []string{"it", "medical", "sport"}
		filter = bson.M{"status": true, "category": bson.M{"$ne": category}}
	} - this it is not works

	cursor, err := r.col.Find(ctx, filter, opts)

	var results []*domain.User
	if err = cursor.All(ctx, &results); err != nil {
		r.logger.Error().Err(err).Msg("failed find")
		return nil, err
	}

	return results, nil
}

How to make a query to get a list of users who don't have these (array of categories in the code) categories in the array, and their status is true?

答案1

得分: 2

$ne("不等于")将列出用户的category字段不是给定数组的文档。

但这不是你想要的:你想列出用户中没有给定类别的用户,或者换句话说,用户的类别与给定类别的交集为空。

为此,您必须使用$nin("不在")运算符:列出类别不在给定数组中的用户。对于数组字段,将对所有数组元素进行检查。

if query.OtherCategory {
    category := []string{"it", "medical", "sport"}
    filter = bson.M{"status": true, "category": bson.M{"$nin": category}}
}
英文:

$ne ("not equal") will list you documents where the category field of the user is not the given array.

But this is not you want: you want to list users where the user has none of the given categories, or in other words the intersection of the user's categories and the given categories is empty.

For this you have to use the $nin ("not in") operator: list users where the category is not in the given array. For array fields this will be checked for all array elements.

if query.OtherCategory {
    category := []string{"it", "medical", "sport"}
    filter = bson.M{"status": true, "category": bson.M{"$nin": category}}
}

huangapple
  • 本文由 发表于 2022年9月19日 19:21:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/73772607.html
匿名

发表评论

匿名网友

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

确定