如何使用 Sails.js 和 Waterline 更新 MongoDB 子文档中的特定键?

2024-05-13

当尝试使用 Sails.js 和 Waterline ORM 更新 MongoDB 子文档中的单个键时,我遇到了问题。这就是我的person.js模型看起来像:

module.exports = {
    attributes: {
        name: { type: 'string', required: true },
        favorites: {
            type: 'json',
            defaultsTo: {
                color: null,
                place: null,
                season: null
            }
        }
    }
}

现在,假设我想从我的控制器更新特定人的名字和最喜欢的季节,我正在这样做:

Person.update({ id: '1' }, {
    name: 'Dan',
    favorites: {
        season: 'Summer'
    }
}).exec(function(error, updatedPerson) {

    console.log(updatedPerson);
});

当我运行这个时,favorites对象被完全替换为只有我更新的一个键(季节键),而不是保留其他两个键(颜色和位置),同时只更新我想要的一个。我缺少什么?如何让它只更新我指定的密钥?


您可以使用.native() http://sailsjs.org/documentation/reference/waterline-orm/models/native模型上的方法可以直接访问 mongo 驱动程序,然后$set https://docs.mongodb.com/manual/reference/operator/update/set/操作员独立更新字段。但是,您需要首先将对象转换为具有点符号的单级文档,例如

{
    "name": "Dan",
    "favorites.season": "Summer"
}

这样您就可以将其用作:

var criteria = { "id": "1" },
    update = { "$set": { "name": "Dan", "favorites.season": "Summer" } },
    options = { "new": true };

// Grab an instance of the mongo-driver
Person.native(function(err, collection) {        
    if (err) return res.serverError(err);

    // Execute any query that works with the mongo js driver
    collection.findAndModify(
        criteria, 
        null,
        update,
        options,
        function (err, updatedPerson) {
            console.log(updatedPerson);
        }
    );
});

要转换需要更新的原始对象,请使用以下函数

var convertNestedObjectToDotNotation = function(obj){
    var res = {};
    (function recurse(obj, current) {
        for(var key in obj) {
            var value = obj[key];
            var newKey = (current ? current + "." + key : key);  // joined key with dot
            if  (value && typeof value === "object") {
                recurse(value, newKey);  // it's a nested object, so do it again
            } else {
                res[newKey] = value;  // it's not an object, so set the property
            }
        }
    })(obj);

    return res;
}

然后您可以在更新中调用它

var criteria = { "id": "1" },
    update = { "$set": convertNestedObjectToDotNotation(params) },
    options = { "new": true };

检查下面的演示。

var example = {
	"name" : "Dan",
	"favorites" : {
		"season" : "winter"
	}
};

var convertNestedObjectToDotNotation = function(obj){
	var res = {};
	(function recurse(obj, current) {
		for(var key in obj) {
			var value = obj[key];
			var newKey = (current ? current + "." + key : key);  // joined key with dot
			if	(value && typeof value === "object") {
				recurse(value, newKey);  // it's a nested object, so do it again
			} else {
				res[newKey] = value;  // it's not an object, so set the property
			}
		}
	})(obj);
	
	return res;
}


var update = { "$set": convertNestedObjectToDotNotation(example) };

pre.innerHTML = "update =  " + JSON.stringify(update, null, 4);
<pre id="pre"></pre>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何使用 Sails.js 和 Waterline 更新 MongoDB 子文档中的特定键? 的相关文章

随机推荐