MongoDB 将一个 $jsonSchema 链接到另一个 $jsonSchema
MongoDB Linking a $jsonSchema to another $jsonSchema
我有两个具有 1 对 N 关系的模式。一个是书,另一个是作者。
我在下面创建了三个文件名:book.js、genre.js 和 author.js。如您所见,我在书中引用了其他文件中的流派和作者。
author: {
$ref: "./models/author.js"
},
和
"genre" : {
$ref: "./models/genre.js",
description: "must be a string and is required"
}
但是,当我在 mongo> 中发出该命令时,我得到以下信息:
{
"ok" : 0,
"errmsg" : "$jsonSchema keyword '$ref' is not currently supported",
"code" : 9,
"codeName" : "FailedToParse"
}
我想知道我该怎么做?
// book.js
var book = {
validator: {
$jsonSchema: {
bsonType: "object",
required: [ "title", "author", "summary", "isbn", "genre" ],
properties: {
title: {
bsonType: "string",
description: "must be a string and is required"
},
author: {
$ref: "./models/author.js"
},
isbn: {
bsonType: "int",
minimum: 2017,
maximum: 3017,
exclusiveMaximum: false,
description: "must be an integer in [ 2017, 3017 ] and is required"
},
"summary" : {
bsonType: "string",
description: "must be a string and is required"
},
"genre" : {
$ref: "./models/genre.js"
}
}
}
}
};
module.exports = book;
//genre.js
var genre = {
validator: {
$jsonSchema: {
bsonType: "object",
required: [ "name"],
properties: {
first_name: {
bsonType: "string",
size: 100,
description: "must be a string and is required"
},
url: {
bsonType: "string",
minLength:3,
maxLength:20,
description: "must be a string and size between [3, 100] is not required"
}
}
}
}
};
module.exports = genre;
//author.js
var Author = {
validator: {
$jsonSchema: {
bsonType: "object",
required: [ "first_name", "family_name" ],
properties: {
first_name: {
bsonType: "string",
maxLength:100,
description: "must be a string and is required"
},
family_name: {
bsonType: "string",
maxLength:100,
description: "must be a string and is not required"
},
date_of_birth: {
bsonType: "int",
minimum: 0,
maximum: 2018,
exclusiveMaximum: false,
description: "must be an integer in [ 0, 3017 ] and is required"
},
date_of_death: {
bsonType: "int",
minimum: 0,
maximum: 2018,
exclusiveMaximum: false,
description: "must be an integer in [ 0, 2018 ] and is required"
}
}
}
}
};
module.exports = Author;
手册参考似乎不起作用:
$jsonSchema: {
bsonType: "object",
required: [ "title", "author_id", "summary", "isbn", "genre" ],
properties: {
title: {
bsonType: "string",
description: "must be a string and is required"
},
author_id: {
bsonType: ObjectId(),
description: "must be a string and is required"
},
isbn: {
作为
{
"ok" : 0,
"errmsg" : "$jsonSchema keyword 'bsonType' must be either a string or an array of strings",
"code" : 14,
"codeName" : "TypeMismatch"
}
"$jsonSchema keyword '$ref' is not currently supported",
根据您遇到的错误消息,JSON Schema 的实现(在 MongoDB 4.0 中)不支持引用 ($ref
)。由于 $jsonSchema
正在数据库服务器上进行验证,因此相对文件路径不合适;您应该改为内联所需的模式验证规则。
如果您想要更大的灵活性,您可以寻找可在您的应用程序代码中使用的验证库。 NPM 上有几个 JSON 模式包以及替代方法,例如对象文档映射器(例如,Mongoose)。
It seems the Manual References does not work
bsonType: ObjectId(),
此处的 ObjectId()
用法无效 JSON。您需要使用 BSON type 指定字符串值:bsonType: "objectId"
.
有关详细信息,请参阅您服务器版本的 MongoDB 文档中的 $jsonSchema
Extensions and Omissions。
非常感谢。 bsonType:"objectId"。老实说,我不喜欢使用 Mongoose。使用手动引用:
original_author_id = ObjectId()
db.author.insert({
"_id": original_author_id,
first_name: "ghadamali",
family_name: "sarami",
date_of_birth:NumberInt(1944)
});
original_genre_id = ObjectId()
db.genre.insert({
"_id": original_genre_id,
姓名:"action",
url: "www.action.com"
});
db.book.insert({
title:"az range gol",
author_id: original_author_id,
summary: "shekle senasi shahname",
isbn: NumberInt(12312),
genre:original_genre_id
});
我有两个具有 1 对 N 关系的模式。一个是书,另一个是作者。
我在下面创建了三个文件名:book.js、genre.js 和 author.js。如您所见,我在书中引用了其他文件中的流派和作者。
author: {
$ref: "./models/author.js"
},
和
"genre" : {
$ref: "./models/genre.js",
description: "must be a string and is required"
}
但是,当我在 mongo> 中发出该命令时,我得到以下信息:
{
"ok" : 0,
"errmsg" : "$jsonSchema keyword '$ref' is not currently supported",
"code" : 9,
"codeName" : "FailedToParse"
}
我想知道我该怎么做?
// book.js
var book = {
validator: {
$jsonSchema: {
bsonType: "object",
required: [ "title", "author", "summary", "isbn", "genre" ],
properties: {
title: {
bsonType: "string",
description: "must be a string and is required"
},
author: {
$ref: "./models/author.js"
},
isbn: {
bsonType: "int",
minimum: 2017,
maximum: 3017,
exclusiveMaximum: false,
description: "must be an integer in [ 2017, 3017 ] and is required"
},
"summary" : {
bsonType: "string",
description: "must be a string and is required"
},
"genre" : {
$ref: "./models/genre.js"
}
}
}
}
};
module.exports = book;
//genre.js
var genre = {
validator: {
$jsonSchema: {
bsonType: "object",
required: [ "name"],
properties: {
first_name: {
bsonType: "string",
size: 100,
description: "must be a string and is required"
},
url: {
bsonType: "string",
minLength:3,
maxLength:20,
description: "must be a string and size between [3, 100] is not required"
}
}
}
}
};
module.exports = genre;
//author.js
var Author = {
validator: {
$jsonSchema: {
bsonType: "object",
required: [ "first_name", "family_name" ],
properties: {
first_name: {
bsonType: "string",
maxLength:100,
description: "must be a string and is required"
},
family_name: {
bsonType: "string",
maxLength:100,
description: "must be a string and is not required"
},
date_of_birth: {
bsonType: "int",
minimum: 0,
maximum: 2018,
exclusiveMaximum: false,
description: "must be an integer in [ 0, 3017 ] and is required"
},
date_of_death: {
bsonType: "int",
minimum: 0,
maximum: 2018,
exclusiveMaximum: false,
description: "must be an integer in [ 0, 2018 ] and is required"
}
}
}
}
};
module.exports = Author;
手册参考似乎不起作用:
$jsonSchema: {
bsonType: "object",
required: [ "title", "author_id", "summary", "isbn", "genre" ],
properties: {
title: {
bsonType: "string",
description: "must be a string and is required"
},
author_id: {
bsonType: ObjectId(),
description: "must be a string and is required"
},
isbn: {
作为
{
"ok" : 0,
"errmsg" : "$jsonSchema keyword 'bsonType' must be either a string or an array of strings",
"code" : 14,
"codeName" : "TypeMismatch"
}
"$jsonSchema keyword '$ref' is not currently supported",
根据您遇到的错误消息,JSON Schema 的实现(在 MongoDB 4.0 中)不支持引用 ($ref
)。由于 $jsonSchema
正在数据库服务器上进行验证,因此相对文件路径不合适;您应该改为内联所需的模式验证规则。
如果您想要更大的灵活性,您可以寻找可在您的应用程序代码中使用的验证库。 NPM 上有几个 JSON 模式包以及替代方法,例如对象文档映射器(例如,Mongoose)。
It seems the Manual References does not work
bsonType: ObjectId(),
此处的 ObjectId()
用法无效 JSON。您需要使用 BSON type 指定字符串值:bsonType: "objectId"
.
有关详细信息,请参阅您服务器版本的 MongoDB 文档中的 $jsonSchema
Extensions and Omissions。
非常感谢。 bsonType:"objectId"。老实说,我不喜欢使用 Mongoose。使用手动引用:
original_author_id = ObjectId()
db.author.insert({ "_id": original_author_id, first_name: "ghadamali", family_name: "sarami", date_of_birth:NumberInt(1944) }); original_genre_id = ObjectId()
db.genre.insert({ "_id": original_genre_id, 姓名:"action", url: "www.action.com" });
db.book.insert({
title:"az range gol",
author_id: original_author_id,
summary: "shekle senasi shahname",
isbn: NumberInt(12312),
genre:original_genre_id
});