Each player can simply be represented by a document with a single field for the name:
{ name: 'leo' }
We will use Mongoose for our data modeling. Let's start with designing our user schema. The schemas are placed in the models
folder in the app. The following screenshot shows the folder structure. The schema will have one required field name
, this is done by adding required: true
to the name object in the schema.
To make querying a user by name fast, we can add an index to name
. By default, only the _id
field that MongoDB generates will be indexed. This means, to perform a search by name, the database will need to iterate over all the documents in the collection to find a matching name. When you add an index to name
, you can query by name as quickly as when you query by _id
. Now, when a user leaves, we can find the user by name directly and remove that user.
Also, we add the unique : true
property to the index to avoid having multiple users with the same name, as shown...