这是一个样本
app/models/item.js
var mongoose = require("mongoose");var ItemSchema = new mongoose.Schema({ name: { type: String, index: true }, equipped: Boolean, owner_id: { type: mongoose.Schema.Types.ObjectId, index: true }, room_id: { type: mongoose.Schema.Types.ObjectId, index: true }});var Item = mongoose.model('Item', ItemSchema);module.exports = { Item: Item}要从项目控制器中加载它,
app/controllers/items.js我会这样做
var Item = require("../models/item").Item; //Now you can do Item.find, Item.update, etc换句话说,在模型模块中定义模式和模型,然后仅导出模型。使用相对需求路径将模型模块加载到控制器模块中。
要建立连接,请在服务器启动代码(
server.js或类似内容)的早期进行处理。通常,您需要从配置文件或环境变量中读取连接参数,如果未提供任何配置,则默认为开发模式localhost。
var mongoose = require('mongoose');mongoose.connect('mongodb://localhost');


