2012-03-05 51 views
0

在我的MongoDB数据库的序列号,有一个“信息”集合,它有一个字段“秩序”,其值为每个消息整数如何更新MongoDB中安全

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

,代表的有序序列消息的子集合。同时,这些消息可以通过web页面使用jquery.sortable重新排序。例如,如果我将位置No.3移动到No.1,那么我应该将'订单'值更改为

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

。有没有任何mongodb修饰符或其他方式来做这样的更新,以便我可以在一个步骤,或以一种安全的方式做这样的更新?

3样证件:

{ 
"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 
} 
+0

您可以发布示例文档吗? – Shekhar 2012-03-05 13:00:15

+0

MongoDB操作[仅适用于单个文档](http://www.mongodb.org/display/DOCS/Atomic+Operations) – 2012-03-05 14:01:55

回答

0

为了原子地做到这一点,所有你的三个样本文件必须是同一文档的一部分。 MongoDB的只做操作原子上的简单文件:http://www.mongodb.org/display/DOCS/Atomic+Operations

如果他们是一个单一文件的一部分,下面将改变第2和第3子文档的顺序:

 
> 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 
     } 
    ] 
} 

查询:

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

结果:

 
> 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 
     } 
    ] 
}