Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
76
rated 0 times [  81] [ 5]  / answers: 1 / hits: 16874  / 10 Years ago, thu, february 20, 2014, 12:00:00
    Polls.update({_id: id}, {$set : {already_voted[length]: ip});


Now this obviously doesn't work. I can't simply put a variable length in there.



Basically I have already_voted which is an array and I want to add a new ip to this array. The way I handle this currently is by getting the old length and using the old length as the new index to add the element.



I am wondering how I should go about doing this as my current setup does not work.



To clarify : I don't have the whole array, I just want to add a new element into the array in the Poll document.


More From » arrays

 Answers
41

Use the $push Mongo operator:



Polls.update({ _id: id },{ $push: { already_voted: ip }})



See the docs here.


[#72420] Tuesday, February 18, 2014, 10 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
kaitlynd

Total Points: 470
Total Questions: 108
Total Answers: 120

Location: Faroe Islands
Member since Thu, Apr 8, 2021
3 Years ago
;