如何使用Monger更新/插入MongoDB子文档?

时间:2012-05-12 23:46:38

标签: mongodb clojure monger

我正在使用Clojure's Monger库连接到MongeoDB数据库。

我想更新,插入&删除Mongo数据库中的子文档。 MongoDB的$ push修饰符允许我在搜索文档的根目录上执行此操作。但我希望能够推进子集合。看Monger's tests,看起来很可能。但我想确保我可以推送到第三位父母的子集。 Monger可以这样做吗?


    (mgcol/update mycollection { :my-criteria-key "my-criteria-value" } { $push { "parent.3.child-collection" "fubar" }} ) 

更好的是能够在$ push中使用$ where子句。这样的事情可能吗?


    (mgcol/update mycollection 
          { :doc-criteria-key "doc-criteria-value" } 
          { $push 
              { { $where { parent.child.lastname: 'Smith' } } 
              "fubar" } } 
    )

但即使在基本级别,当我在我的repl中尝试以下命令时,我得到以下错误。

  1. “fubar”数据库肯定存在

  2. 我肯定是连接到数据库

  3. { :owner "fubar@gmail.com" }条件绝对有效;以及

  4. 我尝试了"content.1.content""content.$.content"

  5. 
        repl => (mc/update "fubar" { :owner "fubar@gmail.com" } { $push { "content.1.content" { "fu" "bar" } } } ) 
        ClassCastException clojure.lang.Var$Unbound cannot be cast to com.mongodb.DB  monger.collection/update (collection.clj:310)
        repl => 
        repl => 
        repl => (clojure.repl/pst *e)
        ClassCastException clojure.lang.Var$Unbound cannot be cast to com.mongodb.DB
                monger.collection/update (collection.clj:310)
                bkell.run.run-ring/eval2254 (NO_SOURCE_FILE:46)
                clojure.lang.Compiler.eval (Compiler.java:6406)
                clojure.lang.Compiler.eval (Compiler.java:6372)
                clojure.core/eval (core.clj:2745)
                clojure.main/repl/read-eval-print--6016 (main.clj:244)
                clojure.main/repl/fn--6021 (main.clj:265)
                clojure.main/repl (main.clj:265)
                user/eval27/acc--3869--auto----30/fn--32 (NO_SOURCE_FILE:1)
                java.lang.Thread.run (Thread.java:619)
    
    

    有没有人碰到这个并解决了它?

    谢谢

1 个答案:

答案 0 :(得分:2)

您有三个部分的问题,描述中存在一些不一致和漏洞。所以这是我最好的猜测,希望它很接近。

如果模式与您的更新请求匹配,我可以让所有三个工作,请参阅下面的test / core.clj以获取完整的详细信息。

第一部分:是的,你可以按照你所写的那样推送到第三个父母的子集合。

第二部分:您希望将“$ where”子句移动到条件中,并在objNew中使用$。

第三部分:是的,您的基本更新对我来说非常适合我,正如您所写的那样。

“lein test”的输出位于底部。在你的努力中,你将一切顺利。

测试/ core.clj

(ns free-11749-clojure-subdoc.test.core
  (:use [free-11749-clojure-subdoc.core])
  (:use [clojure.test])
  (:require [monger.core :as mg] [monger.collection :as mgcol] [monger.query])
  (:use [monger.operators])
  (:import [org.bson.types ObjectId] [com.mongodb DB WriteConcern]))

(deftest monger-sub-document

    (mg/connect!)
    (mg/set-db! (mg/get-db "test"))

    (def mycollection "free11749")

    ;; first part
    (mgcol/remove mycollection)
    (is (= 0 (mgcol/count mycollection)))

    (def doc1 {
        :my-criteria-key "my-criteria-value"
        :parent [
                { :child-collection [ "cc0" ] }
                { :child-collection [ "cc1" ] }
                { :child-collection [ "cc2" ] }
                { :child-collection [ "cc3" ] }
                { :child-collection [ "cc4" ] }
            ]
        }
    )

    (mgcol/insert mycollection doc1)
    (is (= 1 (mgcol/count mycollection)))

    (mgcol/update mycollection { :my-criteria-key "my-criteria-value" } { $push { "parent.3.child-collection" "fubar" }} )

    (def mymap1 (first (mgcol/find-maps mycollection { :my-criteria-key "my-criteria-value" })))
    (is (= "fubar" (peek (:child-collection (get (:parent mymap1) 3)))))

    (prn (mgcol/find-maps mycollection { :my-criteria-key "my-criteria-value" }))


    ;; second part
    (mgcol/remove mycollection)
    (is (= 0 (mgcol/count mycollection)))

    (def doc2 {
        :doc-criteria-key "doc-criteria-value"
        :parent [
                    { :child  { :lastname [ "Alias" ] } }
                    { :child  { :lastname [ "Smith" ] } }
                    { :child  { :lastname [ "Jones" ] } }
                ]
        }
    )

    (mgcol/insert mycollection doc2)
    (is (= 1 (mgcol/count mycollection)))

    (mgcol/update mycollection { :doc-criteria-key "doc-criteria-value" "parent.child.lastname" "Smith"} { $push { :parent.$.child.lastname "fubar" } } )

    (def mymap2 (first (mgcol/find-maps mycollection { :doc-criteria-key "doc-criteria-value" })))
    (is (= "fubar" (peek (:lastname (:child (get (:parent mymap2) 1))))))

    (prn (mgcol/find-maps mycollection { :doc-criteria-key "doc-criteria-value" }))

    ;; third part
    (mgcol/remove "fubar")
    (is (= 0 (mgcol/count "fubar")))

    (def doc3 {
            :owner "fubar@gmail.com"
            :content [
                    { :content [ "cc0" ] }
                    { :content [ "cc1" ] }
                    { :content [ "cc2" ] }
            ]
        }
    )

    (mgcol/insert "fubar" doc3)
    (is (= 1 (mgcol/count "fubar")))

    (mgcol/update "fubar" { :owner "fubar@gmail.com" } { $push { "content.1.content" { "fu" "bar" } } } )

    (def mymap3 (first (mgcol/find-maps "fubar" { :owner "fubar@gmail.com" })))
    (is (= { :fu "bar" } (peek (:content (get (:content mymap3) 1)))))

    (prn (mgcol/find-maps "fubar" { :owner "fubar@gmail.com" }))
)
莱因测试

Testing free-11749-clojure-subdoc.test.core
({:_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"]}]})
({:_id #<ObjectId 4fb3e98447281968f7d42cad>, :doc-criteria-key "doc-criteria-value", :parent [{:child {:lastname ["Alias"]}} {:child {:lastname ["Smith" "fubar"]}} {:child {:lastname ["Jones"]}}]})
({:_id #<ObjectId 4fb3e98447281968f7d42cae>, :content [{:content ["cc0"]} {:content ["cc1" {:fu "bar"}]} {:content ["cc2"]}], :owner "fubar@gmail.com"})

Ran 1 tests containing 9 assertions.
0 failures, 0 errors.