How to access a preexisting collection with Mongoose?

前端 未结 6 2108
终归单人心
终归单人心 2020-11-28 00:53

I have a large collection of 300 question objects in a database test. I can interact with this collection easily through MongoDB\'s interactive she

6条回答
  •  被撕碎了的回忆
    2020-11-28 01:35

    Mongoose added the ability to specify the collection name under the schema, or as the third argument when declaring the model. Otherwise it will use the pluralized version given by the name you map to the model.

    Try something like the following, either schema-mapped:

    new Schema({ url: String, text: String, id: Number}, 
               { collection : 'question' });   // collection name
    

    or model mapped:

    mongoose.model('Question', 
                   new Schema({ url: String, text: String, id: Number}), 
                   'question');     // collection name
    

提交回复
热议问题