search

Home  >  Q&A  >  body text

node.js - 如何使用mongoose连接数据库中已经存在的一个集合。

PHP中文网PHP中文网2877 days ago539

reply all(3)I'll reply

  • 黄舟

    黄舟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

    reply
    0
  • 高洛峰

    高洛峰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

    reply
    0
  • 大家讲道理

    大家讲道理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!

    reply
    0
  • Cancelreply