MongoDB将一个$jsonSchema链接到另一个$jsonSchema

MongoDB将一个$jsonSchema链接到另一个$jsonSchema,mongodb,jsonschema,ref,Mongodb,Jsonschema,Ref,我有两个1到N关系的模式。一个是书,另一个是作者 我在下面列出了三个文件名:book.js、genre.js和author.js。如你所见,我在书中引用了其他文件中的流派和作者 author: { $ref: "./models/author.js" }, 及 但是,当我在mongo>中发布时,我得到以下信息: { "ok" : 0, "errmsg" : "$jsonSchema keyword '$ref' is not curren

我有两个1到N关系的模式。一个是书,另一个是作者

我在下面列出了三个文件名:book.js、genre.js和author.js。如你所见,我在书中引用了其他文件中的流派和作者

author: {
               $ref: "./models/author.js"
            },

但是,当我在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: {
作为

“$jsonSchema关键字“$ref”当前不受支持”

根据您遇到的错误消息,JSON模式的实现(与MongoDB 4.0一样)不支持引用(
$ref
)。由于正在数据库服务器上验证
$jsonSchema
,因此相对文件路径不合适;相反,您应该内联所需的架构验证规则

如果您想要更大的灵活性,可以寻找一个可以在应用程序代码中使用的验证库。NPM上有几个JSON模式包以及其他方法,如对象文档映射器(例如)

看来手动参考不起作用

     bsonType: ObjectId(),
此处的
ObjectId()
用法不是有效的JSON。您需要使用以下命令指定字符串值:
bsonType:“objectId”

有关更多信息,请参见服务器版本的MongoDB文档中的
$jsonSchema

“$jsonSchema关键字“$ref”当前不受支持”

根据您遇到的错误消息,JSON模式的实现(与MongoDB 4.0一样)不支持引用(
$ref
)。由于正在数据库服务器上验证
$jsonSchema
,因此相对文件路径不合适;相反,您应该内联所需的架构验证规则

如果您想要更大的灵活性,可以寻找一个可以在应用程序代码中使用的验证库。NPM上有几个JSON模式包以及其他方法,如对象文档映射器(例如)

看来手动参考不起作用

     bsonType: ObjectId(),
此处的
ObjectId()
用法不是有效的JSON。您需要使用以下命令指定字符串值:
bsonType:“objectId”


有关更多信息,请参见服务器版本的MongoDB文档中的
$jsonSchema

非常感谢。bsonType:“objectId”。老实说,我不喜欢用猫鼬。使用手动参考:

original_author_id = ObjectId()
db.author.insert({ “\u id”:原始作者\u id, 名字:“ghadamali”, 姓:“萨拉米”, 出生日期:NumberPrint(1944年) }); 原始类型id=ObjectId()

db.genre.insert({ “\u id”:原始\u类型\u id, 名称:“行动”, 网址:“www.action.com” });

db.book.insert({


}))

非常感谢。bsonType:“objectId”。老实说,我不喜欢用猫鼬。使用手动参考:

original_author_id = ObjectId()
db.author.insert({ “\u id”:原始作者\u id, 名字:“ghadamali”, 姓:“萨拉米”, 出生日期:NumberPrint(1944年) }); 原始类型id=ObjectId()

db.genre.insert({ “\u id”:原始\u类型\u id, 名称:“行动”, 网址:“www.action.com” });

db.book.insert({


}))

你在说什么?我在节点应用程序中使用MongoDB。我当然会在应用程序端对表单或任何有意义的东西进行验证。为什么要在终端中进行验证?>ObjectId的类型(“5c036dd47d0db45bee0f64df”)=“object”true>ObjectId的类型(“5c036dd47d0db45bee0f64df”)=“ObjectId”false@chikitin我指的是MongoDB中的
$jsonSchema
的扩展和省略实现,如果您正在寻找使用NPM上的引用进行应用程序验证,那么就需要更通用的JSON模式支持。
ObjectId()
typeof
值应为
对象,因为本机JavaScript不知道类型。
mongo
shell和扩展的JSON语法提供了帮助函数,可以在适当的情况下使用扩展类型,但不扩展内置的JavaScript类型?我在节点应用程序中使用MongoDB。我当然会在应用程序端对表单或任何有意义的东西进行验证。为什么要在终端中进行验证?>ObjectId的类型(“5c036dd47d0db45bee0f64df”)=“object”true>ObjectId的类型(“5c036dd47d0db45bee0f64df”)=“ObjectId”false@chikitin我指的是MongoDB中的
$jsonSchema
的扩展和省略实现,如果您正在寻找使用NPM上的引用进行应用程序验证,那么就需要更通用的JSON模式支持。
ObjectId()
typeof
值应为
对象,因为本机JavaScript不知道类型。
mongo
shell和扩展的JSON语法提供了帮助函数,可以在适当的情况下使用扩展的类型,但不扩展内置的JavaScript类型。
title:"az range gol",
author_id: original_author_id, 
summary: "shekle senasi shahname", 
isbn: NumberInt(12312), 
genre:original_genre_id