Categories
JavaScript Answers

How to perform a full text search in Node.js MongoDB and Mongoose?

Sometimes, we want to perform a full text search in Node.js MongoDB and Mongoose.

In this article, we’ll look at how to perform a full text search in Node.js MongoDB and Mongoose.

How to perform a full text search in Node.js MongoDB and Mongoose?

To perform a full text search in Node.js MongoDB and Mongoose, we create an index on the schema.

For instance, we write

const schema = new Schema({
  name: String,
  email: String,
  profile: {
    something: String,
    somethingElse: String
  }
});

schema.index({
  name: 'text',
  'profile.something': 'text'
});

to call schema.index method to create text indexes on the name and profile.something fields.

As a result, we can do full text search on the name and profile.something fields.

Conclusion

To perform a full text search in Node.js MongoDB and Mongoose, we create an index on the schema.

Categories
JavaScript Answers

How to add a timestamp field into a Mongoose schema?

Sometimes, we want to add a timestamp field into a Mongoose schema.

In this article, we’ll look at how to add a timestamp field into a Mongoose schema.

How to add a timestamp field into a Mongoose schema?

To add a timestamp field into a Mongoose schema, we can use set the timestamps option when we create the schema.

For instance, we write

const mongoose = require('mongoose');
const {
  Schema
} = mongoose;

const schemaOptions = {
  timestamps: {
    createdAt: 'created_at',
    updatedAt: 'updated_at'
  },
};

const mySchema = new Schema({
  name: String
}, schemaOptions);

to create the mySchema schema with the Schema constructor.

We pass in schemaOptions as the 2nd argument, which has the timestamps option.

We set the createdAt and updatedAt properties to the field names of the created at and updated at timestamp fields.

Conclusion

To add a timestamp field into a Mongoose schema, we can use set the timestamps option when we create the schema.

Categories
JavaScript Answers

How to remove object from array with MongoDB?

Sometimes, we want to remove object from array with MongoDB.

In this article, we’ll look at how to remove object from array with MongoDB.

How to remove object from array with MongoDB?

To remove object from array with MongoDB, we can use the $pull operator.

For instance, we write

db.mycollection.update({
    '_id': ObjectId("5150a1199fac0e6910000002")
  }, {
    $pull: {
      items: {
        id: 23
      }
    }
  },
  false,
  true,
);

to call update with an object that has the $pull property with items.id set to 23 to remove the items entry with id 23.

We pass in false to disable upsert and pass in true to let us update multiple entries.

Conclusion

To remove object from array with MongoDB, we can use the $pull operator.

Categories
JavaScript Answers

How to respond with a JSON object in Node.js and Express?

Sometimes, we want to respond with a JSON object in Node.js and Express.

In this article, we’ll look at how to respond with a JSON object in Node.js and Express.

How to respond with a JSON object in Node.js and Express?

To respond with a JSON object in Node.js and Express, we can call the res.json method.

For instance, we add

res.json({
  anObject: {
    item1: "item1val",
    item2: "item2val"
  },
  anArray: ["item1", "item2"],
  another: "item"
});

in our route handler function to respond with

{
  anObject: {
    item1: "item1val",
    item2: "item2val"
  },
  anArray: ["item1", "item2"],
  another: "item"
}

res.json takes any plain JavaScript object and return it as a JSON string response.

Conclusion

To respond with a JSON object in Node.js and Express, we can call the res.json method.

Categories
JavaScript Answers

How to create blobs with Node.js?

Sometimes, we want to create blobs with Node.js.

In this article, we’ll look at how to create blobs with Node.js.

How to create blobs with Node.js?

To create blobs with Node.js, we can use the Buffer.from method.

For instance, we write

const buffer = Buffer.from(arrBuffer);
const arrayBuffer = Uint8Array.from(buffer).buffer;

to call Buffer.from with the arrBuffer array buffer object to create a buffer.

Then we call Uint8Array.from with a buffer to convert the buffer to an Uint8Array and get the array buffer with the buffer property.

Conclusion

To create blobs with Node.js, we can use the Buffer.from method.