覆盖mongodb中的对象 [英] Overwrite object in mongodb

查看:32
本文介绍了覆盖mongodb中的对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这应该很简单,但它出奇的困难并且非常令人沮丧.我试图用用户在我的客户端网页中创建的新对象覆盖 mongodb 中的对象"字段.我已经验证我传递给更新操作的所有其他字段实际上都在更新,除了 javascript 对象.它不是用我传递的对象更新(虽然我验证的是用我传递的对象填充),它只是将它更新回 {} 而不是传递的内容:

This should be simple but it is surprisingly difficult and extremely frustrating. I am trying to overwrite an 'Object' field in mongodb with a new Object that the user creates in my client webpage. I have validated that all other fields I am passing to the update operation are in fact being updated, with the exception of the javascript object. Instead of it updating with the object I am passing (While I validated is being populated with the object I am passing through), it just updates it back to {} instead of whats being passed:

{ nodes:[ { w: 120, h: 80,type: 'InHive',left: 184,top: 90,text: 'item',query: 'hey',name: 'sample',id: '7686132d-6fcf-4a3b-baa2-b1c628e0b2d6' } ], edges: [], ports: [],groups: [] }

当我尝试直接从 mongo 控制台界面更新流星方法之外的数据字段时,它使用 javascript 对象成功覆盖了该字段.我在这里做错了什么,因为我一生都无法弄清楚这一点?

When I attempt to update the data field outside of the meteor method, directly from the mongo console interface, it overwrites that field successfully with the javascript object. What am I doing wrong here, because I cant for the life of me figure this one out?

服务器方法

    'updateOneWorkflow': function(id, field, object) {
    this.unblock;
    if (Meteor.userId()) {
        var _username = Meteor.user().username;
        MYCOLLECTION.update({
            _id: id
        }, {
            $set: {
                [field]: object, //this just gets reset back to {} whenever this update method is called
                "metadata.last_modified_dt": new Date(), //this gets updated
                "metadata.modified_by": Meteor.userId(), //this gets updated
                'metadata.modified_by_username': _username //This gets updated
            }
        });
    } else {
        throw new Meteor.Error(403, "You are not authorized to perform this function");
    }
}

客户电话:

var _jsonformat = toolkit.exportData();
        var currentid = Session.get('rulesRowClicked')._id;
        console.log(_jsonformat);
        Meteor.call('updateOneWorkflow' , currentid, 'data', _jsonformat, function(err, res){
            if(err){
                toastr.error('Failed to save result ' + err);
            }
            else{
                toastr.success('Saved workflow');
            }
        });

推荐答案

这个问题比我预期的更疯狂.如果您使用的是 Meteorjs 并且您使用的是 Aldeed Schema 2 集合框架,那么即使您将字段类型设置为 Object,它似乎也完全忽略了 json 对象的更新/插入,除非您设置了与对象完全相同的架构(包括嵌套数组对象)并将其附加到您的集合中.我见过的最愚蠢的事情,不知道为什么没有人警告你这一点.我删除了架构附件并且它起作用了.

The issue was crazier than I expected. If you are using Meteorjs and you are using the Aldeed Schema 2 collection framework, it seems to completely ignore updates/inserts of json objects even if you set the field type to Object, unless you set up the exact same schema as the object (including nested array objects) and attach it to your collection. Dumbest thing Ive ever seen, no idea why nothing warns you of this. I removed the schema attachment and it worked.

这篇关于覆盖mongodb中的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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