Pull from Nested Arrays
Pulling from nested arrays involves using the $pull operator with dot notation to specify the nested array path. You can also remove elements based on conditions within the nested array.
Example:
// app.js
const express = require('express')
const app = express();
const PORT = 8000;
const { Product, dbConnection } = require('./server')
dbConnection(); // connecting to database
async function pulling(productId, reviewer) {
const result = await Product.findByIdAndUpdate(productId,
{ $pull: { reviews: { reviewer } } });
console.log('Review by reviewer removed:', result);
}
pulling('664491772960d488ab55a620', 'nakul')
// server listening
app.listen(PORT, () => {
console.log(`Server is running on ${PORT}`)
})
Output:
How to Pull Item from an Array in Mongoose ?
In Mongoose, pulling an item from an array can be done using several methods. To pull an item from an array, you can use the $pull operator along with the updateOne() or updateMany() method.
We will discuss the different methods to pull items from an array in Mongoose
Table of Content
- Using $pull Operator
- Pull Based on a Condition
- Pull from Nested Arrays
- Using the $pop Operator
Contact Us