0

Why does mongoose/ mongo auto-order/sort all my data keys on update?

For instance, this is the data I set to pass into the db:

{ name: 'PM 22',
  public: false,
  fields: [ 'particles', 'no2', 'no3', 'no4' ],
  '$set': 
   { 'data.particles': { Array: [Function: Array] },
     'data.no2': { Array: [Function: Array] },
     'data.no3': { Array: [Function: Array] },
     'data.no4': { Array: [Function: Array] },
     'data.timestamp': { Array: [Function: Array] } } }

Code to update:

     dataset.update(updateQuery, function(err, datasetID) {
        if (err) {
            console.log("Error updating dataset: " + err);
            req.session.error = "Update failed, please try again.";
        } else {
            console.log("Update on dataset: " + datasetID);
            req.session.success = "Update successul.";
        }
        res.redirect("/index");
    });

When I check the data in the db, I get this:

{
    "__v" : 0,
    "_id" : ObjectId("5773529bdb0dac831f885957"),
    "created_at" : ISODate("2016-06-29T04:46:19.553Z"),
    "data" : {
        "no2" : {},
        "no3" : {},
        "no4" : {},
        "particles" : {},
        "timestamp" : {}
    },
    "entries_number" : 0,
    "fields" : [ 
        "particles", 
        "no2", 
        "no3", 
        "no4"
    ],
    "index" : "1IQ0EGIHS",
    "name" : "PM 22"
}

If you take a look at you know what I mean:

"data" : {
    "no2" : {},
    "no3" : {},
    "no4" : {},
    "particles" : {},
    "timestamp" : {}
},

It should be:

"data" : {
    "particles" : {},
    "no2" : {},
    "no3" : {},
    "no4" : {},
    "timestamp" : {}
},

As I pass in the data like this:

 '$set': 
   { 'data.particles': { Array: [Function: Array] },
     'data.no2': { Array: [Function: Array] },
     'data.no3': { Array: [Function: Array] },
     'data.no4': { Array: [Function: Array] },
     'data.timestamp': { Array: [Function: Array] }

Any ideas why and how I can fix this?

EDIT:

var dataset = {
    "index" : "9IQ09VRDN",
    "name" : "PM 2",
    "owner_name" : "xxx",
    "read_key" : "16e8da42c5c93f6d53d01931e64aeda7",
    "write_key" : "521e838e26aafaefc2d9684959ee1540",
    "data" : {
        "particles" : [],
        "a" : [],
        "no3" : [],
        "no2" : [],
        "timestamp" : [],
    },
    "entries_number" : 0,
    "public" : false,
    "fields" : [
        "particles",
        "no2"
    ],
    "__v" : 0
};

// forEach is a method of array, not of object
for(var obj in dataset.data) {
    console.log(obj);
}

I got the result as expected, it is nothing to do with the javascript itself just as the answer that marked this question as duplicate:

particles
a
no3
no2
timestamp

It is nothing to do with js itself but mongo version.

I'm on 2.4. And this issue has been fixed in 2.6.

https://jira.mongodb.org/browse/SERVER-2592

mongo db update changing the order of object fields

This issue fixed by upgrading mongo 2.4 to 2.6:

Step 1: Import the MongoDB public key

$ sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 7F0CEB10

Step 2: Generate a file with the MongoDB repository url

$ echo 'deb http://downloads-distro.mongodb.org/repo/ubuntu-upstart dist 10gen' | sudo tee /etc/apt/sources.list.d/mongodb.list

Step 3: Refresh the local database with the packages

$ sudo apt-get update

Step 4: Install the last stable MongoDB version and all the necessary packages on our system

$ sudo apt-get install mongodb-org
Community
  • 1
  • 1
Run
  • 54,938
  • 169
  • 450
  • 748

0 Answers0