Is there a convention to name collection in MongoDB?

后端 未结 4 1221
没有蜡笔的小新
没有蜡笔的小新 2020-12-02 07:36

I would like to know if there is a convention for database collections such as:

PageVisit or page_visit.

Are there any advantages/d

4条回答
  •  借酒劲吻你
    2020-12-02 08:17

    MongoDB has some naming conventions. One of them is that database name are case insensitive. Also, mongo will plural your collection name if not specified. "course" will become "courses".

    Since database names are case insensitive in MongoDB, database names cannot differ only by the case of the characters.

    Because of them, try to name all your collection in lowercase and without special characters. You'll avoid a lot of error - especially if you use Mongoose. Mongoose has some weird querying specificities.

    For example, if you have a collection named "courses", here's how you need to structure your model:

    const LawModel = mongoose.model(
      "course",
      new mongoose.Schema({
        id: String,
        name: String,
    
      }),
    

    Note how "course" is singular? Mongoose will plural it hence why you might see an empty array "[]". --> you are querying an unexisting collection.

    Try renaming and adjusting your model.

提交回复
热议问题