Mongodb 增量嵌套值
我用以下方法创建玩家Mongodb 增量嵌套值,mongodb,meteor,Mongodb,Meteor,我用以下方法创建玩家 Players.insert({ name: name, score: 0, items: [{'name': 0}, {'name2': 0}...] }); 如何增加特定玩家和特定物品名称的分数(必要时增加) 很抱歉用了这么糟糕的措词:p让我们假设您有: { "_id" : ObjectId("5465332e6c3e2eeb66ef3683"), "name" : "Alex", "score" : 0, "items"
Players.insert({
name: name,
score: 0,
items: [{'name': 0}, {'name2': 0}...]
});
如何增加特定玩家和特定物品名称的分数(必要时增加)
很抱歉用了这么糟糕的措词:p让我们假设您有:
{
"_id" : ObjectId("5465332e6c3e2eeb66ef3683"),
"name" : "Alex",
"score" : 0,
"items" : [
{
"food" : 0
}
]
}
要更新,您可以执行以下操作:
db.Players.update({name: "Alex", "items.food": {$exists : true}},
{$inc: {score: 1, "items.$.food": 5}})
结果:
{
"_id" : ObjectId("5465332e6c3e2eeb66ef3683"),
"name" : "Alex",
"score" : 1,
"items" : [
{
"food" : 5
}
]
}
如果由于更新数组所需的位置运算符,文档不存在,我不确定您是否可以取消插入。假设您有:
{
"_id" : ObjectId("5465332e6c3e2eeb66ef3683"),
"name" : "Alex",
"score" : 0,
"items" : [
{
"food" : 0
}
]
}
要更新,您可以执行以下操作:
db.Players.update({name: "Alex", "items.food": {$exists : true}},
{$inc: {score: 1, "items.$.food": 5}})
结果:
{
"_id" : ObjectId("5465332e6c3e2eeb66ef3683"),
"name" : "Alex",
"score" : 1,
"items" : [
{
"food" : 5
}
]
}
如果由于更新数组所需的位置运算符,文档不存在,我不确定您是否可以取消插入。假设您有:
{
"_id" : ObjectId("5465332e6c3e2eeb66ef3683"),
"name" : "Alex",
"score" : 0,
"items" : [
{
"food" : 0
}
]
}
要更新,您可以执行以下操作:
db.Players.update({name: "Alex", "items.food": {$exists : true}},
{$inc: {score: 1, "items.$.food": 5}})
结果:
{
"_id" : ObjectId("5465332e6c3e2eeb66ef3683"),
"name" : "Alex",
"score" : 1,
"items" : [
{
"food" : 5
}
]
}
如果由于更新数组所需的位置运算符,文档不存在,我不确定您是否可以取消插入。假设您有:
{
"_id" : ObjectId("5465332e6c3e2eeb66ef3683"),
"name" : "Alex",
"score" : 0,
"items" : [
{
"food" : 0
}
]
}
要更新,您可以执行以下操作:
db.Players.update({name: "Alex", "items.food": {$exists : true}},
{$inc: {score: 1, "items.$.food": 5}})
结果:
{
"_id" : ObjectId("5465332e6c3e2eeb66ef3683"),
"name" : "Alex",
"score" : 1,
"items" : [
{
"food" : 5
}
]
}
如果由于更新数组所需的位置运算符,文档不存在,我不确定您是否可以取消插入。好吧,答案是——就像在生活中一样——通过分解来简化问题 为了避免mongoDB中的数组,毕竟,对象可以有任意多个键。因此,我的结构变成:
{
"_id": <id>,
"name": <name>,
"score": <score>,
"items": {}
}
工作有魅力:)答案是——就像在生活中一样——通过分解问题来简化问题 为了避免mongoDB中的数组,毕竟,对象可以有任意多个键。因此,我的结构变成:
{
"_id": <id>,
"name": <name>,
"score": <score>,
"items": {}
}
工作有魅力:)答案是——就像在生活中一样——通过分解问题来简化问题 为了避免mongoDB中的数组,毕竟,对象可以有任意多个键。因此,我的结构变成:
{
"_id": <id>,
"name": <name>,
"score": <score>,
"items": {}
}
工作有魅力:)答案是——就像在生活中一样——通过分解问题来简化问题 为了避免mongoDB中的数组,毕竟,对象可以有任意多个键。因此,我的结构变成:
{
"_id": <id>,
"name": <name>,
"score": <score>,
"items": {}
}
很有魅力:)你能再解释一下你计划增加哪些领域吗。注意,有一个列表,所以?对不起,措辞太糟糕了。我知道播放器的_id,我想增加播放器记录中items数组中已知键对应的值。在伪javascript播放器中[_id].items[key]++。您能再解释一下您计划增加哪些字段吗。注意,有一个列表,所以?对不起,措辞太糟糕了。我知道播放器的_id,我想增加播放器记录中items数组中已知键对应的值。在伪javascript播放器中[_id].items[key]++。您能再解释一下您计划增加哪些字段吗。注意,有一个列表,所以?对不起,措辞太糟糕了。我知道播放器的_id,我想增加播放器记录中items数组中已知键对应的值。在伪javascript播放器中[_id].items[key]++。您能再解释一下您计划增加哪些字段吗。注意,有一个列表,所以?对不起,措辞太糟糕了。我知道播放器的_id,我想增加播放器记录中items数组中已知键对应的值。在伪javascript播放器[_id].items[key]++中。