Mongo-go-driver嵌套查询golang

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

Mongo-go-driver nested query golang

问题

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

  1. filter = bson.M{
  2. "$and": []bson.M{
  3. {"partnerA.id": id},
  4. {"unlocked": false},
  5. {"deletedAt": nil},
  6. },
  7. }
  1. filter = bson.M{
  2. "$and": []bson.M{
  3. {"partnerB.id": id},
  4. {"unlocked": false},
  5. {"deletedAt": nil},
  6. },
  7. }

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

  1. filter := bson.M{
  2. "$and": []bson.M{
  3. {"partnerA.id": id},
  4. {"unlocked": false},
  5. {"deletedAt": nil},
  6. },
  7. "$or": bson.A{
  8. bson.M{"$and": []bson.M{
  9. {"partnerB.id": id},
  10. {"unlocked": false},
  11. {"deletedAt": nil},
  12. }},
  13. },
  14. }

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

谢谢。

英文:

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.

  1. filter = bson.M{
  2. "$and": []bson.M{
  3. {"partnerA.id": id},
  4. {"unlocked": false},
  5. {"deletedAt": nil},
  6. },
  7. }
  1. filter = bson.M{
  2. "$and": []bson.M{
  3. {"partnerB.id": id},
  4. {"unlocked": false},
  5. {"deletedAt": nil},
  6. },
  7. }

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

  1. filter := bson.M{
  2. "$and": []bson.M{
  3. {"partnerA.id": id},
  4. {"unlocked": false},
  5. {"deletedAt": nil},
  6. },
  7. "$or": bson.A{
  8. bson.M{"$and": []bson.M{
  9. {"partnerB.id": id},
  10. {"unlocked": false},
  11. {"deletedAt": nil},
  12. }},
  13. },
  14. }

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

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

  1. filter := bson.M{
  2. "$or": []bson.M{
  3. {"partnerA.id": id},
  4. {"partnerB.id": id},
  5. },
  6. "unlocked": false,
  7. "deletedAt": nil,
  8. }

更新 #1

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

  1. filter := bson.M{
  2. "$or": []bson.M{
  3. {
  4. "partnerA.id": id,
  5. "partnerA.unlocked": true,
  6. },
  7. {
  8. "partnerB.id": id,
  9. "partnerB.unlocked": true,
  10. },
  11. },
  12. }
英文:

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:

  1. filter := bson.M{
  2. "$or": []bson.M{
  3. {"partnerA.id": id},
  4. {"partnerB.id": id},
  5. },
  6. "unlocked": false,
  7. "deletedAt": nil,
  8. }

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

  1. filter := bson.M{
  2. "$or": []bson.M{
  3. {
  4. "partnerA.id": id,
  5. "partnerA.unlocked": true,
  6. },
  7. {
  8. "partnerB.id": id,
  9. "partnerB.unlocked": true,
  10. },
  11. },
  12. }

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:

确定