node.js - 如何使用mongoose连接数据库中已经存在的一个集合。
PHP中文网
PHP中文网 2017-04-17 16:23:51
0
3
508
PHP中文网
PHP中文网

认证高级PHP讲师

reply all(3)
黄舟

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

小葫芦

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

大家讲道理

Please refer to the relevant chapters of Mongoose’s documentation:

http://mongoosejs.com/docs/qu...

For reference.

Love MongoDB! Have Fun!

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template