Mongo-go-driver嵌套查询golang

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

Mongo-go-driver nested query golang

问题

我曾经使用两个过滤器从我的MongoDB获取数据,但我认为这种方法不够高效,因为它需要对数据库进行两次查询。

filter = bson.M{
    "$and": []bson.M{
        {"partnerA.id": id},
        {"unlocked": false},
        {"deletedAt": nil},
    },
}
filter = bson.M{
    "$and": []bson.M{
        {"partnerB.id": id},
        {"unlocked": false},
        {"deletedAt": nil},
    },
}

我尝试使用我找到的这个解决方案将它们合并,得到了以下过滤器:

filter := bson.M{
    "$and": []bson.M{
        {"partnerA.id": id},
        {"unlocked": false},
        {"deletedAt": nil},
    },
    "$or": bson.A{
        bson.M{"$and": []bson.M{
            {"partnerB.id": id},
            {"unlocked": false},
            {"deletedAt": nil},
        }},
    },
}

然而,它并不起作用,我找不到解决办法。有人看到这个问题吗?

谢谢。

英文:

I used to have two filters to get data from my mongoDB, however I do not think that it is efficient considering it has to do two queries to the DB.


	filter = bson.M{
		"$and": []bson.M{
			{"partnerA.id": id},
			{"unlocked": false},
			{"deletedAt": nil},
		},
	}

	filter = bson.M{
		"$and": []bson.M{
			{"partnerB.id": id},
			{"unlocked": false},
			{"deletedAt": nil},
		},
	}

I tried to combine them using this solution I found and came out with this filter:

	filter := bson.M{
		"$and": []bson.M{
			{"partnerA.id": id},
			{"unlocked": false},
			{"deletedAt": nil},
		},
		"$or": bson.A{
			bson.M{"$and": []bson.M{
				{"partnerB.id": id},
				{"unlocked": false},
				{"deletedAt": nil},
			}},
		},
	}

However it does not work and I can't find the solution for it. Does anyone see the problem for this?

Thank you.

答案1

得分: 2

我假设你想使用OR运算符将这两个查询组合起来。另外,我注意到这两个查询之间有两个相似的子句,分别是"unlocked": false"deletedAt": nil

你可以使用以下更简短的查询:

filter := bson.M{
    "$or": []bson.M{
        {"partnerA.id": id},
        {"partnerB.id": id},
    },
    "unlocked": false,
    "deletedAt": nil,
}

更新 #1

> 如果我只返回满足条件的值,条件是((partnerA.id = id and partnerA.unlocked = true) or (partnerB.id = id and partnerB.unlocked = true)),那么新的查询应该如何编写?
> 引用

filter := bson.M{
    "$or": []bson.M{
        {
            "partnerA.id": id,
            "partnerA.unlocked": true,
        },
        {
            "partnerB.id": id,
            "partnerB.unlocked": true,
        },
    },
}
英文:

I presume you are trying to combine those two queries with OR operator. Another thing, I saw two similar clauses between the two, it's "unlocked": false and "deletedAt": nil.

You can have shorter query like below:

filter := bson.M{
    "$or": []bson.M{
        {"partnerA.id": id},
        {"partnerB.id": id},
    },
    "unlocked": false,
    "deletedAt": nil,
}

Update #1

> How about a new query where I only return the values if ((partnerA.id = id and partnerA.unlocked = true) or (partnerB.id = id and partnerB.unlocked = true))
> Blockquote

filter := bson.M{
    "$or": []bson.M{
        {
            "partnerA.id": id,
            "partnerA.unlocked": true,
        },
        {
            "partnerB.id": id,
            "partnerB.unlocked": true,
        },
    },
}

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

发表评论

匿名网友

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

确定