MongoDB 问题中的文档验证
Document validation in MongoDB issue
所以我是 MongoDB 的新手,当然,关于文档验证失败的反馈有点……没有信息。所以我希望任何人都能看到我犯的错误。
架构相对简单且有效:
db.createCollection("observations", {
autoIndexId : true,
validator : {
$jsonSchema: {
bsonType: "object",
required: [ "observationTimestamp", "family", "species", "numAnimals" ],
properties: {
observationTimestamp: {
bsonType: "long",
description: "Time that the animal(s) were observed. Use 'Date.parse()' syntax when inserting records. Required field. Example: 'Date.parse('Tue, 12 Dec 1995 16:17:18 GMT')'."
},
family: {
bsonType: "string",
description: "Vernacular name of group which species belongs to. Required field. Must be a string."
},
species: {
bsonType: "string",
description: "Scientific name of observed animal. Required field. Must be a string."
},
numAnimals: {
bsonType: "int",
minimum: 1,
description: "Number of animals observed. Required field. Must be an integer."
}
}
}
}
} );
我在这里使用 "long" 作为 'Date.parse()' 因为我了解到它 returns 一个 64 位整数。我也尝试过使用 "date" 和 "timestamp" 日期类型但无济于事。
我的插入内容是:
db.observations.insert([
{
"observationTimestamp" : Date.parse("Mon, 25 Dec 1995 12:34:56 GMT"),
"family" : "Sharks",
"species" : "Carcharodon carcharias",
"numAnimals" : 3
},
{
"observationTimestamp" : Date.parse("Tue, 12 Dec 1995 16:17:18 GMT"),
"family" : "Sharks",
"species" : "Carcharias taurus",
"numAnimals" : 4
}
]);
命令为运行时给出的错误是通用的:
BulkWriteResult({
"writeErrors" : [
{
"index" : 0,
"code" : 121,
"errmsg" : "Document failed validation",
"op" : {
"_id" : ObjectId("5a5e4291aa4da4bdfe1a234c"),
"observationTimestamp" : 819894896000,
"family" : "Sharks",
"species" : "Carcharodon carcharias",
"numAnimals" : 3
}
}
],
"writeConcernErrors" : [ ],
"nInserted" : 0,
"nUpserted" : 0,
"nMatched" : 0,
"nModified" : 0,
"nRemoved" : 0,
"upserted" : [ ]
})
这是使用 MongoDB 服务器版本 3.6。我正在 运行ning Windows 10 平台(如果这意味着什么)。
我唯一能想到的是将 bsonType 'number' 用于 numAnimals,但这也不起作用。
如有任何有用的想法或意见,我们将不胜感激!
您的验证需要 "observationTimestamp" 和 "numAnimals" 作为 int 和 long 类型。
所以在插入时关联正确的类型。
类似
db.observations.insert([
{
"observationTimestamp" : NumberLong(Date.parse("Mon, 25 Dec 1995 12:34:56 GMT")),
"family" : "Sharks",
"species" : "Carcharodon carcharias",
"numAnimals" : NumberInt(3)
}
]);
所以我是 MongoDB 的新手,当然,关于文档验证失败的反馈有点……没有信息。所以我希望任何人都能看到我犯的错误。
架构相对简单且有效:
db.createCollection("observations", {
autoIndexId : true,
validator : {
$jsonSchema: {
bsonType: "object",
required: [ "observationTimestamp", "family", "species", "numAnimals" ],
properties: {
observationTimestamp: {
bsonType: "long",
description: "Time that the animal(s) were observed. Use 'Date.parse()' syntax when inserting records. Required field. Example: 'Date.parse('Tue, 12 Dec 1995 16:17:18 GMT')'."
},
family: {
bsonType: "string",
description: "Vernacular name of group which species belongs to. Required field. Must be a string."
},
species: {
bsonType: "string",
description: "Scientific name of observed animal. Required field. Must be a string."
},
numAnimals: {
bsonType: "int",
minimum: 1,
description: "Number of animals observed. Required field. Must be an integer."
}
}
}
}
} );
我在这里使用 "long" 作为 'Date.parse()' 因为我了解到它 returns 一个 64 位整数。我也尝试过使用 "date" 和 "timestamp" 日期类型但无济于事。
我的插入内容是:
db.observations.insert([
{
"observationTimestamp" : Date.parse("Mon, 25 Dec 1995 12:34:56 GMT"),
"family" : "Sharks",
"species" : "Carcharodon carcharias",
"numAnimals" : 3
},
{
"observationTimestamp" : Date.parse("Tue, 12 Dec 1995 16:17:18 GMT"),
"family" : "Sharks",
"species" : "Carcharias taurus",
"numAnimals" : 4
}
]);
命令为运行时给出的错误是通用的:
BulkWriteResult({
"writeErrors" : [
{
"index" : 0,
"code" : 121,
"errmsg" : "Document failed validation",
"op" : {
"_id" : ObjectId("5a5e4291aa4da4bdfe1a234c"),
"observationTimestamp" : 819894896000,
"family" : "Sharks",
"species" : "Carcharodon carcharias",
"numAnimals" : 3
}
}
],
"writeConcernErrors" : [ ],
"nInserted" : 0,
"nUpserted" : 0,
"nMatched" : 0,
"nModified" : 0,
"nRemoved" : 0,
"upserted" : [ ]
})
这是使用 MongoDB 服务器版本 3.6。我正在 运行ning Windows 10 平台(如果这意味着什么)。
我唯一能想到的是将 bsonType 'number' 用于 numAnimals,但这也不起作用。
如有任何有用的想法或意见,我们将不胜感激!
您的验证需要 "observationTimestamp" 和 "numAnimals" 作为 int 和 long 类型。
所以在插入时关联正确的类型。
类似
db.observations.insert([
{
"observationTimestamp" : NumberLong(Date.parse("Mon, 25 Dec 1995 12:34:56 GMT")),
"family" : "Sharks",
"species" : "Carcharodon carcharias",
"numAnimals" : NumberInt(3)
}
]);