mongoose从数据库读取数据, 不需要mongoose.collection(‘collectionName’).
完整的学习参考mongoose文档。简单例子如下,其中{}是具体条件或者数据。
– model.js –
const mongoose = require('mongoose');
mongoose.connect('mongodb://locahost/dbName')
const dataSchema = new mongoose.Schema({});
const dataModel = mongoose.model('modelName', dataSchema, 'collectionName');
module.exports = dataModel;
我使用默认端口,现在的项目数据老是存储到到之前项目创建的集合中去,办法:
**
**const mongoose = require("mongoose");
const Schema = mongoose.Schema;
let userSchema = new Schema({
userName: {
type: String,
required: true
},
pass: {
type: String,
required: true
},
}, {
versionKey: false
});
module.exports = mongoose.model("user", userSchema, "user")**