多次使用位置`$`运算符来更新嵌套数组 [英] Multiple use of the positional `$` operator to update nested arrays
问题描述
这个问题与这个问题密切相关,我将考虑给出的建议关于NoSQL上下文中的模式设计,但我很好奇这一点:
This question is closely related to this one and I will consider the advice given with respect to schema design in a NoSQL context, yet I'm curious to understand this:
假设您具有以下文档:
_id : 2 abcd
name : 2 unittest.com
paths : 4
0 : 3
path : 2 home
queries : 4
0 : 3
name : 2 query1
url : 2 www.unittest.com/home?query1
requests: 4
1 : 3
name : 2 query2
url : 2 www.unittest.com/home?query2
requests: 4
基本上,我想知道
if it is possible to use MongoDB's positional
$
operator (details) multiple times, or put differently, in update scenarios that involve array/document structures with a "degree of nestedness" greater than 1:
{ <update operator>: { "paths.$.queries.$.requests" : value } }
(不起作用)
可以使用$
一次而不是仅"用于顶级数组,并且必须对更高级别"的数组使用显式索引:
instead of "only" be able to use $
once for a top-level array and being bound to use explicit indexes for arrays on "higher levels":
{ <update operator>: { "paths.$.queries.0.requests" : value } }
)(作品)
如果可能的话,相应的R语法是什么样子.
if possible at all, how the corresponding R syntax would look like.
下面您将找到一个可复制的示例.我试图尽可能简洁.
Below you'll find a reproducible example. I tried to be as concise as possible.
require("rmongodb")
db <- "__unittest"
ns <- paste(db, "hosts", sep=".")
# CONNCETION OBJECT
con <- mongo.create(db=db)
# ENSURE EMPTY DB
mongo.remove(mongo=con, ns=ns)
示例文档
q <- list("_id"="abcd")
b <- list("_id"="abcd", name="unittest.com")
mongo.insert(mongo=con, ns=ns, b=b)
q <- list("_id"="abcd")
b <- list("$push"=list(paths=list(path="home")))
mongo.update(mongo=con, ns, criteria=q, objNew=b)
q <- list("_id"="abcd", paths.path="home")
b <- list("$push"=list("paths.$.queries"=list(
name="query1", url="www.unittest.com/home?query1")))
mongo.update(mongo=con, ns, criteria=q, objNew=b)
b <- list("$push"=list("paths.$.queries"=list(
name="query2", url="www.unittest.com/home?query2")))
mongo.update(mongo=con, ns, criteria=q, objNew=b)
使用显式位置索引更新嵌套数组(有效)
这可行,但是它涉及第二级数组queries
的 explicit 索引(嵌套在数组paths
的子文档元素中):
Update of nested arrays with explicit position index (works)
This works, but it involves an explicit index for the second-level array queries
(nested in a subdoc element of array paths
):
q <- list("_id"="abcd", paths.path="home", paths.queries.name="query1")
b <- list("$push"=list("paths.$.queries.0.requests"=list(time="2013-02-13")))
> mongo.bson.from.list(b)
$push : 3
paths.$.queries.0.requests : 3
time : 2 2013-02-13
mongo.update(mongo=con, ns, criteria=q, objNew=b)
res <- mongo.find.one(mongo=con, ns=ns, query=q)
> res
_id : 2 abcd
name : 2 unittest.com
paths : 4
0 : 3
path : 2 home
queries : 4
0 : 3
name : 2 query1
requests : 4
0 : 3
time : 2 2013-02-13
url : 2 www.unittest.com/home?query1
1 : 3
name : 2 query2
url : 2 www.unittest.com/home?query2
使用位置$
索引更新嵌套数组(无效)
现在,我想用位置$
运算符替换显式的0
,就像我为了让服务器找到所需的数组paths
(paths.$.queries
)的子文档元素一样.
Update of nested arrays with positional $
indexes (doesn't work)
Now, I'd like to substitute the explicit 0
with the positional $
operator just like I did in order to have the server find the desired subdoc element of array paths
(paths.$.queries
).
AFAIU 文档,这应该是至关重要的事情是指定一个正确的"查询选择器:
AFAIU the documentation, this should work as the crucial thing is to specify a "correct" query selector:
位置$运算符与update()方法一起使用时,用作更新查询选择器的第一个匹配项的占位符:
The positional $ operator, when used with the update() method and acts as a placeholder for the first match of the update query selector:
我想我指定了一个查询选择器,它确实找到了正确的嵌套元素(由于paths.queries.name="query1"
部分):
I think I specified a query selector that does find the correct nested element (due to the paths.queries.name="query1"
part):
q <- list("_id"="abcd", paths.path="home", paths.queries.name="query1")
我猜翻译为普通MongoDB"语法,查询选择器看起来像这样
I guess translated to "plain MongoDB" syntax, the query selector looks somewhat like this
{ _id: abcd, paths.path: home, paths.queries.name: query1 }
对我来说似乎是一个有效的查询选择器.实际上,它确实符合所需的元素/文档:
which seems like a valid query selector to me. In fact it does match the desired element/doc:
> !is.null(mongo.find.one(mongo=con, ns=ns, query=q))
[1] TRUE
我的想法是,如果它可以在顶层使用,为什么它也不能在更高级别使用(只要查询选择器指向正确的嵌套组件)?
My thought was that if it works on the top-level, why shouldn't it work for higher levels as well (as long as the query selector points to the right nested components)?
但是,服务器似乎不喜欢$
的嵌套或多次使用:
However, the server doesn't seem to like a nested or multiple use of $
:
b <- list("$push"=list("paths.$.queries.$.requests"=list(time="2013-02-14")))
> mongo.bson.from.list(b)
$push : 3
paths.$.queries.$.requests : 3
time : 2 2013-02-14
> mongo.update(mongo=con, ns, criteria=q, objNew=b)
[1] FALSE
我不确定它是否不起作用,因为MongoDB不支持此功能,或者我没有正确使用R语法.
I'm not sure if it doesn't work because MongoDB doesn't support this or if I didn't get the R syntax right.
推荐答案
位置运算符仅支持一个级别的深度,并且仅支持第一个匹配元素.
The positional operator only supports one level deep and only the first matching element.
这里有一个针对您想要的行为的JIRA可跟踪: https://jira.mongodb.org/browse /SERVER-831
There is a JIRA trackable for the sort of behaviour you want here: https://jira.mongodb.org/browse/SERVER-831
我不确定是否可以进行多场比赛,但我相信这是由于工作方式的动态变化所致.
I am unsure if it will allow for more than one match but I believe it will due to the dynamics of how it will need to work.
这篇关于多次使用位置`$`运算符来更新嵌套数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!