$elemMatch

语法

  1. { <字段名1>: { $elemMatch: <表达式1> }, <字段名2>: { $elemMatch: <表达式2> }, ... }

描述

返回数组内满足条件的元素的集合,或者嵌套对象中满足条件的子对象。

其中“<表达式>”可以是值,也可以是带有匹配符的表达式,“$elemMatch”匹配符支持多层嵌套。

示例

在集合 sample.employee 插入 3 条记录,两条是数组类型,一条是嵌套对象类型

  1. > db.sample.employee.insert( { "_id": 1, "class": 1, "students": [ { "name": "ZhangSan", "age": 18 }, { "name": "LiSi", "age": 19 }, { "name": "WangErmazi", "age": 18 } ] } )
  2. > db.sample.employee.insert( { "_id": 2, "class": 2, "students": { "name": "LinWu", "age": 18 } } )
  3. > db.sample.employee.insert( { "_id": 3, "class": 3, "students": [ { "name": "ZhangSan", "age": 18, course: [ { math: 1 }, { english: 0 } ] }, { "name": "LiSi", "age": 19, course: [ { math: 1 }, { english: 1 } ] }, { "name": "WangErmazi", "age": 18, course: [ { math: 0 }, { english: 0 } ] } ] } )
  4. > db.sample.employee.find()
  5. {
  6. "_id": 1,
  7. "class": 1,
  8. "students": [
  9. {
  10. "name": "ZhangSan",
  11. "age": 18
  12. },
  13. {
  14. "name": "LiSi",
  15. "age": 19
  16. },
  17. {
  18. "name": "WangErmazi",
  19. "age": 18
  20. }
  21. ]
  22. }
  23. {
  24. "_id": 2,
  25. "class": 2,
  26. "students": {
  27. "name": "LinWu",
  28. "age": 18
  29. }
  30. }
  31. {
  32. "_id": 3,
  33. "class": 3,
  34. "students": [
  35. {
  36. "name": "ZhangSan",
  37. "age": 18,
  38. "course": [
  39. {
  40. "math": 1
  41. },
  42. {
  43. "english": 0
  44. }
  45. ]
  46. },
  47. {
  48. "name": "LiSi",
  49. "age": 19,
  50. "course": [
  51. {
  52. "math": 1
  53. },
  54. {
  55. "english": 1
  56. }
  57. ]
  58. },
  59. {
  60. "name": "WangErmazi",
  61. "age": 18,
  62. "course": [
  63. {
  64. "math": 0
  65. },
  66. {
  67. "english": 0
  68. }
  69. ]
  70. }
  71. ]
  72. }
  73. Return 3 row(s).

SequoiaDB shell 运行如下:

  • 指定返回“age”等于 18 的元素:

    1. > db.sample.employee.find( {}, { "students": { "$elemMatch": { "age": 18 } } } )
    2. {
    3. "_id": 1,
    4. "class": 1,
    5. "students": [
    6. {
    7. "name": "ZhangSan",
    8. "age": 18
    9. },
    10. {
    11. "name": "WangErmazi",
    12. "age": 18
    13. }
    14. ]
    15. }
    16. {
    17. "_id": 2,
    18. "class": 2,
    19. "students": {
    20. "name": "LinWu",
    21. "age": 18
    22. }
    23. }
    24. {
    25. "_id": 3,
    26. "class": 3,
    27. "students": [
    28. {
    29. "name": "ZhangSan",
    30. "age": 18,
    31. "course": [
    32. {
    33. "math": 1
    34. },
    35. {
    36. "english": 0
    37. }
    38. ]
    39. },
    40. {
    41. "name": "WangErmazi",
    42. "age": 18,
    43. "course": [
    44. {
    45. "math": 0
    46. },
    47. {
    48. "english": 0
    49. }
    50. ]
    51. }
    52. ]
    53. }
    54. Return 3 row(s).
  • 指定返回“age”大于 18 的元素,使用“$gt”表达式:

    1. > db.sample.employee.find( { "class": 1 }, { "students": { "$elemMatch": { "age": { $gt: 18 } } } } )
    2. {
    3. "_id": 1,
    4. "class": 1,
    5. "students": [
    6. {
    7. "name": "LiSi",
    8. "age": 19
    9. }
    10. ]
    11. }
    12. Return 1 row(s).
  • 指定返回姓“Wang”的元素,使用“$regex”表达式:

    1. > db.sample.employee.find( { "class": 1 }, { "students": { "$elemMatch": { "name": { $regex: "^Wang.*" } } } } )
    2. {
    3. "_id": 1,
    4. "class": 1,
    5. "students": [
    6. {
    7. "name": "WangErmazi",
    8. "age": 18
    9. }
    10. ]
    11. }
    12. Return 1 row(s).
  • 指定返回 3 班学生数组中选择了数学的元素,使用嵌套的“$elemMatch”表达式:

    1. > db.sample.employee.find( { class: 3 }, { students: { $elemMatch: { course: { $elemMatch: { math: 1 } } } } } )
    2. {
    3. "_id": 3,
    4. "class": 3,
    5. "students": [
    6. {
    7. "name": "ZhangSan",
    8. "age": 18,
    9. "course": [
    10. {
    11. "math": 1
    12. },
    13. {
    14. "english": 0
    15. }
    16. ]
    17. },
    18. {
    19. "name": "LiSi",
    20. "age": 19,
    21. "course": [
    22. {
    23. "math": 1
    24. },
    25. {
    26. "english": 1
    27. }
    28. ]
    29. }
    30. ]
    31. }
    32. Return 1 row(s).