验证中...
语言: Clojure
最后更新于 2017-07-27 18:36
test.clj
原始数据 复制代码
(ns blog.Model (:gen-class)
(:require [monger.core :as mg] [monger.collection :as mc] [monger.operators :refer :all]
[clojure.string :as Str])
(:import org.bson.types.ObjectId)
)
(defn add-model [db src-path add-value]
(let [{bson-type :type coll :coll val :val} (find-bson db src-path)]
(case bson-type
"map"
(case (:type add-value)
"map" (let [retmap (mc/insert-and-return db coll {})]
(mc/update db coll
{:_id (:_id val)}
{$set {(:key add-value) {:type "map" :ref (:_id retmap)} }}
{:upsert true}
)
)
"array" (mc/update db coll
{:_id (:_id val)}
{$set {(:key add-value) {:type "array" :ref (str (:key add-value) (ObjectId.))}}}
{:upsert true}
)
)
"array" (println "not supper now!")
)
)
)
(defn find-bson [db str-path]
(let [arr (clojure.string/split str-path #"\.") root (mc/find-one-as-map db "root" {:name "root"})]
(loop [lst-elem (next arr) bson-map {:type "map" :coll "root" :val root}]
(if (nil? lst-elem)
bson-map
(recur (next lst-elem) (get-bson db bson-map (first lst-elem)) )
)
)
)
)
(defn- get-bson [db parent-bson-map str-desc]
(if (Str/includes? str-desc "#")
(println "is array elem")
(let [curElem ((keyword str-desc) (parent-bson-map :val)) coll (:coll parent-bson-map)]
{:type "map" :coll coll :val (mc/find-one-as-map db coll {:_id (curElem :ref)})}
)
)
)
(defn test-db []
(let [conn (mg/connect) db (mg/get-db conn "ClojureBlog")]
;(add-model db "root.setting" {:type "map" :key "normalconfig"})
;(add-model db "root.setting" {:type "array" :key "doc-list"})
(mg/disconnect conn)
)
)
(test-db)
(defn update-model [src-path up-value])
(add-model "root.setting" {:type "map" :key "setting"})
(add-model "root.doc-list#111" {:type "array"})
(update-model "root.setting" {:site-name "yangdao"})
(update-model "root.doc-list" {:title "ssss" :context "aaaaa"})
(update-model "root.doc-list#11111" {:title "biubiubiubiu"})
#_(
root collection
{:_id 00001 :name "root" :setting {:type "map" :ref 1111111} :doc-list {:type "array" :ref "2323232"}}
{:_id 1111111 :site-name "are you ok?" :addr "moon"}
2323232 collection
{:title "doc title" :context "biu biu biu biu biu biu asdfghjk"}
{
:setting {:site-name "aaa" :title "aha"}
:doc-list [
{:_id 11 :title "qqqq" :context "aaaaaaaa"}
{:_id 22 :title "bbbb" :context "cccccccc"}
]
}
;root.setting
;root.doclist#11
;
)

评论列表( 0 )

你可以在登录后,发表评论

搜索帮助