How to use mongoose to connect a collection that already exists in the database.

  node.js, question

In mongoose, a model is defined first and then a model. As follows:图片描述

图片描述

But now I need to directly use the existing data in mongo database. I used mongoose.collection (collection name) to connect a data collection in Figure 2 above, but I reported an error:图片描述

Beginners, a little don’t understand, also please great god advice

If you want to use mongooes to connect a data set that already exists in the mongo database, you need to add a parameter {collection: “collection name”} when defining the schema, where the collection name is the set that already exists in the database. As follows:图片描述

When defining the model afterwards, it is the same as before:图片描述

The third parameter here is to solve the problem that the set name will automatically change to complex number in the database.