【问题标题】:Boolean fields on mongoose not setting value猫鼬上的布尔字段未设置值
【发布时间】:2018-04-20 19:05:29
【问题描述】:

我正在为我的前端应用程序使用 mongoose、mongoDb 和 expressjs 编写小型 REST API。我在保存布尔数组对象时遇到问题。其他数组对象按预期保存在数据库中,但布尔值得到意外的 id 而不是值,除了第一个数组值。这是我的代码: 模型架构:

const companiesSchema = mongoose.Schema({
    _id: mongoose.Schema.Types.ObjectId,
    benefitsOffers: [
        {
        balance : Boolean
        },
         {
        car: Boolean
        },
        {
        bonseModel: Boolean
        }, 
        {
        centerlaOffice: Boolean
        }, 
        {
        owenerShip: Boolean
        }, 
       ]
})
module.exports = mongoose.model('Companies', companiesSchema); 

邮递员发送的测试值

    {

     "benefitsOffers": [
        {
            "balance": true

        },

        {
            "car": false

        },
        {
            "bonseModel": true

        },
        {
            "centerlaOffice": true

        },
        {
            "owenerShip": true

        },
]

}

服务器响应

 {
    "_id": "5ad93a2bd60c32622a8fce1d",
      "benefitsOffers": [
                {
                    "_id": "5ad93a2bd60c32622a8fce34",
                    "balance": true
                },
                {
                    "_id": "5ad93a2bd60c32622a8fce33"
                },
                {
                    "_id": "5ad93a2bd60c32622a8fce32"
                },
                {
                    "_id": "5ad93a2bd60c32622a8fce31"
                },
                {
                    "_id": "5ad93a2bd60c32622a8fce30"
                },

    }

我对其他数据类型有类似的数组结构,没有问题。我错过了什么? ,我参考了官方文档,它没有关于布尔数组对象的任何具体内容。我只需要有人指出我的错误

【问题讨论】:

  • Mongoose 无法像这样跟踪具有不同属性名称的数组成员。它们本质上是“不同的模式”,并且在数组成员中没有这样的支持(以这种方式)。无论如何,这作为“数组”并没有真正的意义。您要么想要[{ "type": "balance", "value": true },{ "type": "car", "value": false }]"benefitsOffers": { "balance": true, "car": false } 之类的东西。所以本质上你真的应该改变结构,因为你现在的结构真的没有意义。
  • @Nel Lunn,实际上 Mongoose 确实跟踪不同的数组成员,我用于其他数据类型,我唯一的问题是布尔值。

标签: node.js mongodb mongoose postman mongoose-schema


【解决方案1】:

这是一个解决方案

创建 benefitsOffers 作为单独的架构

const benefitsOffersSchema =  new Schema({
    balance : { type : Boolean, default: false },
    car : { type : Boolean, default: false },
    bonseModel : { type : Boolean, default: false },
    centerlaOffice : { type : Boolean, default: false },
    owenerShip : { type : Boolean, default: false }
});

附加 benefitsOffersSchemacompaniesSchema

const companiesSchema = mongoose.Schema({
    benefitsOffers : [benefitsOffersSchema]
})
const Companies = mongoose.model("Companies",companiesSchema);
module.exports = Companies;

现在您可以插入布尔值

var benefitsOffers = {
    balance : true,
    car : true,
    bonseModel : false,
    centerlaOffice : false,
    owenerShip : false
};
var company = new Companies({ benefitsOffers : benefitsOffers});
company.save();

下面是插入文档的输出

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-10
    • 1970-01-01
    • 2017-10-05
    • 1970-01-01
    • 2020-04-09
    • 1970-01-01
    相关资源
    最近更新 更多