如何在侧面嵌套数组猫鼬中定义对象数组

时间:2019-04-08 20:31:15

标签: javascript node.js mongoose mongoose-schema

我有一个使用已经填充的mongoDB的express API,并且已经定义了如下架构:

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

我已经从数据库中复制并粘贴了所有属性,所以我知道它们匹配,所以我知道这是不可能的

我得到的响应是这样:

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

但是我想要得到的响应是这样的:

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

我希望帐号帐号出现在 accountHolders 字段之前。

我不确定这是否是我在另一个抛出结构的嵌套数组中定义嵌套数组的方式。如果我没有定义 accountHolderSchema ,则返回的结构很好。有什么想法吗?

1 个答案:

答案 0 :(得分:2)

如果属性的顺序对您确实很重要,则可以使用一些简单的JavaScript。
遍历模式属性并将属性分配给obj

({Object.keys(obj))将返回一个带有属性的数组,然后通过从数组中获取键并从文档中分配值来制作文档的有组织副本。

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