问题
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
}
回答1:
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 } ] }
来源:https://stackoverflow.com/questions/9566847/how-to-update-sequence-number-in-mongodb-safely