Mongoose 不尊重模式并插入错误的数据类型

Posted

技术标签:

【中文标题】Mongoose 不尊重模式并插入错误的数据类型【英文标题】:Mongoose not honoring schema and inserting wrong data types 【发布时间】:2014-06-25 06:07:30 【问题描述】:

我遇到了一个奇怪的问题,即 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: Cast to number failed for value 路径“fundsTransfer”中的“银行测试”

如果我排除bankName,那么实际到数据库的数据如下:

如果我使用 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"

fundsTransfer 子对象甚至不存在,即使它肯定在 mongodb 中。

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

【问题讨论】:

【参考方案1】:

AccountSchema 中,您需要将type 字段定义从:

type: Number,

type: type: Number,

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

【讨论】:

谢谢,约翰尼。实际上,我今天早上很早就弄清楚了这一点。只是其中一个“DOH!!!”时刻,当我终于明白为什么“路径”是“fundsTranfer”而不是“bankName”时。希望这会得到一些支持,以防其他人遇到类似的问题,因为这是在您遇到它之前并不明显的事情之一。

以上是关于Mongoose 不尊重模式并插入错误的数据类型的主要内容,如果未能解决你的问题,请参考以下文章

mongoose模式的类型有错误

如何使用 mongoose 和 NodeJs 在 Mongodb 的数组字段中插入数据

std::set 插入器不尊重自定义比较器。 (可能的编译器错误?)

请教各位大神,用mongoose如何获取最新插入的一条数据的id

Express nodejs不使用带表单数据的mongoose插入数据

针对 Mongoose 模式验证对象而不保存为新文档