2

I am using Meteor where I have a mongodb document where something inserted with the following code:

Poll_Coll.insert({question:quest,option1:[{pd:op1,ids:[]}],
        option2:[{pd:op2,ids:[]}],
        option3:[{pd:op3,ids:[]}],
        option4:[{pd:op4,ids:[]}]});

I want to update multiple ids in the option1.ids array, which I tried to do like this:

Polls_Coll.update({_id:"xxxx","option1.pd":"xxx"},{$push:{"option1.$.ids":6}});
Polls_Coll.update({_id:"xxxxx","option1.pd":"xxx"},{$push:{"option1.$.ids":{id:"ya"}}});

option1.pd is working perfectly. I tried both the above commands and am getting the error

Error: MinimongoError: can't append to array using string field name [$] [409]

How do I insert into that ids field?

1 Answer 1

3

The issue is with the minimongo implementation being used by meteor as it currently does not support the positional $ operator.

From the following data sample the operations are working in the mongo shell

{
    "_id" : ObjectId("52eb0a6542b2498fd49f4f28"),
    "question" : "quest",
    "option1" : [
            {
                    "pd" : "op1",
                    "ids" : [ ]
            },
            {
                    "pd" : "op7",
                    "ids" : [ ] 
            }
    ],
    "option2" : [
            {
                    "pd" : "op2",
                    "ids" : [ ]
            }
    ],
    "option3" : [
            {
                    "pd" : "op3",
                    "ids" : [ ]
            }
    ],
    "option4" : [
            {
                    "pd" : "op4",
                    "ids" : [ ]
            }
    ]
}

Applying the following statement will push values onto the matching element's 'ids' array.

db.poll.update({"option1.pd": "op1"},{$push: { "option1.$.ids": 6 }})

Basically the same code will work server side as well.

It would seem the solution would be to wrap this update in a function call that can be invoked from the client and execute the code on the server.

Here are some links confirming the issue:

https://github.com/meteor/meteor/issues/153

https://github.com/meteor/meteor/blob/master/packages/minimongo/NOTES

Sign up to request clarification or add additional context in comments.

4 Comments

This is how i'm inserting into the collection 'Polls_Coll.insert({question: quest, option1:[{pd:op1,ids:[]}], option2:[{pd:op2,ids:[]}], option3:[{pd:op3,ids:[]}], option4:[{pd:op4,ids:[]}] });' and this is how i tried to update in the code Polls_Coll.update({_id:this._id},{$push:{'option1.$.ids':'hooya'}}); i tried this in browser console this too not working Polls_Coll.update({_id:"soeg9ezkMwKFmdPkT","option1.pd": "android"},{$push: { "option1.$.ids": 6 }}) i declared this collection at the top of the js file and published from the server side
and subscribed at client side. now i tried this in console Polls_Coll.update({_id:"soeg9ezkMwKFmdPkT","option1.pd": "android"},{$push: { "option1.$.ids": 6 }}) now it is showing error Error: Not permitted. Untrusted code may only update documents by ID. [403] although i gave id. Thanks for the help
And yes that will be the issue. Updated notes on answer and your question to clarify this is a Meteor problem with minimongo. You won't be able to use the transparent "update" methods for this data and need to roll up a more traditional RPC call.
Hey Neil i appreciate your help and i am new to this stackoverflow. Once i get those 15 reputation points i'll definitely up vote your answer. Sorry for the delay

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.