$elemMatch

语法

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

描述

选择集合中“<字段名>”匹配指定“{ <子字段>: <表达式> … }”的记录。

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

示例

在集合 foo.bar 中插入 3 条记录:

  1. > db.foo.bar.insert( { "id": 1, "content": { "name": "Jack", "phone": "123", "address": "1000 Market Street, Philadelphia" } } )
  2. > db.foo.bar.insert( { "id": 2, "content": [ { "name": "Tom", "phone": "456", "address": "2000 Market Street, Philadelphia" } ] } )
  3. > db.foo.bar.insert( { "id": 3, "content": { "name": "Septem", "phone": "789", "address": { "addr1": "3000 Market Street, Philadelphia", "addr2": "4000 Market Street, Philadelphia" } } } )
  4. > db.foo.bar.find()
  5. {
  6. "_id": {
  7. "$oid": "5a73ce416a3e18f64e000010"
  8. },
  9. "id": 1,
  10. "content": {
  11. "name": "Jack",
  12. "phone": "123",
  13. "address": "1000 Market Street, Philadelphia"
  14. }
  15. }
  16. {
  17. "_id": {
  18. "$oid": "5a73ce476a3e18f64e000011"
  19. },
  20. "id": 2,
  21. "content": [
  22. {
  23. "name": "Tom",
  24. "phone": "456",
  25. "address": "2000 Market Street, Philadelphia"
  26. }
  27. ]
  28. }
  29. {
  30. "_id": {
  31. "$oid": "5aaa25ee48bcff191e000002"
  32. },
  33. "id": 3,
  34. "content": {
  35. "name": "Septem",
  36. "phone": "789",
  37. "address": {
  38. "addr1": "3000 Market Street, Philadelphia",
  39. "addr2": "4000 Market Street, Philadelphia"
  40. }
  41. }
  42. }
  43. Return 3 row(s).

SequoiaDB shell 运行如下:

  • 嵌套对象匹配:
  1. > db.foo.bar.find( { "content": { $elemMatch: { "name": "Jack", "phone": "123" } } } )
  2. {
  3. "_id": {
  4. "$oid": "5822868a2b4c38286d000007"
  5. },
  6. "id": 1,
  7. "content": {
  8. "name": "Jack",
  9. "phone": "123",
  10. "address": "1000 Market Street, Philadelphia"
  11. }
  12. }
  13. Return 1 row(s).
  • 数组匹配:
  1. > db.foo.bar.find( { "content": { $elemMatch: { "name": "Tom", "phone": "456" } } } )
  2. {
  3. "_id": {
  4. "$oid": "5822868a2b4c38286d000008"
  5. },
  6. "id": 2,
  7. "content": [
  8. {
  9. "name": "Tom",
  10. "phone": "456",
  11. "address": "2000 Market Street, Philadelphia"
  12. }
  13. ]
  14. }
  15. Return 1 row(s).
  • 匹配“content”字段中子字段“phone”符合表达式“ $lte:"123" ”的记录:
  1. > db.foo.bar.find( { content: { $elemMatch: { "phone" : { $lte : "123" } } } } )
  2. {
  3. "_id": {
  4. "$oid": "5a0106e51f9b983f4600000b"
  5. },
  6. "id": 1,
  7. "content": {
  8. "name": "Jack",
  9. "phone": "123",
  10. "address": "1000 Market Street, Philadelphia"
  11. }
  12. }
  13. Return 1 row(s).
  • 使用“$regex”匹配符以及嵌套的“$elemMatch”匹配符:
  1. > db.foo.bar.find( { "content" : { $elemMatch : { address : { $elemMatch: { addr1 : { $regex : ".*Philadelphia$" } } } } } } )
  2. {
  3. "_id": {
  4. "$oid": "5a0107641f9b983f4600000d"
  5. },
  6. "id": 3,
  7. "content": {
  8. "name": "Septem",
  9. "phone": "789",
  10. "address": {
  11. "addr1": "3000 Market Street, Philadelphia",
  12. "addr2": "4000 Market Street, Philadelphia"
  13. }
  14. }
  15. }
  16. Return 1 row(s).