简体   繁体   中英

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. 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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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