MongoDB过滤嵌套数组

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

MongoDB filter nested array

问题

  1. [
  2. {
  3. "_id": 1,
  4. "title": "dummy title",
  5. "settings": [
  6. {
  7. "type": "light",
  8. "status": "enabled"
  9. },
  10. {
  11. "type": "toolbar",
  12. "status": "enabled"
  13. }
  14. ]
  15. }
  16. ]

我想按ID获取它,但只想获取“enabled”设置,不确定聚合管道应该是什么样子的。

我应该使用Java / Kotlin中的mongoTemplate创建查询,但仅有Mongo查询即可。

英文:

I have a collection that looks something like:

  1. [
  2. {
  3. "_id": 1,
  4. "title": "dummy title",
  5. "settings": [
  6. {
  7. "type": "light",
  8. "status": "enabled"
  9. },
  10. {
  11. "type": "flare",
  12. "status": "disabled"
  13. },
  14. {
  15. "type": "toolbar",
  16. "status": "enabled"
  17. }
  18. ]
  19. }
  20. ]

I wanna fetch it by Id but only with the "enabled" settings and not sure how the aggregation pipeline should look like.

I supposed to create the query using mongoTemplate in Java / Kotlin but even just the mongo query would be enough.

答案1

得分: 1

  1. // MongoDB shell/CLI版本4.26的工作代码(在Windows 10机器/操作系统上)
  2. > db.titles.find().pretty();
  3. {
  4. "_id" : 1,
  5. "title" : "虚拟标题",
  6. "settings" : [
  7. {
  8. "type" : "light",
  9. "status" : "已启用"
  10. },
  11. {
  12. "type" : "flare",
  13. "status" : "已禁用"
  14. },
  15. {
  16. "type" : "toolbar",
  17. "status" : "已启用"
  18. }
  19. ]
  20. }
  21. > db.titles.aggregate([
  22. ... {$unwind:"$settings"},
  23. ... {$match:{"settings.status":"已启用"}}
  24. ... ]);
  25. { "_id" : 1, "title" : "虚拟标题", "settings" : { "type" : "light", "status" : "已启用" } }
  26. { "_id" : 1, "title" : "虚拟标题", "settings" : { "type" : "toolbar", "status" : "已启用" } }
  27. > print("MongoDB: ",db.version());
  28. MongoDB: 4.2.6
  29. >
英文:
  1. //working code from MongoDB shell/CLI version 4.26(on windows 10 machine/OS)
  2. > db.titles.find().pretty();
  3. {
  4. "_id" : 1,
  5. "title" : "dummy title",
  6. "settings" : [
  7. {
  8. "type" : "light",
  9. "status" : "enabled"
  10. },
  11. {
  12. "type" : "flare",
  13. "status" : "disabled"
  14. },
  15. {
  16. "type" : "toolbar",
  17. "status" : "enabled"
  18. }
  19. ]
  20. }
  21. > db.titles.aggregate([
  22. ... {$unwind:"$settings"},
  23. ... {$match:{"settings.status":"enabled"}}
  24. ... ]);
  25. { "_id" : 1, "title" : "dummy title", "settings" : { "type" : "light", "status" : "enabled" } }
  26. { "_id" : 1, "title" : "dummy title", "settings" : { "type" : "toolbar", "status" : "enabled" } }
  27. > print("MongoDB: ",db.version());
  28. MongoDB: 4.2.6
  29. >

答案2

得分: 1

你可以使用$filter来实现。

  1. db.collection.aggregate([
  2. {
  3. $project: {
  4. _id: 1,
  5. title: 1,
  6. settings: {
  7. $filter: {
  8. input: "$settings",
  9. as: "item",
  10. cond: {
  11. $eq: [
  12. "$$item.status",
  13. "enabled"
  14. ]
  15. }
  16. }
  17. }
  18. }
  19. }
  20. ])

在此链接中尝试。

英文:

You can do it with a $filter

  1. db.collection.aggregate([
  2. {
  3. $project: {
  4. _id: 1,
  5. title: 1,
  6. settings: {
  7. $filter: {
  8. input: "$settings",
  9. as: "item",
  10. cond: {
  11. $eq: [
  12. "$$item.status",
  13. "enabled"
  14. ]
  15. }
  16. }
  17. }
  18. }
  19. }
  20. ])

try it here

huangapple
  • 本文由 发表于 2020年10月12日 00:03:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/64306166.html
匿名

发表评论

匿名网友

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

确定