Categories
JavaScript Answers

How to create and use enums in Mongoose?

Sometimes, we want to create and use enums in Mongoose.

In this article, we’ll look at how to create and use enums in Mongoose.

How to create and use enums in Mongoose?

To create and use enums in Mongoose, we can set the enum property when we create our schema.

For instance, we write

const UserSchema = new Schema({
  userType: {
    type: String,
    enum: ['user', 'admin'],
    default: 'user'
  },
})

to create the UserSchema with the userType column.

We make it a string column by setting type to String.

And we set enum to an array of possible values we want the userType to have.

Also, we set the default value for userType to 'user'.

Conclusion

To create and use enums in Mongoose, we can set the enum property when we create our schema.

Categories
JavaScript Answers

How to write JSON object to a JSON file with fs.writeFileSync?

Sometimes, we want to write JSON object to a JSON file with fs.writeFileSync.

In this article, we’ll look at how to write JSON object to a JSON file with fs.writeFileSync.

How to write JSON object to a JSON file with fs.writeFileSync?

To write JSON object to a JSON file with fs.writeFileSync, we can convert the JSON object to a string before we write it.

For instance, we write

fs.writeFileSync('../data/freqs.json', JSON.stringify(output));

to call fs.writeFileSync with the JSON file path and the stringified JSON object.

We call JSON.stringify to stringify output.

Conclusion

To write JSON object to a JSON file with fs.writeFileSync, we can convert the JSON object to a string before we write it.

Categories
JavaScript Answers

How to make a collection store unique entries if the entry not null with Mongoose?

Sometimes, we want to make a collection store unique entries if the entry not null with Mongoose.

In this article, we’ll look at how to make a collection store unique entries if the entry not null with Mongoose.

How to make a collection store unique entries if the entry not null with Mongoose?

To make a collection store unique entries if the entry not null with Mongoose, we can specify a unique constraint when we define the schema.

For instance, we write

const UsersSchema = new Schema({
  name: {
    type: String,
    trim: true,
    index: true,
    required: true
  },
  email: {
    type: String,
    trim: true,
    index: {
      unique: true,
      partialFilterExpression: {
        email: {
          $type: "string"
        }
      }
    }
  }
});

to define UserSchema with the email field required to be unique for each entry.

The constraint is set by setting index.unique to true.

Conclusion

To make a collection store unique entries if the entry not null with Mongoose, we can specify a unique constraint when we define the schema.

Categories
JavaScript Answers

How to use Mongoose’s find method with $or?

Sometimes, we want to use Mongoose’s find method with $or.

In this article, we’ll look at how to use Mongoose’s find method with $or.

How to use Mongoose’s find method with $or?

To use Mongoose’s find method with $or, we can set $or to an array of properties and values we’re looking for.

For instance, we write

User.find({
    $or: [{
      '_id': objId
    }, {
      'name': param
    }, {
      'nickname': param
    }]
  },
  (err, docs) => {
    if (!err) {
      //...
    }
  });

to call User.find with an object with the $or property set to an array we’re looking for.

We’re searching for a User entry with _id set to objId or name set to param or nickname set to param.

Then callback is run when the query is done and the docs parameter has the query results.

Conclusion

To use Mongoose’s find method with $or, we can set $or to an array of properties and values we’re looking for.

Categories
JavaScript Answers

How to use Mongoose without defining a schema?

Sometimes, we want to use Mongoose without defining a schema.

In this article, we’ll look at how to use Mongoose without defining a schema.

How to use Mongoose without defining a schema?

To use Mongoose without defining a schema, we can define a field with the Mixed data type.

For instance, we write

const Any = new Schema({ any: {} });

or

const Any = new Schema({ any: Schema.Types.Mixed });

to define the Any schema that has the any property set to the mixed type by setting the any property to {} or Schema.Types.Mixed.

As a result, we can set any value as the value of any.

Conclusion

To use Mongoose without defining a schema, we can define a field with the Mixed data type.