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?

7
  • Try rearranging accountSchema backwards, sounds stupid but maybe works :) Commented Apr 8, 2019 at 20:52
  • @vitomadio that doesn't work Commented Apr 8, 2019 at 20:56
  • How does the order affect your code ? Commented Apr 8, 2019 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. Commented Apr 9, 2019 at 8: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. Commented Apr 9, 2019 at 14:56

1 Answer 1

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);

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.