node.js - mongodb how to change array position -
i have mongodb document has array. there way rearrange order of array element?
example - have 10 elements in array , need element in position 7 moved position 2.
is possible?
thanks
if done within node, 1 option use mongoose along splice:
for example, remove element @ position 7 (remember, 0th based):
var position7 = doc.subdocs.splice(6, 1);
then insert earlier in array, @ position 2:
doc.subdocs.splice(1, 0, position7[0]);
more information on splice function can found here: https://developer.mozilla.org/en-us/docs/web/javascript/reference/global_objects/array/splice
Comments
Post a Comment