Is there a way that whenever I $push a new element in monodb array, a normal _id is added to it? I remember that mongoose was doing something like that automatically but now I am using mongodb's native js and it seems to be not inserting any _id.
Example:
chats.updateOne({_id: chat_id},
{$push: {messages:
{
message: data.message,
date: new Date(),
}}},
function(err, response){}
)};
On executing, messages array should have regular _id field, message and date. Currently it only creates message and date.
You can use ObjectId() :
chats.updateOne({_id: chat_id},
{$push: {messages:
{
message: data.message,
date: new Date(),
_id: ObjectId()
}}},
function(err, response){}
)};
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.