简体   繁体   English

使用node.js驱动程序实施特定的MongoDB模式

[英]Enforce specific MongoDB schema with node.js driver

How do I make MongoDB itself or some node.js driver for MongoDB require my documents to be a subset of a certain predefined schema. 如何使MongoDB本身或MongoDB的某些node.js驱动程序要求我的文档是某些预定义架构的子集。 I've heard it's possible, but I'm so far not finding exactly how to do it. 我听说有可能,但是到目前为止我还没有找到确切的方法。

I know that mongoose.js allows you to define a schema. 我知道mongoose.js允许您定义架构。 For example: 例如:

var mongoose = require('mongoose');
var Schema = mongoose.Schema;

var blogSchema = new Schema({
  title:  String,
  author: String,
  body:   String,
  comments: [{ body: String, date: Date }],
  date: { type: Date, default: Date.now },
  hidden: Boolean,
  meta: {
    votes: Number,
    favs:  Number
  }
});

With that you can define the schema of your collection. 这样,您可以定义集合的架构。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM