用于mongodb文档更新问题的clojure数据结构

时间:2012-04-18 10:31:47

标签: mongodb clojure

我有一个具有以下结构的mongodb文档..

{

    "key4" : 

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

当我用给定的结构更新此文档时。

{

    "key4" : 

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

我需要输出为..

{

"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"}
         ]
}

这意味着如果新数组中存在'k1'的值,它应该将数组的唯一对象合并为new。 我正在clojure map中实现这个结构。

2 个答案:

答案 0 :(得分:1)

使用merge-withmapmerge

的组合可以非常接近
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> 

然后你需要合并更长的集合中的任何项目。

答案 1 :(得分:0)

在您的原始问题中,目前尚不清楚您是否正在寻找主要针对Clojure或针对MongoDB答案的答案。我将接受面向MongoDB的答案的挑战。

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

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

如果您想利用MongoDB upsert获取单个更新/插入请求,则可以取消嵌入/展平架构,但是您必须阅读多个文档。

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

(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))
)