mongoimport 尝试导入带有日期字段的 json 文件时出错

问题描述 投票:0回答:1

我使用以下 mongoschema 创建了集合“mycollection”

db.createCollection("testing",{
    validator: {
        $jsonSchema: {
            bsonType: "object",
            required: ["date"],
            properties: {
                date: {
                    bsonType: "date",
                    description: "must be a date and is required"
                }
            }
        }
    } })

我想使用 mongoimport 将下面的 json 文件“datefile.json”导入到我创建的上面的集合中

[
    {
      "date": "2024-04-07T12:00:00Z"
    },
    {
      "date": "2024-04-08T10:30:00Z"
    },
    {
      "date": "2024-04-09T08:45:00Z"
    }
  ]

但是,我收到此错误

"2024-04-07T14:30:57.453-0400    connected to: mongodb://localhost/
2024-04-07T14:30:57.459-0400    continuing through error: Document failed validation
2024-04-07T14:30:57.459-0400    continuing through error: Document failed validation
2024-04-07T14:30:57.459-0400    continuing through error: Document failed validation
2024-04-07T14:30:57.460-0400    0 document(s) imported successfully. 3 document(s) failed to import." 

当我输入以下命令时

mongoimport --db mydatabase--collection mycollection --file datefile.json --jsonArray
.

我的问题是如何为上面创建的模式创建 json 文件

json mongodb pymongo mongoimport
1个回答
0
投票

我找到了解决办法。 json 应该如下所示:-

[
    {
      "date": { "$date": "2024-04-07T12:00:00Z" }
    },
    {
      "date": { "$date": "2024-04-08T10:30:00Z" }
    },
    {
       "date": { "$date": "2024-04-09T08:45:00Z" }
    }
  ]
© www.soinside.com 2019 - 2024. All rights reserved.