Guest User

Untitled

a guest
Sep 14th, 2018
408
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.65 KB | None | 0 0
  1. How to update / insert a MongoDB sub document with Monger?
  2. { :owner "fubar@gmail.com" }
  3.  
  4. { :owner "fubar@gmail.com" }
  5.  
  6. $push
  7.  
  8. (ns free-11749-clojure-subdoc.test.core
  9. (:use [free-11749-clojure-subdoc.core])
  10. (:use [clojure.test])
  11. (:require [monger.core :as mg] [monger.collection :as mgcol] [monger.query])
  12. (:use [monger.operators])
  13. (:import [org.bson.types ObjectId] [com.mongodb DB WriteConcern]))
  14.  
  15. (deftest monger-sub-document
  16.  
  17. (mg/connect!)
  18. (mg/set-db! (mg/get-db "test"))
  19.  
  20. (def mycollection "free11749")
  21.  
  22. ;; first part
  23. (mgcol/remove mycollection)
  24. (is (= 0 (mgcol/count mycollection)))
  25.  
  26. (def doc1 {
  27. :my-criteria-key "my-criteria-value"
  28. :parent [
  29. { :child-collection [ "cc0" ] }
  30. { :child-collection [ "cc1" ] }
  31. { :child-collection [ "cc2" ] }
  32. { :child-collection [ "cc3" ] }
  33. { :child-collection [ "cc4" ] }
  34. ]
  35. }
  36. )
  37.  
  38. (mgcol/insert mycollection doc1)
  39. (is (= 1 (mgcol/count mycollection)))
  40.  
  41. (mgcol/update mycollection { :my-criteria-key "my-criteria-value" } { $push { "parent.3.child-collection" "fubar" }} )
  42.  
  43. (def mymap1 (first (mgcol/find-maps mycollection { :my-criteria-key "my-criteria-value" })))
  44. (is (= "fubar" (peek (:child-collection (get (:parent mymap1) 3)))))
  45.  
  46. (prn (mgcol/find-maps mycollection { :my-criteria-key "my-criteria-value" }))
  47.  
  48.  
  49. ;; second part
  50. (mgcol/remove mycollection)
  51. (is (= 0 (mgcol/count mycollection)))
  52.  
  53. (def doc2 {
  54. :doc-criteria-key "doc-criteria-value"
  55. :parent [
  56. { :child { :lastname [ "Alias" ] } }
  57. { :child { :lastname [ "Smith" ] } }
  58. { :child { :lastname [ "Jones" ] } }
  59. ]
  60. }
  61. )
  62.  
  63. (mgcol/insert mycollection doc2)
  64. (is (= 1 (mgcol/count mycollection)))
  65.  
  66. (mgcol/update mycollection { :doc-criteria-key "doc-criteria-value" "parent.child.lastname" "Smith"} { $push { :parent.$.child.lastname "fubar" } } )
  67.  
  68. (def mymap2 (first (mgcol/find-maps mycollection { :doc-criteria-key "doc-criteria-value" })))
  69. (is (= "fubar" (peek (:lastname (:child (get (:parent mymap2) 1))))))
  70.  
  71. (prn (mgcol/find-maps mycollection { :doc-criteria-key "doc-criteria-value" }))
  72.  
  73. ;; third part
  74. (mgcol/remove "fubar")
  75. (is (= 0 (mgcol/count "fubar")))
  76.  
  77. (def doc3 {
  78. :owner "fubar@gmail.com"
  79. :content [
  80. { :content [ "cc0" ] }
  81. { :content [ "cc1" ] }
  82. { :content [ "cc2" ] }
  83. ]
  84. }
  85. )
  86.  
  87. (mgcol/insert "fubar" doc3)
  88. (is (= 1 (mgcol/count "fubar")))
  89.  
  90. (mgcol/update "fubar" { :owner "fubar@gmail.com" } { $push { "content.1.content" { "fu" "bar" } } } )
  91.  
  92. (def mymap3 (first (mgcol/find-maps "fubar" { :owner "fubar@gmail.com" })))
  93. (is (= { :fu "bar" } (peek (:content (get (:content mymap3) 1)))))
  94.  
  95. (prn (mgcol/find-maps "fubar" { :owner "fubar@gmail.com" }))
  96. )
  97.  
  98. Testing free-11749-clojure-subdoc.test.core
  99. ({:_id #<ObjectId 4fb3e98447281968f7d42cac>, :my-criteria-key "my-criteria-value", :parent [{:child-collection ["cc0"]} {:child-collection ["cc1"]} {:child-collection ["cc2"]} {:child-collection ["cc3" "fubar"]} {:child-collection ["cc4"]}]})
  100. ({:_id #<ObjectId 4fb3e98447281968f7d42cad>, :doc-criteria-key "doc-criteria-value", :parent [{:child {:lastname ["Alias"]}} {:child {:lastname ["Smith" "fubar"]}} {:child {:lastname ["Jones"]}}]})
  101. ({:_id #<ObjectId 4fb3e98447281968f7d42cae>, :content [{:content ["cc0"]} {:content ["cc1" {:fu "bar"}]} {:content ["cc2"]}], :owner "fubar@gmail.com"})
  102.  
  103. Ran 1 tests containing 9 assertions.
  104. 0 failures, 0 errors.
Add Comment
Please, Sign In to add comment