【问题标题】:Insert command works on command line but not in pymongo Error: key '$date' must not start with '$'插入命令在命令行上有效,但在 pymongo 中无效错误:键 '$date' 不能以 '$' 开头
【发布时间】:2019-07-26 00:53:10
【问题描述】:

我正在尝试使用我之前手动插入的 pymongo 自动填充数据表。我遇到的问题是我的插入语句在命令行中工作,但在 pymongo 中抛出“key '$date' must not start with '$'”错误。注意:尝试在 mongo 命令行中使用 update() 时,我收到了同样的错误。

我正在使用 python3 和 mongodb 4.0.10

这是我的代码:

命令行查询:

device_query = {
    "_id":device_ids[i],
    "customer_id":"18012320",
    "rep":"MVO2",
    "active":"true",
    "description":descriptions[i],
    "location":"test, test" ,
    "system":"MM 60 gallon, 40 LPM, pre-HPR",
    "device_type":"O2",
    "iot_start_date":{
        "$date":{
            "$numberLong":"1563598800000"
        }
    },
    "iot_expire_date":{
        "$date":{
            "$numberLong":expirations[i]
        }
    }
}
device.insert_one(device_query)`

此代码成功插入文档

pymongo 代码:

device_query = {
    "_id":device_ids[i],
    "customer_id":"18012320",
    "rep":"MVO2",
    "active":"true",
    "description":descriptions[i],
    "location":"test, test" ,
    "system":"MM 60 gallon, 40 LPM, pre-HPR",
    "device_type":"O2",
    "iot_start_date":{
        "$date":{
            "$numberLong":"1563598800000"
        }
    },
    "iot_expire_date":{
        "$date":{
            "$numberLong":expirations[i]
        }
    }
}
device.insert_one(device_query)`

此代码抛出 键“$date”不能以“$”开头

【问题讨论】:

    标签: python python-3.x mongodb pymongo


    【解决方案1】:

    你不需要在pymongo代码中使用$date$numberLong,只需在字段中传递值即可。

    试试这个:

    device_query = {
        "_id":device_ids[i],
        "customer_id":"18012320",
        "rep":"MVO2",
        "active":"true",
        "description":descriptions[i],
        "location":"test, test" ,
        "system":"MM 60 gallon, 40 LPM, pre-HPR",
        "device_type":"O2",
        "iot_start_date":1563598800000,
        "iot_expire_date":expirations[i]
    }
    device.insert_one(device_query)
    

    【讨论】:

      猜你喜欢
      • 2016-07-05
      • 2016-12-21
      • 2012-01-31
      • 2022-01-23
      • 1970-01-01
      • 1970-01-01
      • 2017-10-04
      • 2013-10-14
      相关资源
      最近更新 更多