2012-05-12 9 views
1

私はMongeoDBデータベースに接続するためにClojure's Mongerライブラリを使用しています。MongoDBサブ文書をMongerで更新/挿入するにはどうすればいいですか?

私の更新するMongoデータベースのサブ文書を削除する&を挿入します。 MongoDBの$ push修飾子を使うと、検索したドキュメントのルートでこれを行うことができます。しかし、私は$をサブコレクションにプッシュできるようにしたい。 Monger's testsを見ると、これは可能です。しかし、私は第3の親の子供のコレクションに私が押し入れることができるようにしたいと思います。モンガーはこのようなことをすることができますか?

 

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

私の$プッシュで$ where句を使用することができます。このようなことは可能でしょうか?

 

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

でも基本レベルであっても、私のレプリケーションで次のコマンドを試してみると、私は以下のエラーを受け取ります。

  1. "めちゃくちゃ" データベースは、私は間違いなくDB

  2. { :owner "[email protected]" }基準に接続されています

  3. 間違いなく存在する確かに有効です。そして

  4. は私が"content.1.content""content.$.content"の両方を試してみました:

 

    repl => (mc/update "fubar" { :owner "[email protected]" } { $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) 

は、誰もがこの遭遇していたし、それを解決しましたか?

おかげ

+0

イマイチ '$のpush'

テストは、私はそれが権利はないと思う' "$プッシュ" ' – Ankur

+0

する必要があります。 [mongerのテスト](https://github.com/michaelklishin/monger/blob/master/test/monger/test/updating_test.clj)を見ると、彼らは*** $ set ***演算子を使用します'monger.operators)。私はまだそれを試み、同じエラーを持っています。 また、ホストとポートの異なる組み合わせを試しました(27017)。サイコロはありません... hmmm:/ – Nutritioustim

答えて

2

あなたが説明にいくつかの矛盾や穴に、3つの部分の質問があります。だから私の最高の推測は、それが近いことを願っています。

詳細については、以下のtest/core.cljを参照してください。

最初の部分:はい、書いたとおりに、3番目の親の子コレクションにプッシュできます。

2番目の部分: "$ where"節を条件に移動し、objNewで$を使用します。

3番目の部分:はい、あなたの基本的なアップデートは、あなたが書いたのとまったく同じように私のために働きます。

"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 "[email protected]" 
      :content [ 
        { :content [ "cc0" ] } 
        { :content [ "cc1" ] } 
        { :content [ "cc2" ] } 
      ] 
     } 
    ) 

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

    (mgcol/update "fubar" { :owner "[email protected]" } { $push { "content.1.content" { "fu" "bar" } } }) 

    (def mymap3 (first (mgcol/find-maps "fubar" { :owner "[email protected]" }))) 
    (is (= { :fu "bar" } (peek (:content (get (:content mymap3) 1))))) 

    (prn (mgcol/find-maps "fubar" { :owner "[email protected]" })) 
) 

レインテスト

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 "[email protected]"}) 

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

ホッハー、そのトリックをしました。私は[$位置オペレータ](http://www.mongodb.org/display/DOCS/Updating#Updating-The%24positionaloperator)でちょっと読まなければならなかった。しかし、私はそれよりはるかに良いです。ありがとう:) – Nutritioustim

+0

後続の質問として..あなたはサブ文書を削除する方法を知っていますか?私はこれを行うのがとても難しいです。私は、私のジレンマと[ここにはさまざまなステップ](https://groups.google.com/forum/#!topic/clojure-mongodb/f9A8_lHh46E)を概説しました。助けてくれてありがとう。 – Nutritioustim

+0

私はこの後続の問題を解決しました。 [このスレッド](https://groups.google.com/forum/#!topic/clojure-mongodb/f9A8_lHh46E)の最後に私の解決策が記載されています。お役に立てれば。 – Nutritioustim

関連する問題