Categories
JavaScript Answers

How to iterate over a MongoDB cursor serially with Node.js?

Sometimes, we want to iterate over a MongoDB cursor serially with Node.js.

In this article, we’ll look at how to iterate over a MongoDB cursor serially with Node.js.

How to iterate over a MongoDB cursor serially with Node.js?

To iterate over a MongoDB cursor serially with Node.js, we can use the cursor.hasNext method.

For instance, we write

const cursor = db.collection("foo").find({});
while (await cursor.hasNext()) {
  const doc = await cursor.next();
  // ...
}

to call cursor.hasNext which returns a promise.

Then we use await to wait for hasNext to return a result.

If the promise resolves to truer, then we run the loop body.

We get cursor from making a query with find.

Conclusion

To iterate over a MongoDB cursor serially with Node.js, we can use the cursor.hasNext method.

Categories
JavaScript Answers

How to add or delete columns in Sequelize CLI?

Sometimes, we want to add or delete columns in Sequelize CLI.

In this article, we’ll look at how to add or delete columns in Sequelize CLI.

How to add or delete columns in Sequelize CLI?

To add or delete columns in Sequelize CLI, we can use the sequelize migration:create command to create a migration file.

Then we call addColumn to add a column and removeColumn to remove a column in the migration file.

For instance, we we run

sequelize migration:create --name name_of_your_migration

to create a migration file.

Then we write

module.exports = {
  up: (queryInterface, Sequelize) => {
    return queryInterface.addColumn(
      'Todo',
      'completed',
      Sequelize.BOOLEAN
    );

  },

  down: (queryInterface, Sequelize) => {
    return queryInterface.removeColumn(
      'Todo',
      'completed'
    );
  }
}

in the migration file.

We call queryInterface.addColumn with the table name, column name, and the column type to add a new column.

And we call queryInterface.removeColumn with the table name and column name to remove a column.

Conclusion

To add or delete columns in Sequelize CLI, we can use the sequelize migration:create command to create a migration file.

Then we call addColumn to add a column and removeColumn to remove a column in the migration file.

Categories
JavaScript Answers

How to wrap a buffer as a stream2 Readable stream with Node.js?

Sometimes, we want to wrap a buffer as a stream2 Readable stream with Node.js.

In this article, we’ll look at how to wrap a buffer as a stream2 Readable stream with Node.js.

How to wrap a buffer as a stream2 Readable stream with Node.js?

To wrap a buffer as a stream2 Readable stream with Node.js, we can use the stream.PassThrough method.

For instance, we write

const stream = require('stream');

const bufferStream = new stream.PassThrough();
bufferStream.end(Buffer.from('Test data.'));
bufferStream.pipe(process.stdout)

to call stream.PassThrough to create a new pass through stream instance.

Then we call end with a buffer to write the buffer into the stream.

And then we call pipe to pipe the stream into stdout.

Conclusion

To wrap a buffer as a stream2 Readable stream with Node.js, we can use the stream.PassThrough method.

Categories
JavaScript Answers

How to use Node.js Winston in several modules?

Sometimes, we want to use Node.js Winston in several modules.

In this article, we’ll look at how to use Node.js Winston in several modules.

How to use Node.js Winston in several modules?

To use Node.js Winston in several modules, we can create a module to configure the options for Winston.

Then we can require the logger module we created in the entry point file and require winston in other modules.

For instance, we create the log.js module by writing

const winston = require('winston');

winston.configure({
  level: "debug",
  format: winston.format.combine(
    winston.format.colorize(),
    winston.format.simple()
  ),
  transports: [
    new winston.transports.Console()
  ]
});

Then we in the app.js entry point file, we add

require('log.js')

And in other modules, we write

require ('winston')

so we can use the logger in any module.

Conclusion

To use Node.js Winston in several modules, we can create a module to configure the options for Winston.

Then we can require the logger module we created in the entry point file and require winston in other modules.

Categories
JavaScript Answers

How to get all the values that contains part of a string using Mongoose find?

Sometimes, we want to get all the values that contains part of a string using Mongoose find.

In this article, we’ll look at how to get all the values that contains part of a string using Mongoose find.

How to get all the values that contains part of a string using Mongoose find?

To get all the values that contains part of a string using Mongoose find, we can use the $regex operator.

For instance, we write

Books.find({
    "authors": {
      "$regex": "Alex",
      "$options": "i"
    }
  },
  (err, docs) => {}
);

to call Books.find to query for authors set to a string that has 'Alex' in it anywhere in the string.

We set $options to 'i' to compare authors in a case-insensitive manner.

Conclusion

To get all the values that contains part of a string using Mongoose find, we can use the $regex operator.