Untuk melakukan ini secara atom, ketiga dokumen sampel Anda harus menjadi bagian dari dokumen yang sama. MongoDB hanya melakukan operasi secara atom pada dokumen sederhana:http://www.mongodb.org/ tampilan/DOCS/Atomic+Operations
Jika mereka adalah bagian dari satu dokumen, berikut ini akan mengubah urutan subdokumen ke-2 dan ke-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" :"Senang bertemu denganmu.", "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, "skor" :0, "text" :"Bagus!", "vote_down_count" :0, "vote_up_count" :0 } ]}
Pertanyaan:
db.so.update( { _id:new ObjectId("4f55e7ba362e2f2a734c92f8")}, { $set :{ 'subs.1.order' :3, 'subs.2.order' :2 } });Hasil:
> 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" :"Senang bertemu denganmu.", "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" :"Bagus!", "vote_down_count" :0, "vote_up_count" :0 } ]}