0

I have the following Document:

mainDoc = {
        owner: Meteor.userId(),
        createdOn: new Date(),
        active: false,
        label: "Dashboard #" + ($("ul#u-nav-tabs").find("li.u-tab").length + 1),
        monitors: [/*Embedded documents*/],
        sharewith: []
    };

mainDoc.monitors is an array of the following documents:

innerDoc = {
                _id: id._str,
                owner: Meteor.userId(),
                createdOn: new Date(),
                label: monitorLabel,
                metadata: {custDate: {}},
                style: {
                    top: mystyle.top,
                    left: mystyle.left,
                    width: 0,
                    height: 0
                },
                shown: true,
                sharewith: []
            }

I have set the following permissions on the server

userDashboards.allow({
    insert: function (userId) {
        "use strict";
        return userId;
    },
    update: function (userId, doc) {
        "use strict";
        return doc.owner === userId;
    },
    remove: function (userId, doc) {
        "use strict";
        return doc.owner === userId;
    },
    fetch: ["owner", "monitors"]
});

I tried this so far on the client:

console.log(userDashboards.findOne({"monitors._id": "5f94f2a15bddd908f2bc9d5d"}));

But I only get the full document, not the embedded document.

So the question is, how can I update innerDoc.style directly and from the browser?

Kyll
  • 7,036
  • 7
  • 41
  • 64
juanp_1982
  • 917
  • 2
  • 16
  • 37
  • hey @Ethaan ! I kind of still the problem, I found a partial solution in [here][1] but now I have a problem how I allow that collection to be updated :-( I get this error: Uncaught Error: Not permitted. Untrusted code may only update documents by ID. \[403\] [1]: http://maz-dev.cc/update-on-mongo-subdocuments/ – juanp_1982 Jan 28 '15 at 19:06
  • it helped me, I had ANOTHER problem and I solved it with your solution – juanp_1982 Jan 28 '15 at 19:10

1 Answers1

0

Try with this.

Updating

userDashboards.update({_id:"5f94f2a15bddd908f2bc9d5d"},{$set:{'monitors[0]._id':"yeaaa look mom im editing documents with arrays"}})

Assuming "5f94f2a15bddd908f2bc9d5d" its ._id not monitors._id. Like you say monitors is an array, so you should specify the index aka [0]. Remember the syntaxis of the update should be like this.

    Collection.update({_id:idDocument},{$set:{fieldToUpdate:newData}}) // correct

Collection.update({_id:idDocument,$set:{fieldToUpdate:newData}}) // wrong, you will get some "stack error" 

Collection.update({$set:{fieldToUpdate:newData}}) // wrong, you need to specify what document will be update and by stric the id should be always {_id:"5f94f2a15bddd908f2bc9d5d"} <-- like this

That should works.

Ethaan
  • 11,291
  • 5
  • 35
  • 45