Mongoose in the Browser
Sponsor #native_company# — #native_desc#
Mongoose supports creating schemas and validating documents in the browser. Mongoose’s browser library does not support saving documents, queries, populate, discriminators, or any other Mongoose feature other than schemas and validating documents.
Mongoose has a pre-built bundle of the browser library. If you’re bundling your code with Webpack, you should be able to import Mongoose’s browser library as shown below if your Webpack target
is 'web'
:
import mongoose from 'mongoose';
You can use the below syntax to access the Mongoose browser library from Node.js:
// Using `require()`
const mongoose = require('mongoose/browser');
// Using ES6 imports
import mongoose from 'mongoose/browser';
Using the Browser Library
Mongoose’s browser library is very limited. The only use case it supports is validating documents as shown below.
import mongoose from 'mongoose';
// Mongoose's browser library does **not** have models. It only supports
// schemas and documents. The primary use case is validating documents
// against Mongoose schemas.
const doc = new mongoose.Document({}, new mongoose.Schema({
name: { type: String, required: true }
}));
// Prints an error because `name` is required.
console.log(doc.validateSync());
当前内容版权归 mongoosejs 或其关联方所有,如需对内容或内容相关联开源项目进行关注与资助,请访问 mongoosejs .