Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/clojure/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用Monger更新/插入MongoDB子文档?_Mongodb_Clojure_Monger - Fatal编程技术网

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

如何使用Monger更新/插入MongoDB子文档?,mongodb,clojure,monger,Mongodb,Clojure,Monger,我正在使用库连接到MongeoDB数据库 我想更新、插入和删除Mongo数据库中的子文档。MongoDB的$push修饰符允许我在搜索文档的根上执行此操作。但我希望能够将$push推到子集合上。现在看来,这是可能的。但我想确保我可以推到第三个家长的子集合。Monger能做这样的事吗 (mgcol/update mycollection { :my-criteria-key "my-criteria-value" } { $push { "parent.3.child-collection" "f

我正在使用库连接到MongeoDB数据库

我想更新、插入和删除Mongo数据库中的子文档。MongoDB的$push修饰符允许我在搜索文档的根上执行此操作。但我希望能够将$push推到子集合上。现在看来,这是可能的。但我想确保我可以推到第三个家长的子集合。Monger能做这样的事吗

(mgcol/update mycollection { :my-criteria-key "my-criteria-value" } { $push { "parent.3.child-collection" "fubar" }} ) 有没有人发现并解决了这个问题


谢谢

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

给定与您的更新请求相匹配的模式,我可以让这三个都工作,请参阅下面的test/core.clj以获取完整的详细信息

第一部分:是的,您可以按您所写的方式推送到第三个父对象的子集合

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

第三部分:是的,你的基本更新在下面对我有效,正如你所写的那样

“莱恩测试”的输出在底部。祝你一切顺利

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.
Testing free-11749-clojure-subdoc.test.core
({:_id#,:我的条件键“我的条件值”,:父[{:子集合[“cc0”]}{:子集合[“cc1”]}{:子集合[“cc2”]}{:子集合[“cc3”“fubar”]}{:子集合[“cc4”]})
({:{u id},:doc-criteria-key“doc-criteria-value”,:parent[{:child{:lastname[“Alias”]}}{:child{:lastname[“Smith”“fubar”]}{:child{:lastname[“Jones”]}})
({:{u id},:content[{:content[“cc0”]}{:content[“cc1”{:fu“bar”}]}{:content[“cc2”]}],:owner“fubar@gmail.com"})
运行了1个包含9个断言的测试。
0次失败,0次错误。

不是
$push
应该是
“$push”
我认为这不对。如果你看,他们使用$set操作符(使用'monger.operators')。我还是试了一下,还是犯了同样的错误。我还尝试了主机和端口的不同组合(27017)。没有骰子……嗯:/Ho-haa,这就成功了。我得在报纸上读点东西。但我做得更好。谢谢:)作为后续问题。。您知道如何删除子文档吗?我很难做到这一点。我已经概述了我的困境和挑战。谢谢你的帮助。好的,我解决了这个后续问题。您可以看到我的解决方案在本文末尾概述。希望这有帮助。
(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.