Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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?

share|improve this question

1 Answer 1

up vote 1 down vote accepted

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

share|improve this answer
    
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 –  iAmME Jan 31 at 5:52
    
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 –  iAmME Jan 31 at 5:56
    
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. –  Neil Lunn Jan 31 at 8:15
    
An up-vote would be nice :) –  Neil Lunn Jan 31 at 13:40
    
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 –  iAmME Jan 31 at 19:47

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.