1. 程式人生 > >MongoDB中級---->關聯多表查詢

MongoDB中級---->關聯多表查詢

http://www.linuxidc.com/Linux/2011-08/41043.htm

DBRef is a more formal specification for creating references between documents.  DBRefs (generally) include a collection name as well as an object id.  Most developers only use DBRefs if the collection can change from one document to the next.  If your referenced collection will always be the same, the manual references outlined above are more efficient.

  1. ^_^[[email protected]:/usr/local/mongodb/bin]#./mongo  
  2. MongoDB shell version: 1.8.2  
  3. connecting to: test  
  4. > var a = {name:"C++"}                                                                                             
  5. > db   
  6. test  
  7. > db.language.save(a)  
  8. > db.language.find()  
  9. "_id" : ObjectId("4da32c897d2de864e0448e06"
    ), "name" : "C++" }  
  10. > var b = {name:"javascript"}  
  11. > db.language.save(b)  
  12. > db.language.find()  
  13. "_id" : ObjectId("4da32c897d2de864e0448e06"), "name" : "C++" }  
  14. "_id" : ObjectId("4da32cb17d2de864e0448e07"), "name" : "javascript" }  
  15. > lan = {name:"obj1",computer:[new DBRef('language',a._id)]}  
  16. {  
  17.         "name" : "obj1",  
  18.         "computer" : [  
  19.                 {  
  20.                         "$ref" : "language",  
  21.                         "$id" : ObjectId("4da32c897d2de864e0448e06")  
  22.                 }  
  23.         ]  
  24. }  
  25. > lan.computer[0]  
  26. "$ref" : "language""$id" : ObjectId("4da32c897d2de864e0448e06") }  
  27. > lan.computer[0].fetch()  
  28. "_id" : ObjectId("4da32c897d2de864e0448e06"), "name" : "C++" }  
  29. > db.language.insert(lan)                                                                                          
  30. > db.language.find()  
  31. "_id" : ObjectId("4da32c897d2de864e0448e06"), "name" : "C++" }  
  32. "_id" : ObjectId("4da32cb17d2de864e0448e07"), "name" : "javascript" }  
  33. "_id" : ObjectId("4da33b487d2de864e0448e08"), "name" : "obj1""computer" : [ { "$ref" : "language""$id" : ObjectId("4da32c897d2de864e0448e06") } ] }  
  34. > db.language.findOne({name:"obj1"}).computer[0].fetch()                                                           
  35. "_id" : ObjectId("4da32c897d2de864e0448e06"), "name" : "C++" }  
  36. > lan2 = {name:"obj2",computer:[new DBRef('language',b._id)]}       
  37. {  
  38.         "name" : "obj2",  
  39.         "computer" : [  
  40.                 {  
  41.                         "$ref" : "language",  
  42.                         "$id" : ObjectId("4da32cb17d2de864e0448e07")  
  43.                 }  
  44.         ]  
  45. }  
  46. > db.language.insert(lan2)  
  47. > db.language.find()  
  48. "_id" : ObjectId("4da32c897d2de864e0448e06"), "name" : "C++" }  
  49. "_id" : ObjectId("4da32cb17d2de864e0448e07"), "name" : "javascript" }  
  50. "_id" : ObjectId("4da33b487d2de864e0448e08"), "name" : "obj1""computer" : [ { "$ref" : "language""$id" : ObjectId("4da32c897d2de864e0448e06") } ] }