I am making one project on MongoDB and inserted some documents and now i am updating the documents by this code..

bson_init( query );

bson_append_string(query, "First", "A");
bson_append_string(query, "Second", "B");
bson_append_string(query, "Third", "C");
bson_append_string(query, "Fourth", "D");
bson_append_string(query, "Fifth", "E");

bson_finish(query );

bson_init( b );

if(checkBox_First->isChecked() == true)
bson_append_string( b, "First", "Z");

if(checkBox_Second->isChecked() == true)
bson_append_string( b, "Second", "X");

if(checkBox_Third->isChecked() == true)
bson_append_string( b, "Third", "R");

if(checkBox_Fourth->isChecked() == true)
bson_append_string( b, "Fourth", "Y");

if(checkBox_Fifth->isChecked() == true)
bson_append_string( b, "Fifth","P");

bson_append_finish_object( b );

bson_finish(b );


The code is working perfectly but after updating, the document is stored in Alphabetical order. Can anyone tell me how i can update document without sorting alphabetically. I am using "$set" Update parameter.


I believe the issue you were experiencing here was SERVER-2592: fields in a document are reordered (sorted alphabetically) when setting a field value.

As of the MongoDB 2.6 production release, the existing order of fields is now maintained on updates.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.