MongoDB
 sql >> Database >  >> NoSQL >> MongoDB

come aggiornare il numero di sequenza in mongodb in modo sicuro

Per farlo in modo atomico, tutti e tre i documenti di esempio devono far parte dello stesso documento. MongoDB esegue operazioni atomiche solo su documenti semplici:http://www.mongodb.org/ display/DOCS/Atomic+Operazioni

Se fanno parte di un unico documento, quanto segue modificherebbe l'ordine del 2° e 3° sottodocumento:

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

Domanda:

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

Risultato:

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