Mongoose Timestamps

Mongoose Timestamps

Mongoose Timestamps

In this post, we will give you information about Mongoose Timestamps . Here we will give you detail about Mongoose Timestamps – onlinecode And how to use it also give you a demo for it if it is necessary.

Mongoose schemas have a timestamps option that tells Mongoose to automatically manage createdAt and updatedAt properties on your documents. For example, here’s how you can enable timestamps on a User model.

const userSchema = mongoose.Schema(
  {
    email: String,
  },
  { timestamps: true }
);

const User = mongoose.model("User", userSchema);

const doc = await User.create({ email: "test@google.com" });

doc.createdAt; // 2020-07-06T20:36:59.414Z
doc.updatedAt; // 2020-07-06T20:36:59.414Z

doc.createdAt instanceof Date; // true

When you enable timestamps, Mongoose adds createdAt and updatedAt properties to your schema.
By default, createdAt and updatedAt are of type Date. When you update a document, Mongoose automatically increments updatedAt.

doc.email = "sergey@google.com";
await doc.save();

doc.createdAt; // 2020-07-06T20:36:59.414Z
doc.updatedAt; // 2020-07-06T20:37:09.071Z

Specific mongoose model write operations
allow you to skip timestamps provided that timestamps were set in the schema. To do so, you must set
timestamps to false and the time will not be updated on that operation.

const userSchema = mongoose.Schema({
  email: String
}, { timestamps: true });

const User = mongoose.model('User', userSchema);

const doc = await User.findOneAndUpdate({email: 'test@google.com'}, {email:'newtest@google.com'}, 
{new:true, upsert: true, timestamps:false});

If you want to prevent only one of those
from updating, instead of setting timestamps to false as the value, create an object with key-value pairs. The
key(s) being createdAt and/or updatedAt and the value(s) being true or false depending on what you need.

const userSchema = mongoose.Schema({
  email: String
}, { timestamps: true });

const User = mongoose.model('User', userSchema);

const doc = await User.findOneAndUpdate({email: 'test@google.com'}, {email:'newtest@google.com'}, 
{new:true, upsert: true, timestamps:{createdAt:false, updatedAt:true}});

Alternate Property Names for Mongoose Timestamps

By default, Mongoose uses createdAt and updatedAt as the property names for timestamps.
But you can make Mongoose use any property name you like. For example, if you prefer snake_case
property names, you can make Mongoose use created_at and updated_at instead:

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

const userSchema = mongoose.Schema({ email: String }, opts);
const User = mongoose.model('User', userSchema);

const doc = await User.create({ email: 'test@google.com' });
doc.updated_at; // 2020-07-06T20:38:52.917Z

With Unix Timestamps for Mongoose Timestamps

Although date types are normally sufficient, you can also make Mongoose store timestamps
as seconds since January 1, 1970 (the Unix epoch).
Mongoose schemas support a timestamps.currentTime option that lets you pass a custom
function to use for getting the current time.

const opts = {
  // Make Mongoose use Unix time (seconds since Jan 1, 1970)
  timestamps: { currentTime: () => Math.floor(Date.now() / 1000) },
};

const userSchema = mongoose.Schema(
  {
    email: String,
  },
  opts
);

 

A mongoose is a small, carnivorous mammal that is found in Africa, Asia, and southern Europe. They are known for their ability to kill venomous snakes, and they have been used for centuries to control snake populations. Mongooses are also popular pets, but they can be difficult to care for and are not recommended for everyone(Mongoose Timestamps).

Here are some of the pros and cons of owning a mongoose:

Pros:

  • Mongooses are intelligent and can be trained to do tricks.
  • They are relatively small and easy to care for.
  • They can be effective at controlling snake populations.

Cons:

  • Mongooses can be aggressive and unpredictable.
  • They are not legal to own in some places.
  • They can carry diseases, such as rabies.

If you are considering getting a mongoose as a pet, it is important to do your research and make sure that you are prepared to provide the proper care. Mongooses are not for everyone, but they can make great companions for the right people.

Here are some additional facts about mongooses:

  • They are about the size of a cat, with long bodies and tails.
  • They have sharp claws and teeth, which they use to catch and kill prey.
  • They are active during the day and night.
  • They eat a variety of foods, including snakes, rodents, insects, and fruit.
  • Mongooses live in a variety of habitats, including forests, grasslands, and deserts.
  • They are solitary animals, but they will sometimes gather in groups to hunt.
  • Mongooses are monogamous, and they mate for life.
  • Females give birth to litters of 2-4 young.
  • Mongooses reach sexual maturity at about 6 months old.
  • They have a lifespan of about 10 years.

Hope this code and post will helped you for implement Mongoose Timestamps – onlinecode. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs

For More Info See :: laravel And github

We're accepting well-written guest posts and this is a great opportunity to collaborate : Contact US