当前位置:Gxlcms > mysql > mongodb查询

mongodb查询

时间:2021-07-01 10:21:17 帮助过:14人阅读

这节来说说如何检索mongodb数据。首先向文档中插入一些数据。1. 插入数据 use ttlsa_comswitched to db ttlsa_com db.mediaCollection.insert({ "Type" : "Book", "Title" : "Definitive Guide to MongoDB, the", "ISBN" : "987-1-4302-3051-9", "Publisher"

这节来说说如何检索mongodb数据。首先向文档中插入一些数据。 1. 插入数据
  1. > use ttlsa_com
  2. switched to db ttlsa_com
  3. > db.mediaCollection.insert({ "Type" : "Book", "Title" : "Definitive Guide to MongoDB, the", "ISBN" : "987-1-4302-3051-9", "Publisher" : "Apress", "Author": [ "Membrey, Peter", "Plugge, Eelco", "Hawkins, Tim" ] })
  4. > db.mediaCollection.insert({ "Type" : "CD", "Artist" : "Nirvana", "Title" : "Nevermind" })
  5. > db.mediaCollection.insert({ "Type" : "CD", "Artist" : "Nirvana", "Title" : "Nevermind", "Tracklist" : [ { "Track" : "1", "Title" : "Smells like teen spirit", "Length" : "5:02" }, { "Track" : "2", "Title" : "In Bloom", "Length" : "4:15" } ]})
  6. > db.mediaCollection.find()
  7. { "_id" : ObjectId("5353462f93efef02c962da71"), "Type" : "Book", "Title" : "Definitive Guide to MongoDB, the", "ISBN" : "987-1-4302-3051-9", "Publisher" : "Apress", "Author" : [ "Membrey, Peter", "Plugge, Eelco", "Hawkins, Tim" ] }
  8. { "_id" : ObjectId("5353462f93efef02c962da72"), "Type" : "CD", "Artist" : "Nirvana", "Title" : "Nevermind" }
  9. { "_id" : ObjectId("5353463193efef02c962da73"), "Type" : "CD", "Artist" : "Nirvana", "Title" : "Nevermind", "Tracklist" : [ { "Track" : "1", "Title" : "Smells like teen spirit", "Length" : "5:02" }, { "Track" : "2", "Title" : "In Bloom", "Length" : "4:15" } ] }
2. 检索 find函数是经常用到的一个。前面的文章也有介绍到。下面看看有选择性的检索,查看你感兴趣的数据。 检索"Artist" : "Nirvana"的数据:
  1. > db.mediaCollection.find({"Artist" : "Nirvana"}).toArray()
  2. [
  3. {
  4. "_id" : ObjectId("5353462f93efef02c962da72"),
  5. "Type" : "CD",
  6. "Artist" : "Nirvana",
  7. "Title" : "Nevermind"
  8. },
  9. {
  10. "_id" : ObjectId("5353463193efef02c962da73"),
  11. "Type" : "CD",
  12. "Artist" : "Nirvana",
  13. "Title" : "Nevermind",
  14. "Tracklist" : [
  15. {
  16. "Track" : "1",
  17. "Title" : "Smells like teen spirit",
  18. "Length" : "5:02"
  19. },
  20. {
  21. "Track" : "2",
  22. "Title" : "In Bloom",
  23. "Length" : "4:15"
  24. }
  25. ]
  26. }
  27. ]
上面的查询虽说检索出"Artist" : "Nirvana"的数据,但是返回了全部列的信息,但是我只要查看Title和Tracklist.Title列
  1. > db.mediaCollection.find({"Artist" : "Nirvana"}, {Title:1, "Tracklist.Title":1}).toArray()
  2. [
  3. {
  4. "_id" : ObjectId("5353462f93efef02c962da72"),
  5. "Title" : "Nevermind"
  6. },
  7. {
  8. "_id" : ObjectId("5353463193efef02c962da73"),
  9. "Title" : "Nevermind",
  10. "Tracklist" : [
  11. {
  12. "Title" : "Smells like teen spirit"
  13. },
  14. {
  15. "Title" : "In Bloom"
  16. }
  17. ]
  18. }
  19. ]
Title:1, "Tracklist.Title":1表示只返回这两列信息。升序。也可以反着来Title:0, "Tracklist.Title":0表示返回除了这两列的其他所有列信息。 注意:_id字段总是会返回。 3. ?使用逗号 当文档结构变的复杂时,如含有数组或嵌入对象文档,就需要使用到逗号,来检索嵌入在文档中的信息。
  1. > db.mediaCollection.find({"Tracklist.Length":"5:02"}).toArray()
  2. [
  3. {
  4. "_id" : ObjectId("5353463193efef02c962da73"),
  5. "Type" : "CD",
  6. "Artist" : "Nirvana",
  7. "Title" : "Nevermind",
  8. "Tracklist" : [
  9. {
  10. "Track" : "1",
  11. "Title" : "Smells like teen spirit",
  12. "Length" : "5:02"
  13. },
  14. {
  15. "Track" : "2",
  16. "Title" : "In Bloom",
  17. "Length" : "4:15"
  18. }
  19. ]
  20. }
  21. ]
查询整个内嵌文档:
  1. > db.mediaCollection.find({Tracklist:{"Length":"5:02"}}).toArray()
  2. [ ]
  3. > db.mediaCollection.find({Tracklist:{"Track" : "1","Title" : "Smells like teen spirit","Length":"5:02"}}).toArray()
  4. [
  5. {
  6. "_id" : ObjectId("5353463193efef02c962da73"),
  7. "Type" : "CD",
  8. "Artist" : "Nirvana",
  9. "Title" : "Nevermind",
  10. "Tracklist" : [
  11. {
  12. "Track" : "1",
  13. "Title" : "Smells like teen spirit",
  14. "Length" : "5:02"
  15. },
  16. {
  17. "Track" : "2",
  18. "Title" : "In Bloom",
  19. "Length" : "4:15"
  20. }
  21. ]
  22. }
  23. ]
  24. > db.mediaCollection.find({Tracklist:{"Track" : "1","Length" : "5:02","Title" : "Smells like teen spirit"}}).toArray()
  25. [ ]
查询整个文档需要全部列出内嵌文档的字段,且顺序要一致,否则匹配不到。 查询内嵌文档的多个字段。如查询有joe发表且分数在5分以上:
  1. > db.mediaCollection.insert({ "content" : "...", "comments" : [ { "author" : "joe", "score" : 3, "comment" : "nice post" }, { "author" : "mary", "score" : 6, "comment" : "terrible post" } ] })
  2. > db.mediaCollection.find().toArray()
  3. [
  4. {
  5. "_id" : ObjectId("5353462f93efef02c962da71"),
  6. "Type" : "Book",
  7. "Title" : "Definitive Guide to MongoDB, the",
  8. "ISBN" : "987-1-4302-3051-9",
  9. "Publisher" : "Apress",
  10. "Author" : [
  11. "Membrey, Peter",
  12. "Plugge, Eelco",
  13. "Hawkins, Tim"
  14. ]
  15. },
  16. {
  17. "_id" : ObjectId("5353462f93efef02c962da72"),
  18. "Type" : "CD",
  19. "Artist" : "Nirvana",
  20. "Title" : "Nevermind"
  21. },
  22. {
  23. "_id" : ObjectId("5353463193efef02c962da73"),
  24. "Type" : "CD",
  25. "Artist" : "Nirvana",
  26. "Title" : "Nevermind",
  27. "Tracklist" : [
  28. {
  29. "Track" : "1",
  30. "Title" : "Smells like teen spirit",
  31. "Length" : "5:02"
  32. },
  33. {
  34. "Track" : "2",
  35. "Title" : "In Bloom",
  36. "Length" : "4:15"
  37. }
  38. ]
  39. },
  40. {
  41. "_id" : ObjectId("5353681293efef02c962da7a"),
  42. "content" : "...",
  43. "comments" : [
  44. {
  45. "author" : "joe",
  46. "score" : 3,
  47. "comment" : "nice post"
  48. },
  49. {
  50. "author" : "mary",
  51. "score" : 6,
  52. "comment" : "terrible post"
  53. }
  54. ]
  55. }
  56. ]
  57. > db.mediaCollection.find({"comments" : {"author" : "joe", "score" : {"$gte" : 5}}}).toArray()
  58. [ ]
  59. > db.mediaCollection.find({"comments.author" : "joe", "comments.score" : {"$gte" : 5}}).toArray()
  60. [
  61. {
  62. "_id" : ObjectId("5353681293efef02c962da7a"),
  63. "content" : "...",
  64. "comments" : [
  65. {
  66. "author" : "joe",
  67. "score" : 3,
  68. "comment" : "nice post"
  69. },
  70. {
  71. "author" : "mary",
  72. "score" : 6,
  73. "comment" : "terrible post"
  74. }
  75. ]
  76. }
  77. ]
上面的查询是不对的。 要正确的指定一组条件,而不是每个键,因此要使用到$elemMatch。这样就可以用来部分指定匹配数组中的单个内嵌文档的限定条件。正确的写法如下所示:
  1. > db.mediaCollection.find({"comments" : {"$elemMatch" : {"author" : "joe", "score" : {"$gte" : 5}}}}).toArray()
  2. [ ]
对于数组:
  1. > db.mediaCollection.find({"Author":"Membrey, Peter"}).toArray()
  2. [
  3. {
  4. "_id" : ObjectId("5353462f93efef02c962da71"),
  5. "Type" : "Book",
  6. "Title" : "Definitive Guide to MongoDB, the",
  7. "ISBN" : "987-1-4302-3051-9",
  8. "Publisher" : "Apress",
  9. "Author" : [
  10. "Membrey, Peter",
  11. "Plugge, Eelco",
  12. "Hawkins, Tim"
  13. ]
  14. }
  15. ]
正则表达式查询:
  1. > db.mediaCollection.find({"Title":/MongoDB/i}).toArray()
  2. [
  3. {
  4. "_id" : ObjectId("5353462f93efef02c962da71"),
  5. "Type" : "Book",
  6. "Title" : "Definitive Guide to MongoDB, the",
  7. "ISBN" : "987-1-4302-3051-9",
  8. "Publisher" : "Apress",
  9. "Author" : [
  10. "Membrey, Peter",
  11. "Plugge, Eelco",
  12. "Hawkins, Tim"
  13. ]
  14. }
  15. ]
对检索结果进行Sort, Limit, 和Skip请看下节内容。

人气教程排行