在MongoDB中实施架构验证 [英] Enforcing schema validation in MongoDB

查看:44
本文介绍了在MongoDB中实施架构验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种方法来验证插入MongoDB中的数据是否与模式匹配,例如具有所有必填字段和正确的数据类型.我知道Mongo本身是无模式的,但是如果我可以在将数据传递给Mongo驱动程序之前在应用程序级别验证数据,那会很好.

I'm looking for ways to validate that data being inserted into MongoDB matches a schema, e.g. has all the required fields and correct data types. I know Mongo itself is schemaless, but if I could validate the data at the application level before passing it to the Mongo driver, that'd be good.

我看过JSON-Schema.我最大的犹豫是,我只能找到一个用于模式验证的Java库,而且我不知道是否应该信任它,我更喜欢由Apache或Google支持的库.

I've looked at JSON-Schema. My biggest hesitation there is that I can only find one Java library for schema validation, and I don't know whether I should trust it, I prefer libraries backed by Apache or Google.

我还研究了Apache Thrift,Avro和Protocol Buffers,它们并不是专门的验证框架,但它们确实具有模式的概念.我想知道是否有一种方法可以搭载其中一种方法来执行验证.

I've also looked at Apache Thrift, Avro, and Protocol Buffers, which aren't specifically validation frameworks, but they do each have a concept of a schema. I'd be interested if there's a way to piggy-back off of one of those to perform validation.

有什么建议吗?还是我应该接受Mongo的无模式性,甚至不必费心尝试验证数据?

Any suggestions? Or should I embrace the schemalessness of Mongo and not even bother trying to validate the data?

推荐答案

在即将发布的 Mongo 3.2版本中,他们添加了

In the upcoming Mongo 3.2 version they added document validation (slides).

您可以使用 validator 选项使用几乎所有mongo查询运算符($geoNear$near$nearSphere$text$where除外).您可以在我的答案之一中了解有关此内容的更多信息.

You can specify validation rules for each collection, using validator option using almost all mongo query operators (except $geoNear, $near, $nearSphere, $text, and $where). You can read more about it in one of my answers.

这篇关于在MongoDB中实施架构验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆