4

I have an express API using an already populated mongoDB and have defined the schema like so:

const accountHolderSchema= new mongoose.Schema({
  pid: {Type: Number},
  accountNumber: {type: String},
  relationshipType: {type: String},
  firstName: {type: String},
  middleName: {type: String},
  lastName: {type: String}
});

const accountsSchema = new mongoose.Schema({
  accountNumber: String,
  accountType: String,
  accountHolder: [accountHolderSchema]
});


const productDetailSchema = new mongoose.Schema({
  pid: Number,
  accounts: [accountsSchema]
});

I have literally copied and paste all the properties and from the database so i know they match so i know that's out of the picture

The RESPONSE I get is this:

{
"pid": 2697143,
    "accounts": [
        {
            "accountHolders": [
                {
                    "pid": 13209741,
                    "accountNumber": "403716000062",
                    "relationshipType": "BENEFICIARY",
                    "firstName": "Maria",
                    "middleName": "Delores",
                    "lastName": "Jackson"
                }
             ]
            "accountNumber": "12345",
            "accountType": "RSA",
        }
     ]
}

BUT what the response I WANT to get in return is this:

{
"pid": 2697143,
    "accounts": [
        {
            "accountNumber": "12345",
            "accountType": "RSA",
            "accountHolders": [
                {
                    "pid": 13209741,
                    "accountNumber": "403716000062",
                    "relationshipType": "BENEFICIARY",
                    "firstName": "Maria",
                    "middleName": "Delores",
                    "lastName": "Jackson"
                }
             ]
        }
     ]
}

I want the accountNumber and accountNumber to come before accountHolders field.

I'm not sure if its the way how i define a nested array inside of another nested array that's throwing of the structure. If I don't the define accountHolderSchema the structure is returned fine. Any ideas?

O'Dane Brissett
  • 1,284
  • 1
  • 8
  • 23
  • Try rearranging accountSchema backwards, sounds stupid but maybe works :) – vitomadio Apr 08 '19 at 20:52
  • @vitomadio that doesn't work – O'Dane Brissett Apr 08 '19 at 20:56
  • How does the order affect your code ? – Moad Ennagi Apr 08 '19 at 21:51
  • 1
    JavaScript Objects have no *guaranteed* order of keys, **nor should they**. Whilst *most* engines do respect *"insertion order"* it is not absolute and quite often there is underlying code processing things ( very true with mongoose documents ) that may change the order of appearance. In short you really should not care which order keys appear in for a data structure. If you **do care** then the data should be rearranged into an array for the processing in which you absolutely require it in a certain order. – Neil Lunn Apr 09 '19 at 08:21
  • @NeilLunn the order doesn't really affect functionality because the data is still being return. I just wanted the response that's return from my api to match the document structure i defined in the mongoose schema. – O'Dane Brissett Apr 09 '19 at 14:56
  • The point is that just because that's the order you defined in the schema this does not mean the order of keys returned is *guaranteed* in any way. Also as I said *"you really should not care"*, and though it may be a *want* you should simply *accept* that the order should not matter. If you think it matters, then transpose the results to an *array* or *Map* which are the only things that *guarantee* a returned order. Which is also what linked answers say. – Neil Lunn Apr 14 '19 at 03:23
  • Okay noted. Thanks for your input. – O'Dane Brissett Apr 14 '19 at 18:45

1 Answers1

2

If the order of the propreties is really important to you, you could use some simple JavaScript.
Loop through your Schema propreties and assign the propreties to an obj.

(Object.keys(obj)) will return an array with the propreties, then you make an organized copy of your document by getting the keys from the array and assigning values from the document.

let schema = {
"pid": 2697143,
    "accounts": [
        {
            "accountNumber": "12345",
            "accountType": "RSA",
            "accountHolders": [
                {
                    "pid": 13209741,
                    "accountNumber": "403716000062",
                    "relationshipType": "BENEFICIARY",
                    "firstName": "Maria",
                    "middleName": "Delores",
                    "lastName": "Jackson"
                }
             ]
        }
     ]
}
let doc = {
"pid": 2697143,
    "accounts": [
        {
            "accountHolders": [
                {
                    "pid": 13209741,
                    "accountNumber": "403716000062",
                    "relationshipType": "BENEFICIARY",
                    "firstName": "Maria",
                    "middleName": "Delores",
                    "lastName": "Jackson"
                }
             ],
            "accountNumber": "12345",
            "accountType": "RSA",
        }
     ]
}

let docOrganized= {};
docOrganized.pid = doc.pid;
Object.keys(schema.accounts[0]).map(key => docOrganized[key] = doc.accounts[0][key]);
console.log(docOrganized);
Moad Ennagi
  • 1,058
  • 1
  • 11
  • 19