search

Home  >  Q&A  >  body text

mongodb数组值中的元素按顺序保存

比如我有个值已经保存在mongodb中

[
    1,
    2,
    5,
    4,
    3,
    ...
]

想更新成这样

[
    1,
    2,
    3,
    4,
    5,
    ...
]

怎么搞效率最高, 假设这个数组的内容大约100k, 有上千元素

PHP中文网PHP中文网2796 days ago506

reply all(3)I'll reply

  • PHPz

    PHPz2017-04-24 09:11:52

    Use aggregate, unwind first, and then use sort.

    reply
    0
  • 怪我咯

    怪我咯2017-04-24 09:11:52

    Take it out and sort it in the code, then set it back. There is no transaction for writing in mongo, and a large number of database operations are not recommended

    reply
    0
  • 大家讲道理

    大家讲道理2017-04-24 09:11:52

    I think you can do this:

    db.test.update({}, {$set:{"key.2":4, "key.3":3}})

    reply
    0
  • Cancelreply