How to create, update and delete objects from mongoose subdocuments 2022
In this article, we gonna learn how to create, update and delete objects from an array of objects.
Let's take an example or article and comments.
Article Model
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const commentSchema = new Schema(
{
comment: String
},
{
timestamps: true
}
);
const articleSchema = new Schema(
{
title: String,
description: String,
comments: [commentSchema]
},
{
timestamps: true
}
);
const Article = mongoose.model('Article', articleSchema);
module.exports = Article;
Article Stored In MongoDB
{
"title": "What is Lorem Ipsum ?",
"description": "Lorem Ipsum is simply dummy text of the printing and typesetting industry.",
"_id": "632f4d18f33af31a4f257f74",
"comments": [],
"createdAt": "2022-09-24T18:31:52.926Z",
"updatedAt": "2022-09-24T18:31:52.926Z",
"__v": 0
}
Definition
findOneAndUpdate(filter, update, options)
updateOne(filter, update, options)
Update Operators
$push - Adds an item to an array.
$pull - Removes all array elements that match a specified query.
$ - Acts as a placeholder to update the first element that matches the query condition.
$set - Sets the value of a field in a document.
Ref:- Array Update Operators
1. Add a Comment
const articleId = '632f4d18f33af31a4f257f74';
const commentObject = { comment: 'Test comment' };
await Article.findOneAndUpdate(
{ _id: articleId },
{ $push: { comments: commentObject } },
{ new: true }
);
Result:-
{
"_id": "632f4d18f33af31a4f257f74",
"title": "What is Lorem Ipsum ?",
"description": "Lorem Ipsum is simply dummy text of the printing and typesetting industry.",
"comments": [
{
"comment": "Test comment",
"_id": "63302f59d30a55a4aae6417e",
"updatedAt": "2022-09-25T10:37:13.290Z",
"createdAt": "2022-09-25T10:37:13.290Z"
}
],
"createdAt": "2022-09-24T18:31:52.926Z",
"updatedAt": "2022-09-25T10:37:13.290Z",
"__v": 0
}
2. Update a Comment
const commentId = '63302f59d30a55a4aae6417e';
const comment = 'Updated test comment';
await Article.findOneAndUpdate(
{ 'comments._id': commentId },
{ $set: { 'comments.$.comment': comment } },
{ new: true }
);
Result:-
{
"_id": "632f4d18f33af31a4f257f74",
"title": "What is Lorem Ipsum ?",
"description": "Lorem Ipsum is simply dummy text of the printing and typesetting industry.",
"comments": [
{
"comment": "Updated test comment",
"_id": "63302f59d30a55a4aae6417e",
"updatedAt": "2022-09-25T10:39:16.605Z",
"createdAt": "2022-09-25T10:37:13.290Z"
}
],
"createdAt": "2022-09-24T18:31:52.926Z",
"updatedAt": "2022-09-25T10:39:16.605Z",
"__v": 0
}
3. Delete a Comment
const commentId = '63302f59d30a55a4aae6417e';
await Article.findOneAndUpdate(
{ 'comments._id': commentId },
{ $pull: { comments: { _id: commentId } } },
{ new: true }
);
Result:-
{
"_id": "632f4d18f33af31a4f257f74",
"title": "What is Lorem Ipsum ?",
"description": "Lorem Ipsum is simply dummy text of the printing and typesetting industry.",
"comments": [],
"createdAt": "2022-09-24T18:31:52.926Z",
"updatedAt": "2022-09-25T10:43:13.245Z",
"__v": 0
}
Leave Your Comment