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

hvordan man opdaterer sekvensnummer i mongodb sikkert

For at gøre dette atomært, skal alle dine tre prøvedokumenter være en del af det samme dokument. MongoDB udfører kun operationer atomært på simple dokumenter:http://www.mongodb.org/ display/DOCS/Atomic+Operations

Hvis de er en del af et enkelt dokument, vil følgende ændre rækkefølgen af ​​det 2. og 3. underdokument:

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

Forespørgsel:

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

Resultat:

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



  1. Hvordan får jeg farvet forespørgselsoutput og shell i MongoDB?

  2. .NET bedste praksis for MongoDB-forbindelser?

  3. Php7 Redis Client på Alpine OS

  4. slip hele databasen inden for en enkelt kommando til at gendanne dumpet af mongodb