The object array in Mongoose efficiently stores duplicate date and time values

Here's an example of my mongoose schema:

const clothesSchema = new mongoose.Schema({
  clothes: [{
    _id: {
      type: mongoose.Schema.Types.ObjectId,
      ref: 'Clothes'
    },
    dateAdded: {
     type: Date,
     default: Date.now
   }
 }]
});

Currently, if I add a new object to the clothes array within one minute, it will have the same date and time.

"clothes" : [ 
{
    "_id" : ObjectId("5e3bcc9434a40d46e7eb2b25"),
    "dateAdded" : ISODate("2021-01-05T10:50:19.979Z")
}, 
{
    "_id" : ObjectId("5c0599f6975b274a7654702a"),
    "dateAdded" : ISODate("2021-01-05T10:50:19.979Z")
}, 
{
    "_id" : ObjectId("5d8cce493eb29c6e5af6be07"),
    "dateAdded" : ISODate("2021-01-05T10:50:19.979Z")
}, 
{
    "_id" : ObjectId("5db8344a1689662efbc6d816"),
    "dateAdded" : ISODate("2021-01-05T11:02:47.352Z")
}
]

I would greatly appreciate your assistance with this matter.

Answer №1

It's interesting to note that I've encountered a similar unusual occurrence before.

This can be attributed to the fact that you have set the initial value of the dateAdded field as Date.now.

To resolve this, simply remove the default value and instead include new Date() when adding a new entry to the array.

Following these steps should effectively address the problem at hand.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Put the value of req.body inside quotation marks

Recently, I've been troubleshooting an issue with my Express REST API where the JSON body is being populated with unnecessary quotation marks. In my Express middleware setup, I have configured it as follows: app.use(bodyParse.urlencoded({ extended: ...

Issue: Headers cannot be set after they have been sent. This is a problem in node.js

I'm trying to create an application that allows users to execute commands via a URL, but I keep encountering this error message: _http_outgoing.js:346 throw new Error('Can\'t set headers after they are sent.'); ^Error: Can't ...

Using Node.js to retrieve a user's group memberships with NTLM authentication

I am currently utilizing express-ntlm for domain controller authentication, which is working smoothly. However, I now need to access the group details of the authenticated user, with authorization limited to a specific group. I have come across some examp ...

When I activate CSRF, Express4 and Formidable file upload functionality stops working

I have been following along with the content in Ethan Brown's "Web Development with Node and Express" book, and everything was going smoothly until I encountered an issue while trying to enable CSRF for the multipart/form-data upload on a photo submis ...

Tips for running numerous telegram bots on a single port?

I currently have 6 bots that I would like to run on the same server. However, when I attempt to run the second bot, I encounter the following error message: address already in use :::8443 I am aware that I can only utilize 4 ports (80, 88, 443, 8443) for ...

Creating MySQL query results in Node.js manufacturing process

I am looking to format the MySQL result in Node.js in a specific way. The desired result format should look like this: [ { "m_idx" :1 , "contents" : { "m_name" : "a", ...

Angular page startup triggers NPM, leading to a sudden crash

Our ASP.Net + Angular web pages running on the IIS server (built with .Net Core 2.1 and Angular5) have suddenly stopped functioning. An error message "AggregateException: One or more errors occurred. (One or more errors occurred. (The NPM script 'sta ...

Can we program a system that automatically deletes messages that do not include the text "ew"?

Currently developing a basic server with a unique feature - responding with "ew" in the chat. Is there a way to automatically delete any message that does not include the word "ew"? Appreciate your help! ...

What is the best way to list the choices associated with a specific category?

The Node.js package I'm currently working with requires an argument of a specific type, which I can see is defined through a TypeScript declaration as follows: export declare type ArgType = 'A' | 'B' | 'C'; I am interes ...

What is the correct way to exclude a Mongoose model field from logging functions?

When I am working with schemas and models using Mongoose, there is a password field that I must remove before providing a user to an API. To achieve this, I need to perform the following steps: var user = JSON.parse(JSON.stringify(mongooseUserModel)); de ...

Buffer array containing MySql image fetched using Node.js

Can you assist me with this issue? I have already stored some images in my database in a column with a type of BLOB (.png images). However, when I make a request (testing it using POSTMAN), I receive a JSON response with a buffer array containing numbers. ...

How can querystrings be effectively managed within an express API?

As a novice nodejs developer, I am currently working on creating an express rest api that supports optional query parameters. For instance, let's consider the user schema below: phone: { countryCode: String, number: phoneType }, phoneVerified: { ...

The ordering of imports is not followed by prettier-plugin-sort-imports

Currently, I am utilizing @trivago/prettier-plugin-sort-imports version 4.0.0 along with prettier version 2.6.2. However, it appears that the plugin is not adhering to my specifications or perhaps I am unable to correctly define my requirements. Here' ...

The custom domain for my Openshift Nodejs App isn't appearing as expected. [+process.env.OPENSHIFT_APP_DNS]

I have a ghost blog hosted on OpenShift, and I successfully set up custom domains for my site. Everything is working well, except for the subscribe link and some other menu links that still point to the default OpenShift URL ghost-nodejs.rhcloud.com instea ...

Consistently fluctuating eTag in motion

My Node Express API has a default ETag configuration, but it keeps generating a new ETag every time I test hitting the server, even when the response content remains the same. I've compared the headers and tested with both API and static HTML content ...

Interval function failing to update information on Jade template

Currently, I am working on a Node app using Express and Jade. My aim is to retrieve JSON data from an API and have it refresh on the page periodically. To achieve this, I have created an empty div where I intend to inject the contents of a different route/ ...

What is the best way to retrieve a value from a database and display it in a radio

Can you help me figure out how to implement this functionality? I have a database with a "boolean" field that stores 0 or 1 values. On an HTML page, there is a radioButton with options for OK or NO. I need to dynamically load the boolean value from the dat ...

Tips on keeping track of req.ip using bouncy

I have implemented bouncy to handle two websites through two distinct processes (a Ghost blog and an Express web app). bouncy(function(req, bounce) { if (req.headers.host === 'blogdomain.com' || req.headers.host === 'www.blogdomain.com&ap ...

Issue encountered: missing photo data during the transfer of an image from ReactJS to MongoDB

When an image is uploaded to my website, it is sent to the backend for storage in MongoDB database. The base64 of the image is uploaded to the database, but occasionally the photo data retrieved can be "false". For example: (3) [{…}, {…}, {…}] 0 : p ...

Why does the nodejs command line consistently install outdated versions?

Here are my questions: Every time I try to install nodejs using the command line # apt-get install nodejs it always installs an older version (in my case, it installed 0.10.x), even though the latest version available is 4.1.2. Why does this command no ...