如何安全地更新mongodb中的序列号 [英] how to update sequence number in mongodb safely

查看:84
本文介绍了如何安全地更新mongodb中的序列号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的 mongodb 数据库中,有一个 'messages' 集合,它有一个字段 'order',其值为整数

msg1.order=1, msg2.order=2, msg3.order=3, msg4.order=4, msg5.order=5, ...

对于每条消息,表示消息子集合的有序序列.

同时,这些消息可以通过网页使用 jquery.sortable 重新排序.例如,如果我将位置 No.3 的 message 移动到 No.1,那么我应该将 'order' 值更改为

msg1.order=2, msg2.order=3, msg3.order=1, msg4.order=4, msg5.order=5, ...

.是否有任何 mongodb 修饰符或其他方法可以进行此类更新,以便我可以一步或以安全的方式进行此类更新?

3 个示例文档:

<代码>{"author_id": "a","class": "原理",content_id":空,host_id":空,修改日期":1330935540,订单":1,发布日期":1330935540,分数":0,"text": "Hello World!","vote_down_count": 0,vote_up_count":0}{"author_id": "a","class": "原理",content_id":空,host_id":空,修改日期":1330935538​​,订单":2,发布日期":1330935538​​,分数":0,"text": "很高兴认识你.","vote_down_count": 0,vote_up_count":0}{"author_id": "a","class": "原理",content_id":空,host_id":空,修改日期":1330935548,订单":3,发布日期":1330935548,分数":0,"text": "太棒了!","vote_down_count": 0,vote_up_count":0}

解决方案

为了以原子方式执行此操作,您的所有三个示例文档都需要成为同一文档的一部分.MongoDB 只对简单文档进行原子操作:http://www.mongodb.org/display/DOCS/原子+操作

如果它们是单个文档的一部分,以下内容将更改第二个和第三个子文档的顺序:

<前>> db.so.find().pretty();{"_id" : ObjectId("4f55e7ba362e2f2a734c92f8"),子":[{"author_id": "a","class": "原理",content_id":空,host_id":空,修改日期":1330935540,订单":1,发布日期":1330935540,分数":0,"text": "Hello World!",vote_down_count":0,vote_up_count":0},{"author_id": "a","class": "原理",content_id":空,host_id":空,修改日期":1330935538​​,订单":2,发布日期":1330935538​​,分数":0,"text": "很高兴认识你.",vote_down_count":0,vote_up_count":0},{"author_id": "a","class": "原理",content_id":空,host_id":空,修改日期":1330935548,订单":3,发布日期":1330935548,分数":0,"text": "太棒了!",vote_down_count":0,vote_up_count":0}]}

查询:

<前>db.so.update({ _id: new ObjectId("4f55e7ba362e2f2a734c92f8")},{ $set : { 'subs.1.order' : 3, 'subs.2.order' : 2 } });

结果:

<前>> db.so.find().pretty();{"_id" : ObjectId("4f55e7ba362e2f2a734c92f8"),子":[{"author_id": "a","class": "原理",content_id":空,host_id":空,修改日期":1330935540,订单":1,发布日期":1330935540,分数":0,"text": "Hello World!",vote_down_count":0,vote_up_count":0},{"author_id": "a","class": "原理",content_id":空,host_id":空,修改日期":1330935538​​,订单":3,发布日期":1330935538​​,分数":0,"text": "很高兴认识你.",vote_down_count":0,vote_up_count":0},{"author_id": "a","class": "原理",content_id":空,host_id":空,修改日期":1330935548,订单":2,发布日期":1330935548,分数":0,"text": "太棒了!",vote_down_count":0,vote_up_count":0}]}

In my mongodb database, there is a 'messages' collection, it has a field 'order' which values integer

msg1.order=1, msg2.order=2, msg3.order=3, msg4.order=4, msg5.order=5, ... 

for each message, represents an ordered sequence of a sub-collection of messages.

while, these messages can be re-sorted, via web page, using jquery.sortable. for example, If I move message on position No.3 to No.1, then I should change 'order' values to

msg1.order=2, msg2.order=3, msg3.order=1, msg4.order=4, msg5.order=5, ...

. are there any mongodb modifiers or other means of doing such update so that I can do such update in one step, or in a safe way?

3 sample documents:

{
"author_id": "a",
"class": "principle",
"content_id": null,
"host_id": null,
"modified_date": 1330935540,
"order": 1,
"pub_date": 1330935540,
"score": 0,
"text": "Hello World!",
"vote_down_count": 0,
"vote_up_count": 0
}

{
  "author_id": "a",
  "class": "principle",
  "content_id": null,
  "host_id": null,
  "modified_date": 1330935538,
  "order": 2,
  "pub_date": 1330935538,
  "score": 0,
  "text": "Nice to meet you.",
  "vote_down_count": 0,
  "vote_up_count": 0
}
{
  "author_id": "a",
  "class": "principle",
  "content_id": null,
  "host_id": null,
  "modified_date": 1330935548,
  "order": 3,
  "pub_date": 1330935548,
  "score": 0,
  "text": "Great!",
  "vote_down_count": 0,
  "vote_up_count": 0
}

解决方案

In order to do this atomically, all your three sample documents need to be part of the same document. MongoDB only does operations atomically on simple documents: http://www.mongodb.org/display/DOCS/Atomic+Operations

If they are part of a single document, the following would change the order of the 2nd and 3rd subdocument:

> db.so.find().pretty();
{
    "_id" : ObjectId("4f55e7ba362e2f2a734c92f8"),
    "subs" : [
        {
            "author_id" : "a",
            "class" : "principle",
            "content_id" : null,
            "host_id" : null,
            "modified_date" : 1330935540,
            "order" : 1,
            "pub_date" : 1330935540,
            "score" : 0,
            "text" : "Hello World!",
            "vote_down_count" : 0,
            "vote_up_count" : 0
        },
        {
            "author_id" : "a",
            "class" : "principle",
            "content_id" : null,
            "host_id" : null,
            "modified_date" : 1330935538,
            "order" : 2,
            "pub_date" : 1330935538,
            "score" : 0,
            "text" : "Nice to meet you.",
            "vote_down_count" : 0,
            "vote_up_count" : 0
        },
        {
            "author_id" : "a",
            "class" : "principle",
            "content_id" : null,
            "host_id" : null,
            "modified_date" : 1330935548,
            "order" : 3,
            "pub_date" : 1330935548,
            "score" : 0,
            "text" : "Great!",
            "vote_down_count" : 0,
            "vote_up_count" : 0
        }
    ]
}

Query:

db.so.update(
    { _id: new ObjectId("4f55e7ba362e2f2a734c92f8")},
    { $set : { 'subs.1.order' : 3, 'subs.2.order' : 2 } }
);

Result:

> db.so.find().pretty();
{
    "_id" : ObjectId("4f55e7ba362e2f2a734c92f8"),
    "subs" : [
        {
            "author_id" : "a",
            "class" : "principle",
            "content_id" : null,
            "host_id" : null,
            "modified_date" : 1330935540,
            "order" : 1,
            "pub_date" : 1330935540,
            "score" : 0,
            "text" : "Hello World!",
            "vote_down_count" : 0,
            "vote_up_count" : 0
        },
        {
            "author_id" : "a",
            "class" : "principle",
            "content_id" : null,
            "host_id" : null,
            "modified_date" : 1330935538,
            "order" : 3,
            "pub_date" : 1330935538,
            "score" : 0,
            "text" : "Nice to meet you.",
            "vote_down_count" : 0,
            "vote_up_count" : 0
        },
        {
            "author_id" : "a",
            "class" : "principle",
            "content_id" : null,
            "host_id" : null,
            "modified_date" : 1330935548,
            "order" : 2,
            "pub_date" : 1330935548,
            "score" : 0,
            "text" : "Great!",
            "vote_down_count" : 0,
            "vote_up_count" : 0
        }
    ]
}

这篇关于如何安全地更新mongodb中的序列号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆