我有这样的东西:
{
buildings: {
"1": {
"l": 0 ,
"r": 0 ,
"s": 0 ,
"type": "GoldMine" ,
"x": 2 ,
"y": 15
} ,
"10": {
"l": 0 ,
"r": 6 ,
"s": 2 ,
"type": "MagicMine" ,
"x": 26 ,
"y": 22
}
} ,
[...]
}我想要有"GoldMine“型建筑物的物体。
我用map做了些尝试
r.table("Characters").map(function(row) {
return row("planet")("buildings")
})使用keys(),我可以迭代它:
r.db("Unnyworld").table("Characters").map(function(row) {
return row("planet")("buildings").keys().map(function(key) {
return "need to get only buildings with type == GoldMine";
})
}).limit(2)但它会归还所有的建筑。我只想要有== GoldMine和change x类型的建筑物。
发布于 2016-01-10 03:13:08
像这样的事情可能会奏效:
r.table('Characters')
.concatMap(function(doc) {
return doc("planet")("buildings").keys().map(function(k) {
return {id: doc('id'), key: k, type: doc("planet")("buildings")(k)('type'), x: doc("planet")("buildings")(k)('x')}
})
})
.filter(function(building) {
return building('type').eq('GoldMine')
})
.forEach(function(doc) {
return r.table('Characters').get(doc('id'))
.update({
planet: {buildings: r.object(doc('key'), {x: 1111111})}
})
})基本上,通过使用building创建一个平面数组,然后使用concatMap,然后使用filter。对于结果数据,我们可以迭代它并更新到我们想要的值。
https://stackoverflow.com/questions/34699531
复制相似问题