【问题标题】:How to define a boolean field in MongoDB BSON document?如何在 MongoDB BSON 文档中定义布尔字段?
【发布时间】:2018-09-17 17:27:49
【问题描述】:

我正在尝试将布尔 status 字段添加到 MongoLabs 上的文档中,但是当我尝试保存时,该特定字段出现语法错误。

根据docs,类型说明符似乎8 看起来不错,但我找不到在文档中定义布尔字段的任何示例。

我设法将错误锁定到状态字段,因为文档保存时未添加此字段。

谁能建议布尔字段的正确 BSON 语法?

这是我添加的布尔字段:

 "status": {
     {"$type": 8 } : true
}

以及完整的文档供参考:

{
    "_id": {
        "$oid": "565c4a37e4b0ed4652848949"
    },
    "email": "jd@outlook.com",
    "products": [
        {
            "productId": "0121",
            "price": 12.5,
            "description": "A generic muffin"
        },
        {
            "productId": "0122",
            "price": 13.5,
            "description": "A generic coffee"
        },
        {
            "productId": "0123",
            "price": 14.5,
            "description": "A generic tea"
        }
    ],
    "date": {
        "$date": "2014-03-03T03:45:00.000Z"
    },
     "status": {
         {"$type": 8 } : true
    }
}

【问题讨论】:

    标签: blob mongodb-.net-driver bson mlab


    【解决方案1】:

    以防其他人遇到同样的问题。 MongoLabs 上存储的数据是 JSON,然后使用驱动在本地转换为 BSON。

    下面的正确格式如下:

    {
      "status": true
    }
    

    【讨论】:

      【解决方案2】:

      如果您想知道如何在 node.js 的模型类中定义布尔值。如果您使用的是 MongoDB。

      shopOpenStatus: {
        type:Boolean,
        default:"false",
        required:true
      }
      

      我花了 10 分钟才发现这一点。

      【讨论】:

        猜你喜欢
        • 2013-08-06
        • 2018-01-19
        • 1970-01-01
        • 2021-12-14
        • 1970-01-01
        • 2021-04-17
        • 2015-09-27
        • 2014-11-12
        • 1970-01-01
        相关资源
        最近更新 更多