黄舟2017-04-17 16:25:51
If you want to use mongooes to connect to an existing data collection in the mongo database, you need to add a parameter {collection: "collection name"} when defining the schema. The collection name here is an existing collection in the database. As follows:
The definition of the model is the same as before:
The third parameter here is to solve the problem that the collection name in the database will automatically become plural
高洛峰2017-04-17 16:25:51
mongoose reads data from the database, no need for mongoose.collection('collectionName').
For complete learning, refer to the mongoose documentation. A simple example is as follows, where {} is specific conditions or data.
-- 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;
-- CRUD data --
let dataModel = require('./model.js');
dataModel.create({}, cb);
dataModel.find({}, cb);
dataModel.update({}, {}, cb);
dataModel.remove({}, cb);
soonfy
大家讲道理2017-04-17 16:25:51
Please refer to the relevant chapters of Mongoose’s documentation:
http://mongoosejs.com/docs/qu...
For reference.
Love MongoDB! Have Fun!