最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to change order of array with MongoDB? - Stack Overflow

programmeradmin3浏览0评论

I need to be able to increment and decrement the position of an element of an array in a MongoDB object.

I looked at the <update> API in the MongoDB API but could not find anything to let me do so.

I am trying to use findOneAndUpdate through Mongoose and I know the index of the element I am trying to shift up or down.

An example of the array item of base64 encoded images:

{ 
  images: [
    "img1",
    "img2",
    "img3"
  ]
}

And I would like to move, for example "img2", up or down (but "image" should not be able to pushed up since there is nowhere to go).

If I wanted to push "img2" up then the result would be:

{ 
  images: [
    "img2",
    "img1",
    "img3"
  ]
}

It doesn't matter if I achieve this through changing the index, swapping, or pushing up/down.

I need to be able to increment and decrement the position of an element of an array in a MongoDB object.

I looked at the <update> API in the MongoDB API but could not find anything to let me do so.

I am trying to use findOneAndUpdate through Mongoose and I know the index of the element I am trying to shift up or down.

An example of the array item of base64 encoded images:

{ 
  images: [
    "img1",
    "img2",
    "img3"
  ]
}

And I would like to move, for example "img2", up or down (but "image" should not be able to pushed up since there is nowhere to go).

If I wanted to push "img2" up then the result would be:

{ 
  images: [
    "img2",
    "img1",
    "img3"
  ]
}

It doesn't matter if I achieve this through changing the index, swapping, or pushing up/down.

Share Improve this question edited Mar 5, 2016 at 6:59 Adam Thompson asked Mar 5, 2016 at 6:32 Adam ThompsonAdam Thompson 3,5064 gold badges28 silver badges46 bronze badges 7
  • 1 Sorry but your question is very unclear and open to interpretation in many different ways. You should rather show a sample document and the result you want to achieve from your update. – Blakes Seven Commented Mar 5, 2016 at 6:54
  • Thank you for the feedback, I updated my question. Please let me know if I can clarify further. – Adam Thompson Commented Mar 5, 2016 at 7:00
  • 1 The problem with writing an "abstract case" is that the answers that suit that case almost never apply to the real world scenario you are trying to represent and ultimately solve. I would show your real data, or at least as close to what really represents. It's unlikely your base64 images are as simple content as ["img1", "img2", "img3] and are of course plex encoded strings. So it's important to people to know whether or not this is just plain "base64" data directly in each array index, or whether it resides inside another document, with some identifier or key. – Blakes Seven Commented Mar 5, 2016 at 7:06
  • I agree! However, in this case it literally is just a string just as in the abstract case. And if I put in real data it will take up too much of the page for it to be useful. – Adam Thompson Commented Mar 5, 2016 at 7:18
  • stackoverflow./questions/872310/… – Shanoor Commented Mar 5, 2016 at 7:37
 |  Show 2 more ments

1 Answer 1

Reset to default 12

Like @blakes-seven said, you have two ways to do it:

Grabing, updating and pushing

db.images.find({ _id: '' }, { images : 1 })
.then(function(img) {
  var tmpImg = img.images[0];
  img.images[0] = img.images[1];
  img.images[1] = tmpImg;

  db.images.update({ _id: img._id }, { $set: { images: img.images } });
})

Updating directly (if you have the image on the client and know the indexes), using $position

db.images.update({ _id: '' }, { $push: { images: { $each: ['img2'] }, $position: 0 } } } )
.then(function() {
  db.images.update({ _id: '' }, {$unset: {'images.2': 1}})
});

https://docs.mongodb/manual/reference/operator/update/position/

However, I think you should redesign the way you stores your images, using by example a virtual ordering:

{
  images: [
    { base64: 'img1', order: 0, _id: 'img1' },
    { base64: 'img2', order: 1, _id: 'img2' },
    { base64: 'img3', order: 2, _id: 'img3' }
  ]
}

This way, you could order your images using a virtual order index, updating them using only the _id, or updating the whole collection by changing order of all img2, dropping or replacing an image, etc.

发布评论

评论列表(0)

  1. 暂无评论