Mongoose不尊重架构并插入错误的数据类型

时间:2014-05-08 02:33:08

标签: node.js mongodb mongoose

我遇到一个奇怪的问题,Mongoose不尊重我定义的架构。我传递的数据与指定的模式完全一致,但是我要么遇到强制转换错误,要么数据(当我排除无法转换的信息时)最终在mongo中作为错误的数据类型。

架构如下:

var mongoose = require('mongoose');

var Schema = mongoose.Schema;

var AccountSchema = new Schema({
    userId: { type: Number, unique: true, dropDups: true },
    referralCode: String,
    dateStarted: { type: Date, default: Date.now },
    accountType: Number,
    accountCategories: [String],
    beneficiaries: [{
        id: Number,
        relationshipType: Number,
        percentage: Number,
        firstName: String,
        lastName: String,
        middleInitial: String,
        mailingAddress: {
            address1: String,
            address2: String,
            address3: String,
            city: String,
            state: String,
            country: String,
            postalCode: String
        },
        ssn: String,
        birthDate: Date,
        isPrimary: Boolean
    }],
    accountOwner: {
        firstName: String,
        lastName: String,
        middleInitial: String,
        birthDate: Date,
        ssn: String,
        phoneNumber: String,
        mailingAddress: {
            address1: String,
            address2: String,
            address3: String,
            city: String,
            state: String,
            country: String,
            postalCode: String
        },
        physicalAddress: {
            address1: String,
            address2: String,
            address3: String,
            city: String,
            state: String,
            country: String,
            postalCode: String
        }
    },
    fundsTransfer: {
        bankName: String,
        type: Number,
        transferType: Number,
        routingNumber: String,
        accountNumber: String,
        currentYearAmount: Number,
        previousYearAmount: Number
    },
    accountTransfer: {
        accountType: Number,
        custodianName: String,
        accountNumber: String,
        phoneNumber: String,
        faxNumber: String,
        cashAmount: Number,
        cashPortionOptions: Number,
        expediteOption: Boolean,
        otherAccountType: String,
        planType: Number
    },
    wizardFlags: {
        doCashContrib: Boolean,
        doCashTransfer: Boolean,
        doCashContribAndTransfer: Boolean,
        doAssetTransfer: Boolean
    }
});

var Account = mongoose.model("Account", AccountSchema);

module.exports = Account;

传入的数据如下:

this.mockAccountState = {
    userId: 9,
    referralCode: "",
    dateStarted: Date.now(),
    accountOwner: {
        firstName: "Test",
        lastName: "User",
        middleInitial: "X",
        birthDate: null,
        ssn: "123-45-6789",
        phoneNumber: "(123) 456-7890",
        mailingAddress: {
            address1: "123 A Street",
            address2: "",
            address3: "",
            city: "A City",
            state: "CO",
            country: "US",
            postalCode: "12345"
        },
        physicalAddress: null
    },
    accountType: 0,
    accountCategories: [],
    fundsTransfer: {
        bankName: "",
        type: 0,
        transferType: 1,
        routingNumber: "123456789",
        accountNumber: "111222333444",
        currentYearAmount: 6000,
        previousYearAmount: 0
    },
    assetTransfer: {},
    hasBeneficiaries: false,
    beneficiaries: [],
    wizardFlags: {
        doCashContrib: true,
        doCashTransfer: false,
        doCashContribAndTransfer: false,
        doAssetTransfer: false
    }
};

如果我为bankName传入非空字符串值,则会出现以下错误:

  

更新帐户时出错:CastError:对于值,转换为数字失败   路径“fundsTransfer”中的“测试银行”

如果我排除bankName,那么实际在数据库中结束的数据如下:

enter image description here

如果我使用mongo shell选择此数据,我会得到:

{ 
    "_id" : ObjectId("536ae87342d6347028b42e1e"),
    "userId" : 1, 
    "referralCode" : "", 
    "accountType" : 1, 
    "wizardFlags" : { 
        "doAssetTransfer" : false, 
        "doCashContribAndTransfer" : false, 
        "doCashTransfer" : false, 
        "doCashContrib" : true
    }, 
    "accountOwner" : { 
        "mailingAddress" : { 
            "postalCode" : "12345", 
            "country" : "US",
            "state" : "CO",
            "city" : "A City", 
            "address3" : "", 
            "address2" : "", 
            "address1" : "123 A Street" 
        }, 
        "physicalAddress" : {  }, 
        "phoneNumber" : "(123) 456-7890",
        "ssn" : "123-45-6789",
        "birthDate" : null,
        "middleInitial" : "X",
        "lastName" : "User",
        "firstName" : "Test"
    }, 
    "beneficiaries" : [ ], 
    "accountCategories" : [ "3" ],
    "dateStarted" : ISODate("2014-05-08T02:14:05.379Z"),
    "__v" : 0,
    "accountTransfer" : {  }, 
    "fundsTransfer" : { 
        "bankName" : null,
        "type" : 0, 
        "transferType" : 1, 
        "routingNumber" : 123456789, 
        "accountNumber" : 111222333444,
        "currentYearAmount" : 6000, 
        "previousYearAmount" : 0 
    }
}

然而,Mongoose实际返回的数据是:

{
    "dateStarted":"2014-05-08T02:14:05.379Z",
    "accountCategories":["3"],
    "beneficiaries":[],
    "accountOwner":{
        "mailingAddress":{
            "address1":"123 A Street",
            "address2":"",
            "address3":"",
            "city":"A City",
            "state":"CO",
            "country":"US",
            "postalCode":"12345"
        },
        "physicalAddress":{},
        "firstName":"Test",
        "lastName":"User",
        "middleInitial":"X",
        "birthDate":null,
        "ssn":"123-45-6789",
        "phoneNumber":"(123) 456-7890"
    },
    "accountTransfer":{},
    "wizardFlags":{
       "doCashContrib":true,
       "doCashTransfer":false,
       "doCashContribAndTransfer":false,
       "doAssetTransfer":false
   },
   "__v":0,
   "accountType":1,
   "referralCode":"",
   "userId":1,
   "_id":"536ae87342d6347028b42e1e"
}

fundTransfer子对象甚至不存在,即使它绝对是mongodb。

我很困惑。我不明白为什么架构的其余部分工作,但fundsTransfer对象只是拒绝工作。数据和模式匹配,某处,我退出

1 个答案:

答案 0 :(得分:2)

AccountSchema中,您需要更改type字段定义:

type: Number,

type: {type: Number},

否则,Mongoose认为您将父fundsTransfer对象的类型定义为Number,而不是在其中定义名为'type'的字段。