How to rename the collection – MongoDB

In MongoDB, you can use the db.runCommand() method to rename a collection. The db.runCommand() method takes a document as its argument, and the document must contain the following props:

  • renameCollection: The name of the collection to be renamed.
  • to: The new name of the collection.

For example, to rename the collection your_database.your_table to your_database.your_new_table_name, you would use the following command:

db.runCommand({renameCollection: "your_database.your_table", to: "your_database.your_new_table_name"});

If the rename operation was successful, the db.runCommand() method will return a document with the status of the operation.

  ok: 1,
  '$clusterTime': {
    clusterTime: Timestamp({ t: 1689910863, i: 27 }),
    signature: {
      hash: Binary(Buffer.from("3f999f9155064aa974469f94304958eba5eef4b3", "hex"), 0),
      keyId: Long("7200478234696220854")
  operationTime: Timestamp({ t: 1689910863, i: 27 })

Here are some additional things to keep in mind when renaming a collection:

  • You can only rename a collection that exists in the current database.
  • You cannot rename a collection to a name that already exists in the database.
  • If you rename a collection that is being used by a running application, the application may need to be restarted in order to see the changes.

ORM thoughts?

People might have confusion when we start to think about traditional ORM like sequelize.

Got the explanation “Sequelize vs Prisma” from Prisma

While Prisma and Sequelize solve similar problems, they work in very different ways.

Sequelize is a traditional ORM which maps tables to model classes. Instances of the model classes then provide an interface for CRUD queries to an application at runtime.

Prisma is a new kind of ORM that mitigates many problems of traditional ORMs, such as bloated model instances, mixing business with storage logic, lack of type-safety or unpredictable queries caused e.g. by lazy loading.

It uses the Prisma schema to define application models in a declarative way. Prisma Migrate then allows to generate SQL migrations from the Prisma schema and executes them against the database. CRUD queries are provided by Prisma Client, a lightweight and entirely type-safe database client for Node.js and TypeScript.

Battle of the Node.js ORMs: Objection vs. Prisma vs. Sequelize

The first impression of Objection.js is more like a query builder instead of a common ORM. The way of defining models and relationships can be nicely achieved with custom methods, json and schema validation. We can also do Database transactions easily with promise.

const people = await Person.query()
  .select(' as grandParentName')

select "parent:parent"."firstName" as "grandParentName"
from "persons"
inner join "persons"
  as "parent"
  on "parent"."id" = "persons"."parentId"
inner join "persons"
  as "parent:parent"
  on "parent:parent"."id" = "parent"."parentId"

The eager loading is quite interesting too. “withGraphFetched and withGraphJoined. The main difference is that withGraphFetched uses multiple queries under the hood to fetch the result while withGraphJoined uses a single query and joins to fetch the results. Both methods allow you to do different things which we will go through in detail in the examples below and in the examples of the withGraphJoined method.”