1

I have a mongodb document with below structure..

{

    "key4" : 

         [
           {"k1":"v1", "k2":"va1", "k3":"value1"},         
           {"k1":"v2", "k2":"va2", "k4":"name"},
           {"k1":"v3", "k2":"va3"}
         ]
}

when i was updating this document with given structure..

{

    "key4" : 

         [
           {"k1":"v1", "k3":"val1"},         
           {"k1":"v2", "k3":"val2"},
           {"k1":"v3", "k3":"val3"},
           {"k1":"v4", "k3":"val4"}
         ]
}

i need output as..

{

"key4" : 

         [
           {"k1":"v1", "k2":"va1", "k3":"val1"},         
           {"k1":"v2", "k2":"va2", "k3":"val2", "k4":"name"},
           {"k1":"v3", "k2":"va3", "k3":"val3"},
           {"k1":"v4", "k3":"val4"}
         ]
}

which means that if value of 'k1' exist in the new array, it should merge the only object of array into new. i am implementing this structure in clojure map.

4

2 回答 2

1

您可以通过 、 和 的组合merge-with非常map接近merge

user> a
{"key4" [{"k1" "v1", "k2" "va1", "k3" "value1"} 
         {"k1" "v2", "k2" "va2", "k4" "name"} 
         {"k1" "v3", "k2" "va3"}]}
user> b
{"key4" [{"k1" "v1", "k3" "val1"} 
         {"k1" "v2", "k3" "val2"} 
         {"k1" "v3", "k3" "val3"} 
         {"k1" "v4", "k3" "val4"}]}
user> (merge-with #(map merge %1 %2) a b)
{"key4" 
    ({"k1" "v1", "k2" "va1", "k3" "val1"} 
     {"k1" "v2", "k2" "va2", "k3" "val2", "k4" "name"} 
     {"k1" "v3", "k2" "va3", "k3" "val3"})}
user> 

那么您需要合并较长集合中的任何项目。

于 2012-04-18T22:15:26.250 回答
0

在您最初的问题中,尚不清楚您是在寻找主要针对 Clojure 还是针对 MongoDB 答案的答案。我将接受针对 MongoDB 的答案的挑战。

根据您是否关心读取或更新性能,有一些权衡

您提供的架构嵌入了一个数组,该数组读取速度快但更新复杂,并且需要检查结果和随后的第二次更新以推送/追加。

如果您想利用 MongoDB upsert 进行单个更新/插入请求,则可以取消嵌入/展平模式,但随后您将不得不阅读多个文档。

请在下面找到这两种方法的 Clojure/monger 代码,使用 monger [com.novemberain/monger "1.0.0-beta4"] 和 Leiningen。

(ns FREE-10649-clojure.test.core
  (:use [FREE-10649-clojure.core])
  (:use [clojure.test])
  (:require [monger.core] [monger.collection] [monger.query])
  (:use [monger.operators])
  (:import [org.bson.types ObjectId] [com.mongodb DB WriteConcern]))

;; some operators are missing from monger
(defmacro ^{:private true} defoperator
  [operator]
    (let [op# (str operator)
       op-sym# (symbol op#)]
      `(def ~op-sym# ~op#)))

(defoperator $exists)
(defoperator $mod)
(defoperator $size)
(defoperator $type)
(defoperator $not)

(deftest mytest

    (def initial_doc {
        "key4" [
                { "k1" "v1", "k2" "va1", "k3" "value1" }
                { "k1" "v2", "k2" "va2", "k4" "name" }
                { "k1" "v3", "k2" "va3" }
            ]
        }
    )
    (def data_to_update {
        "key4" [
                { "k1" "v1", "k3" "val1" }
                { "k1" "v2", "k3" "val2" }
                { "k1" "v3", "k3" "val3" }
                { "k1" "v4", "k3" "val4" }
            ]
        }
    )
    (def expected_doc {
        "key4" [
                { "k1" "v1", "k2" "va1", "k3" "val1" }
                { "k1" "v2", "k2" "va2", "k3" "val2", "k4" "name" }
                { "k1" "v3", "k2" "va3", "k3" "val3" }
                { "k1" "v4", "k3" "val4" }
            ]
        }
    )

    (monger.core/connect!)
    (monger.core/set-db! (monger.core/get-db "test"))
    (def coll-name "free10649")

    ;; user-supplied schema
    (monger.collection/remove coll-name)
    (is (= 0 (monger.collection/count coll-name)))

    (monger.collection/insert coll-name initial_doc)
    (is (= 1 (monger.collection/count coll-name)))

    (doseq [outer_key (keys data_to_update)]
      (def array_of_updates (data_to_update outer_key))
      (doseq [object_of_updates array_of_updates]
        (def k1 "k1")
        (def k3 "k3")
        (def v1 (object_of_updates k1))
        (def v3 (object_of_updates k3))
        (def query { outer_key { $elemMatch { k1 v1 } } })
        (def update { $set { (str outer_key ".$." k3) v3 } })
        (monger.collection/update coll-name query update)
        (if (false? (monger.result/updated-existing? (monger.core/get-last-error)))
          (monger.collection/update coll-name { outer_key { $exists true } } { $push { outer_key object_of_updates } })
        )
      )
    )
    (prn "user-supplied-schema updated document")
    (prn (monger.collection/find-maps coll-name))

    ;; flattened schema
    (monger.collection/remove coll-name)
    (is (= 0 (monger.collection/count coll-name)))

    (doseq [outer_key (keys initial_doc)]
      (def array_of_docs (initial_doc outer_key))
      (doseq [doc array_of_docs]
        (monger.collection/insert coll-name (merge { outer_key true } doc))
      )
    )
    (prn "flattened-schema initial documents")
    (prn (monger.collection/find-maps coll-name))

    (monger.collection/ensure-index coll-name { "key4" 1 "k1" 1 })
    ;;(monger.collection/explain (monger.collection/find coll-name { "key4" { $exists true } })) ;; missing explain in monger(?)

    (doseq [outer_key (keys data_to_update)]
      (def array_of_updates (data_to_update outer_key))
      (doseq [object_of_updates array_of_updates]
        (def k1 "k1")
        (def v1 (object_of_updates k1))
        (def query { outer_key { $exists true } k1 v1 })
        (def update { $set (merge { outer_key true } object_of_updates) })
        (monger.collection/update coll-name query update :upsert true)
      )
    )

    (prn "flattened-schema updated documents")
    (prn (monger.collection/find-maps coll-name))
)
于 2012-04-20T19:01:43.533 回答