0

I have an object with an array of objects like that:

//restMenuType({_id: 'abcde', hasItems: [{itemId: 'a', sortId: 1}, {itemId: 'b', sortId: 2}]})

I am trying to replace the sortId key on both objects:

'replaceItemsPositionUp': function(typeId, prevId, curId, prevSortId, curSortId){
        RestMenuTypes.update({
            _id: typeId,
            hasItems: {$elemMatch: {itemId: curId}}},
            {$set: {'hasItems.sortId': prevSortId}}
        );
        RestMenuTypes.update({
                _id: typeId,
                hasItems: {$elemMatch: {itemId: prevId}}},
            {$set: {'hasItems.sortId': curSortId}}
        );

    }

What is thee right way to do it? Thanks.

3
  • it looks good, what is the problem here ? Commented Dec 6, 2016 at 18:50
  • I get a generic error 500. Commented Dec 6, 2016 at 19:00
  • can you give me the input and the document ? Commented Dec 6, 2016 at 19:34

1 Answer 1

2

That's the way to do it: Found it here: Update field in exact element array in MongoDB

RestMenuTypes.update({
            _id: typeId,
            'hasItems.itemId': curId},
            {$set: { "hasItems.$.sortId": prevSortId}}
        );
        return RestMenuTypes.update({
                _id: typeId,
                'hasItems.itemId': prevId},
            {$set: {'hasItems.$.sortId': curSortId}}
        );
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.