0

I read: Swap the values in a MongoDB array

And tried to use that but it didn't work for the example I have so will ask if I missed anything or if I need a different approach.

I have a DB with objects that looks something like this:

    {
       "_id": ObjectId("5448ec9c97086e180c239346"),
       "cfg": {
         "children": {
           "0": ObjectId("5448ecb2d9f98f3c1746f5ba"),
           "1": ObjectId("5448ecb2d9f98f3c1746f5bf"),
           "2": ObjectId("5448ecb2d9f98f3c1746f5bb"),
           "3": ObjectId("5448ecb2d9f98f3c1746f5c0"),
           "4": ObjectId("5448ecb2d9f98f3c1746f5bc"),
           "5": ObjectId("5448ecb2d9f98f3c1746f5c1"),
           "6": ObjectId("5448ecb2d9f98f3c1746f5bd"),
           "7": ObjectId("5448ecb2d9f98f3c1746f5c2"),
           "8": ObjectId("5448ecb2d9f98f3c1746f5be"),
           "9": ObjectId("5448ecb2d9f98f3c1746f5c3")
        }
      }
    }   

It also has the objects listen here in the "children" list.

This is supposed to be a ordered list, so if I want to move index 6 up to index 4, i also have to move 5 down to 6 and 4 down to 5.

The way that I handled this is to try and use the swap suggested in the link I had in the start and then run that recursive.

I don't want to paste to much code here but basiclly what I do is:

function swapChilds(index, i, parent, steps, self,callback){
    if (i >= steps + 1)
        return callback(null);
    // Genereate swap
    self.collection.update({_id: parent.meta.parentObjectID},{$set: swap}, function(err){
        if (err){
            console.log('errSwap:' + err);
            return callback(err);
        }
        swapChilds(index,i + 1, parent, steps, self,callback);
    });
}

So this uses index and i to get a correct swaping going and then just calls itself until it has been called steps amout of times (i start with i = 1)

When tying this I was also using console.log(swap) just to see that it looks good, and it does, it looks something like:

{ 'cfg.children.6': 5448ecb2d9f98f3c1746f5c1,
  'cfg.children.5': 5448ecb2d9f98f3c1746f5bd }
{ 'cfg.children.5': 5448ecb2d9f98f3c1746f5bc,
  'cfg.children.4': 5448ecb2d9f98f3c1746f5c1 }

There are 2 problems, the first is that that last row there should be 'cfg.children.4': 5448ecb2d9f98f3c1746f5bd (notice the last two letters) since this "bd" is the one that should get moved up to position 4. I realize why this happens, it's since I don't get a new version of the parent, so it will use the old list (will fix and test if it makes any difference).

Now despite this error the code does nothing, when I check in the db after running this set, nothing has changed.

I don't understand where the problem is. Am I doing something very wrong here? Does it have something todo with ObjectID? I've tried to just use the string, i've tried to make new ObjectID(correctValue) but it doesn't do anything at all.

How can I make multiple swaps to keep a list ordered?

Community
  • 1
  • 1
HenrikT
  • 3
  • 3

1 Answers1

0

I had messed up in the arguments was as simple as that.

If anyone is intressed in the fix the new code looks like:

function swapChilds(index, i, parent, steps, self,callback){
    if (i >= steps + 1)
        return callback(null);
    // Genereate swapdata
    self.collection.update({_id: parent._id},{$set: swap}, function(err){
        if (err){
            console.log('errSwap:' + err);
            return callback(err);
        }
        self.getObjectById(parent._id, function(err,parent2) {
            if (err){
                console.log('errSwap2:' + err);
                return callback(err);
            }
            swapChilds(index,i + 1, parent2, steps, self,callback);
        });
    });
}

I've added a call to get the updated parent so that it doesn't use old values after the first swap, I also change to parent._id instead of the wrong one I had before parent.meta.parentObjectId (I just wanted the current object not it's parent).

HenrikT
  • 3
  • 3