如何更新MongoDB动态属性? [英] How to update MongoDB dynamic attributes?

查看:224
本文介绍了如何更新MongoDB动态属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在使用具有以下结构的动态属性模型:

I am currently using a dynamic attributes model with the next structure:

{
  SKU: "Y32944EW",
  type: "shoes",
  attr: [
      { "k": "manufacturer", 
        "v": "ShoesForAll",
      },
      { "k": "color", 
        "v": "blue",
      },
      { "k": "style", 
        "v": "comfort",
      },
      { "k": "size", 
        "v": "7B"
      }
  ]
}

我需要做的是根据其他属性条件更新某些属性,例如,假设我想将颜色更新为红色,并将样式更新为运动,制造商为"ShoesForAll",如果我喜欢的话这个:

What I need to do is update some of the attributes based on other attributes conditions, for example, lets suppose that I want to update the color to red and style to sport where manufacturer is "ShoesForAll", If I do it like this:

collection.update({"attr": { "$elemMatch" : { "k":"manufacturer", "v":"ShoesForAll" } },
  {$set: {
    attr: [ 
     { "k": "color", 
        "v": "red",
      },
      { "k": "style", 
        "v": "sport",
      },]
  }}
});

我正在丢失其他属性,例如大小",如果这样做,我会这样做:

I am losing other attributes like "size", and if I do it like this:

collection.update({
    "attr": { "$elemMatch" : { "k":"manufacturer", "v":"ShoesForAll" }},
    "attr.k": "color",
    "attr.k": "style"
    }, {$set: {
            "data.$.v": "red",
            "data.$.v": "sport"  
    }})

仅更新一个属性.谁知道我该如何更新某些属性而不丢失其他属性或不对每个属性使用一个查询?

Only one attribute is updated. Anyone knows how can I update some of the attributes without losing the others ones or without using one query for each attribute?

谢谢

推荐答案

我已经尝试过forEach来解决此问题.目前,对于测试数据,它可以正常工作. 代码段如下:-

I have tried forEach to solve this problem. It is currently working fine for the test data . The code snippet is as follows:-

db.<collectionName>.find({"attr":{"k":"manufacturer","v":"ShoesForAll"}}).forEach(function(item){

item.attr.forEach(function(val){
    if(val.k == "color"){
        val.v = "red"
    }
    if(val.k == "style"){
        val.v = "sports"
    }
})
db.<collectionName>.save(item);
});

上面的代码首先找到满足"manufacturer"条件的所有文档为"ShoesForAll".然后在每个文档中,我们遍历"attr"数组,更新有效字段并最终保存它.不会发生不必要的数据丢失.

The above code first finds all the documents that satisfies the condition of "manufacturer" is "ShoesForAll" . Then in each document , we iterate through the "attr" array , update the valid fields and finally save it . No unnecessary data loss occurs.

您还提到过滤条件可以不止一个,在这种情况下,请使用逻辑运算符($ and,$ or).举个例子:-

You have also mentioned that the filtering criteria can be more than one, in that case use logical operators ($and , $or). Giving an example:-

db.<collectionName>.find({$or: [{"attr":{"k":"manufacturer","v":"ShoesForAll"}},{"attr":{"k":"manufacturer","v":"formal"}}]}).forEach(function(item){

item.attr.forEach(function(val){
    if(val.k == "color"){
        val.v = "red"
    }
    if(val.k == "style"){
        val.v = "sports"
    }
})
db.<collectionName>.save(item);
});

在适当的位置替换collectionName和方括号.

Replace the collectionName along with the brackets in the appropiate places.

这篇关于如何更新MongoDB动态属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆