我已根据安装指南中的配置安装了MongoDB 3.6。我有CentOS 7。
现在,我有这份文件:
{
"_id" : ObjectId("5a561e324f5b1e2c0066b568"),
"owners" : [
{
"owner" : "Juan",
"from" : "1990",
"to" : "1995"
},
{
"owner" : "Diego",
"from" : "1995",
"to" : "1997"
},
{
"owner" : "Alonso",
"from" : "1997",
"to" : "1998"
}
]
}
我想同时$inc
from
和to
同时只有一次更新。可以吗?
我在:
db.guitar.findAndModify({
query: {},
update: {
$set: {
$inc:{"owners.$[element].from":1, "owners.$[element].to":2}
}
},
arrayFilters: [{"element.owner":{$eq:"Juan"}}],
new: true
})
有错误:
2018-02-16T15:44:58.138+0100 E QUERY [thread1] Error: findAndModifyFailed failed: {
"ok" : 0,
"errmsg" : "The array filter for identifier 'element' was not used in the update { $set: { $inc: { owners.$[element].from: 1.0, owners.$[element].to: 2.0 } } }",
"code" : 9,
"codeName" : "FailedToParse"
} :
_getErrorWithCode@src/mongo/shell/utils.js:25:13
DBCollection.prototype.findAndModify@src/mongo/shell/collection.js:724:1
@(shell):1:1
如果我使用更新:
> db.guitar.update(
... {"owners.owner":"Juan"},
... {$set: {$inc:{"owners.$.from": 3333, "owners.$.to":4444}}},
... {"multi":true}
... )
WriteResult({
"nMatched" : 0,
"nUpserted" : 0,
"nModified" : 0,
"writeError" : {
"code" : 52,
"errmsg" : "The dollar ($) prefixed field '$inc' in '$inc' is not valid for storage."
}
})
它既不起作用。
那么,有可能吗?
答案 0 :(得分:1)
正确的更新查询将是:
StackLayout
{
id: mainStack
anchors.fill: parent
WelcomePage
{
id: welcomeId
onClicked: mainStack.currentIndex = 1
}
Page
{
}
}
请注意,您不需要仅设置db.guitar.findAndModify({
query: {},
update: {
$inc:{"owners.$[element].from":1, "owners.$[element].to":2}
},
arrayFilters: [{"element.owner":{$eq:"Juan"}}],
new: true
})
,但$inc
仅适用于数值,因此您会收到以下错误:
$inc
在这种情况下,将值转换为数字并再次尝试。