首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >MongoDB将$jsonSchema链接到另一个$jsonSchema

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

Stack Overflow用户
提问于 2018-12-02 01:04:38
回答 2查看 1.9K关注 0票数 0

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

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

代码语言:javascript
复制
author: {
               $ref: "./models/author.js"
            },

代码语言:javascript
复制
"genre" : {
               $ref: "./models/genre.js",
               description: "must be a string and is required"
        }

然而,当我在mongo>中发布这个问题时,我会得到以下内容:

代码语言:javascript
复制
{
"ok" : 0,
"errmsg" : "$jsonSchema keyword '$ref' is not currently supported",
"code" : 9,
"codeName" : "FailedToParse"

}

我在想我怎么能做到呢?

代码语言:javascript
复制
// 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;

“手册参考资料”似乎不起作用:

代码语言:javascript
复制
$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: {

作为

代码语言:javascript
复制
{
    "ok" : 0,
    "errmsg" : "$jsonSchema keyword 'bsonType' must be either a string or an array of strings",
    "code" : 14,
    "codeName" : "TypeMismatch"
}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-12-02 05:00:32

“当前不支持$jsonSchema关键字'$ref‘”,

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

如果您需要更多的灵活性,可以在应用程序代码中查找可以使用的验证库。NPM上有几个JSON包以及其他方法,如(例如猫鼬)。

看来手册的参考资料不起作用 bsonType: ObjectId(),

这里的ObjectId()用法不是有效的JSON。您需要使用BSON型bsonType: "objectId"指定一个字符串值。

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

票数 2
EN

Stack Overflow用户

发布于 2018-12-02 05:46:45

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

代码语言:javascript
复制
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({

代码语言:javascript
复制
title:"az range gol",
author_id: original_author_id, 
summary: "shekle senasi shahname", 
isbn: NumberInt(12312), 
genre:original_genre_id

});

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53576445

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档