Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/34.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/13.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
Node.js MongoDB更新$pull文档来自多个数组_Node.js_Mongodb - Fatal编程技术网

Node.js MongoDB更新$pull文档来自多个数组

Node.js MongoDB更新$pull文档来自多个数组,node.js,mongodb,Node.js,Mongodb,我有一份结构如下(简化)的文件: 如何编写一个从两个容器中删除“component1”的查询?这可能吗 到目前为止,我已经尝试过使用{“$pullAll”:{“containers.$.component”:[“component1”]},对$pull进行类似的查询,设置multi:true,但我总是只从第一个数组中删除组件(我使用的是.update()) 编辑:前面是原始数据 { "_id" : ObjectId("53a056cebe56154c99dc950b"), "_e

我有一份结构如下(简化)的文件:

如何编写一个从两个容器中删除“component1”的查询?这可能吗

到目前为止,我已经尝试过使用
{“$pullAll”:{“containers.$.component”:[“component1”]}
,对
$pull
进行类似的查询,设置
multi:true
,但我总是只从第一个数组中删除组件(我使用的是
.update()

编辑:前面是原始数据

{
    "_id" : ObjectId("53a056cebe56154c99dc950b"),
    "_embedded" : {
        "click" : {
            "items" : [],
            "_links" : {
                "self" : {
                    "href" : "http://localhost/v1/click"
                }
            }
        },
        "container" : {
            "_links" : {
                "self" : {
                    "href" : "http://localhost/v1/container"
                }
            },
            "items" : [ 
                {
                    "name" : "Container test",
                    "uriName" : "Container_test",
                    "description" : "this is a test container",
                    "containerId" : "CONTAINER TEST+SITE_TEST",
                    "component" : [ 
                        "ANOTHER COMPONENT+SITE_TEST", 
                        "ANOTHER COMPONENT+SITE_TEST", 
                        "SARASA+SITE_TEST"
                    ],
                    "_links" : {
                        "self" : {
                            "href" : "http://localhost/v1/container/CONTAINER TEST+SITE_TEST"
                        }
                    }
                }, 
                {
                    "name" : "sasasa",
                    "uriName" : "sasasa",
                    "description" : "container description",
                    "containerId" : "SASASA+SITE_TEST",
                    "component" : [ 
                        "ANOTHER COMPONENT+SITE_TEST", 
                        "COMPONENT+SITE_TEST", 
                        "FAFAFA+SITE_TEST", 
                        "SARASA+SITE_TEST"
                    ],
                    "_links" : {
                        "self" : {
                            "href" : "/v1/container/SASASA+SITE_TEST"
                        }
                    }
                }
            ]
        }
    },
    "name" : "SITE_TEST",
    "siteId" : "SITE_TEST",
    "url" : "/v1/site"
}
好的,我要做的是从两个容器中删除组件“SARASA+SITE_TEST”。我正在使用robomongo测试查询。我尝试了
db.site.update({u embedded.container.items.component:“{SARASA+site\u TEST”},{$pullAll:{u embedded.container.items.component:“[“SARASA+site\u TEST”]},{multi:true})
但它不起作用,以前我尝试过
db.site.update({{u embedded.container.items.items.component:“{SARASA+site\u TEST”},{$pull:{“_embedded.container.items.$.component”:“SARASA+SITE_TEST”},{“multi”:true})
它也不起作用。我假设robomongo直接公开mongo驱动程序,我没有尝试从命令行运行它


(该文档是一个“站点”,这就是为什么我的查询以db.site开头)

我尝试了简化版的数据,$pull工作:

> db.testcoll.insert({"containers": {"containreId": 1, "components": ["component1", "component2"]}})
> db.testcoll.insert({"containers": {"containreId": 2, "components": ["component3", "component1"]}})
> db.testcoll.find()
{ "_id" : ObjectId("53a8428ca2696f063b5c51eb"), "containers" : { "containreId" : 1, "components" : [  "component1",  "component2" ] } }
{ "_id" : ObjectId("53a8429ea2696f063b5c51ec"), "containers" : { "containreId" : 2, "components" : [  "component3",  "component1" ] } }
> db.testcoll.update({"containers.components": "component1"}, {$pull: {"containers.components": "component1"}}, {multi: true})
> db.testcoll.find()
{ "_id" : ObjectId("53a8428ca2696f063b5c51eb"), "containers" : { "components" : [  "component2" ], "containreId" : 1 } }
{ "_id" : ObjectId("53a8429ea2696f063b5c51ec"), "containers" : { "components" : [  "component3" ], "containreId" : 2 } }

我有一个类似的问题,我尝试了$pullAll,它成功了


$pull with multi:true应该可以工作。你的.update()看起来怎么样?你是从mongo shell还是在代码中完成的?如果在代码中,你使用的是哪个驱动程序?你是从某个服务中得到这个json作为响应吗?它的形式是{“containers”:json_array}.我尝试过,$pull似乎在嵌套数组即“containers.components”上不起作用。但是,如果您仅对json_数组应用此查询,它将返回预期的结果.db.collection.update({“components”:“component1”},{$pull:{“components”:“component1”},{multi:true})。@TusharMishra我没听懂你的话。请看我的编辑。我试过做db.site.update({“component”:“SARASA+site_TEST”},{“$pull”:{“component”:“SARASA+site_TEST”},{“multi”:true}),但它不起作用,尽管它执行成功。@Ben我更新了我的问题我想这个问题可以追溯到:如何更新一个数组中的多个元素?”,无论它是否是嵌套数组。好吧,关于stackoverflow的问题已经被提出,答案是:没有。但是有一些解决方法(请参阅链接)。
> db.testcoll.insert({"containers": {"containreId": 1, "components": ["component1", "component2"]}})
> db.testcoll.insert({"containers": {"containreId": 2, "components": ["component3", "component1"]}})
> db.testcoll.find()
{ "_id" : ObjectId("53a8428ca2696f063b5c51eb"), "containers" : { "containreId" : 1, "components" : [  "component1",  "component2" ] } }
{ "_id" : ObjectId("53a8429ea2696f063b5c51ec"), "containers" : { "containreId" : 2, "components" : [  "component3",  "component1" ] } }
> db.testcoll.update({"containers.components": "component1"}, {$pull: {"containers.components": "component1"}}, {multi: true})
> db.testcoll.find()
{ "_id" : ObjectId("53a8428ca2696f063b5c51eb"), "containers" : { "components" : [  "component2" ], "containreId" : 1 } }
{ "_id" : ObjectId("53a8429ea2696f063b5c51ec"), "containers" : { "components" : [  "component3" ], "containreId" : 2 } }