MongoDb - Update array of json objects - node.js

I would like to update an object from a JSON objects array. Here is the schema
qualifications:[ {
Experience: [{
title: String,
companyName: String,
location: String,
years: Number
}],
Education:[ {
school: String,
years: Number,
}],
Licences: [String],
Honnors: [String],
}],
For example how can I push an object to the Education array? This is what i have tried so far.
const updateEducation = async (req, res) => {
try {
const user = await User.findOneAndUpdate(
{ _id: req.body.userid },
{
$push: {
qualifications:{
Education: {
school: req.body.educationSchool,
years: req.body.educationYearText
}
}
},
},
{ new: true }
);
And then i use this to remove an object
const deleteEducation = async (req, res) => {
try {
const user = await User.findOneAndUpdate(
{ _id: req.body.userid },
{
$pull: {
"qualifications.Education": {
school: req.body.school
}
},
}
);
But unfortunately in the update function i get "error": "Plan executor error during findAndModify :: caused by :: The field 'qualifications' must be an array but is of type object in document
what is wrong?

The qualifications property of the User is an array of arrays, in other words, 2D array. And subarray receives data in an object. You are trying to access Education by object keys. But Education is in index 1 of qualifications array.
const updateEducation = async (req, res) => {
try {
const user = await User.findOneAndUpdate({ _id: req.body.userid });
user.qualifications[1].push({
school: req.body.educationSchool,
years: req.body.educationYearText
});
await user.save();
} catch (err) {
// handle error here...
}
);

Related

Mongoose - findByIdAndUpdate

All,
I can seem to figure out why the record in the database will not update. I am not 100% sure where my error is but this isn't really providing me a great error message. Can someone please take a look at this for me?
I believe that I am calling the mongoose request properly. Thank you in advance!
$ npm mongoose -v
8.15.0
const mongoose = require("mongoose");
const CartSchema = new mongoose.Schema(
{
owner: {
type: String,
unique: true,
required: true,
},
discount: {
type: Number,
},
total: {
type: Number,
},
items: [
{
itemId: {
type: Number,
},
sku: {
type: Number,
},
quantity: {
type: Number,
},
price: {
type: Number,
},
},
],
},
{ timestamps: true }
);
const Cart = mongoose.model("Cart", CartSchema);
module.exports = Cart;
Record in Database
{"_id":{"$oid":"630689708997a6589635986c"},
"owner":"611afa8b9069c9126cff3357",
"total":{"$numberInt":"0"},
"items":[],
"createdAt":{"$date":{"$numberLong":"1661372784844"}},
"updatedAt":{"$date":{"$numberLong":"1661372784844"}},
"__v":{"$numberInt":"0"}}
exports.add = async (req, res, next) => {
const { id, product } = req.body;
const addItem = { itemId: product._id, sku: product.sku, quantity: 1, price: product.price };
console.log(addItem);
try {
const updateCart = Cart.findByIdAndUpdate(id, { $addToSet: { items: addItem } }, { new: true, returnDocument: "after" });
if (!updateCart) return next(new ErrorResponse("Unable to update the cart record", 404));
console.log(updateCart);
if (updateCart) {
return sendRes(updateCart, 200, res);
} else {
return sendRes(updateCart, 201, res);
}
} catch (error) {
console.log(error);
next(error);
}
};
This issue was caused by me using an ASYNC Function without the AWAIT on the DB Call.
Please try once with this:
Cart.findByIdAndUpdate(id, { $addToSet: { items: addItem } }, { new: true, returnDocument: "after" });
The first obvious mistake is that you're searching for a document with the wrong field:"id", Kindly change that to "_id: id"
Also you might need to convert the _id string you have to MongoDB Object ID, like this:
const ObjectId = require('mongodb').ObjectId;
Cart.updateOne({_id: new ObjectId(id)}, { $addToSet: { items: addItem } }, { new: true, returnDocument: "after" });
For other update method, you need to specify the field, and also convert it to a MongoDB ID
OR
Cart.findByIdAndUpdate(id, { $addToSet: { items: addItem } }, { new: true, returnDocument: "after" })
You do not to specify the field in findByIdAndUpdate, just pass the id to it.

How do I update nested array values in mongoose?

I am fairly new to nodejs/express and I'm practicing full stack development with devchallenges.io, i'm doing the shoppingify challenge. I'm trying to update the quantity of an item I am targeting inside of the items array. I understand my attempt below was terrible, I'm really struggling to understand the logic to be able to do so.
// #route PUT api/list/item/quantity/:id
// #desc Increase or decrease quantity
// #access Private
router.put('/item/quantity/:id', auth, async (req, res) => {
const { action } = req.body;
try {
let list = await List.findOne({ user: req.user.id });
const item = list.items.find(
(item) => item._id.toString() === req.params.id
);
list = list.updateOne(
{ 'items._id': req.params.id },
{ $set: { 'items.quantity': item.quantity + 1 } }
);
await list.save();
return res.json(list);
} catch (error) {
console.error(error.message);
res.status(500).send('Server Error');
}
});
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const ListSchema = new Schema({
user: {
type: Schema.Types.ObjectId,
},
name: {
type: String,
default: 'Shopping List',
},
items: [
{
name: {
type: String,
default: '',
},
note: {
type: String,
default: '',
},
image: {
type: String,
default: '',
},
category: {
type: String,
default: '',
},
quantity: {
type: Number,
default: 1,
},
},
],
date: {
type: Date,
default: Date.now,
},
});
module.exports = List = mongoose.model('list', ListSchema);
Look this is my update-vendor route here I'm updating nested street and city name.
router.put("/update-vendors", async (req, res, next) => {
const vendor = await Vendor.updateOne(
{
"address.street": "Street2",
},
{
$set: {
"address.$.street": req.body.street,
"address.$.city": req.body.city,
},
}
);
res.status(200).json(vendor);
});
You can update particular things with the help of $set and other $push method

MongoDB array of objects update

I'm trying to update array of user information objects in mongoose.
I've stored the user core information in the login process, I want to update some of user information when user tries to make an order.
Here is the code for the model
const mongoose = require('mongoose');
const { ObjectId } = mongoose.Schema;
const userSchema = new mongoose.Schema(
{
name: String,
email: {
type: String,
required: true,
index: true,
},
role: {
type: String,
default: 'subscriber',
},
info: [
{ country: String },
{ city: String },
{ address: String },
{ phone: String },
{ birthdate: Date },
{ gender: { type: String, enum: ['Male', 'Female'] } },
],
// wishlist: [{ type: ObjectId, ref: "Product" }],
},
{ timestamps: true }
);
module.exports = mongoose.model('User', userSchema);
In my controller I'm getting the data from front-end react app as JSON format, I want to push some data to info which is an array of objects in the users model above.
exports.createOrder = async (req, res) => {
// Here I constract the data
const { plan, service, fullName, country, city, address } = req.body.order;
const { user_id } = req.body;
// This the method I tried
try {
const user = await User.updateOne(
{
_id: user_id,
},
{
$set: {
'info.$.country': country,
'info.$.city': city,
'info.$.address': address,
},
},
{ new: true }
);
if (user) {
console.log('USER UPDATED', user);
res.json(user);
} else {
res.json((err) => {
console.log(err);
});
}
const newOrder = await new Order({
orderPlan: plan,
orderService: service,
orderUser: user_id,
}).save();
console.log(newOrder);
console.log(req.body);
} catch (error) {
console.log(error);
}
};
I tired other solutions like
const user = await User.updateOne(
{
_id: user_id,
info: { $elemMatch: { country, city, address } },
},
{ new: true }
);
So do I need to reformat my model or there is a way to update this array of objects?
Option 1
Use $[]
db.collection.update(
{},
{ $set: { "info.$[i].country": "a1" }} ,
{ arrayFilters: [ { "i.country": "a" } ] }
)
Demo - https://mongoplayground.net/p/UMxdpyiKpa9
Option 2
if you know the index
Demo - https://mongoplayground.net/p/41S7qs6cYPT
db.collection.update({},
{
$set: {
"info.0.country": "a1",
"info.1.city": "b1",
"info.2.address": "c1",
"info.3.phone": "d1"
}
})
Suggestions -
Change info schema to object instead of an array

updating subdocument array in mongoose express

So I have a schema, which is like this:
const playerSchema = new Schema(
{
user: {
type: Schema.Types.ObjectId,
ref: 'user'
},
paid : {type: Boolean, default: false}
}
)
const tournamentSchema = new Schema(
{
name: {
type: String,
required: true
},
player:[ playerSchema])
so in the tournament model i get this as return:
{
"_id": "5fbf3afe1ecd92296c746db6",
"name": "1st Testing Tournament",
"player": [
{
"paid": false,
"_id": "5fbf3cfe6347784028da8181",
"user": "5fbf3b4c1ecd92296c746dcd"
}
]}
in the API I will have only the user ID and the tournament ID. I want to update paid in players array from false to true. Here is what I tried:
exports.put_confirmPayment= async(req, res)=>{
const uid = req.params.user_id
const tid = req.params.tid
const findData= {"_id": tid, "player.user": uid }
const changeData = {"player.$.paid": "true"}
try {
await Tournament.findOneAndUpdate( findData, {$set: {changeData}} )
const tDB = await Tournament.findById(tid)
res.status(200).json(tDB)
} catch (error) {
console.log(error)
res.status(500).json(error.message)
}
}
Where I am going wrong? and what should my approach be?
convert string id from string to object type using mongoose.Types.ObjectId
change "true" string to boolean true
return updated result using returnOriginal: false, or new: true both will return new updated result
have removed extra constant variables, don't create too much variables
exports.put_confirmPayment = async(req, res) => {
try {
const tDB = await Tournament.findOneAndUpdate(
{
_id: mongoose.Types.ObjectId(req.params.tid),
"player.user": mongoose.Types.ObjectId(req.params.user_id)
},
{ $set: { "player.$.paid": true } },
{ returnOriginal: false }
);
res.status(200).json(tDB);
} catch (error) {
console.log(error);
res.status(500).json(error.message);
}
}
Playground
For more information refer mongoose findoneandupdate documentation.

Updating a double nested array MongoDB

Consider this schema:
let userSchema = new mongoose.Schema({
id: String,
displayName: String,
displayImage: String,
posts: [
{
url: String,
description: String,
likes: [String],
comments: [
{ content: String, date: String, author: { id: String, displayName: String, displayImage: String } }
]
}
]
});
I am able to delete a certain item from the comments array using this query
controller.deleteComment = (req, res, next) => {
User.findOneAndUpdate(
{ id: req.query.userid, 'posts._id': req.params.postid, },
{
$pull: {
'posts.$.comments': { _id: req.body.commentID },
}
}
)
.exec()
.then(() => {
res.send('deleted');
})
.catch(next);
};
Is there anyway that I can UPDATE an element inside the comments array by using the $set operator? I need to changed the contents of a comment based on its comment ID.. something like this:
controller.editComment = (req, res, next) => {
User.findOneAndUpdate(
{ id: req.query.userid, 'posts._id': req.params.postid, 'comments._id':req.body.commentID },
{
$set: {
'posts.$.comments': { content: req.body.edited },
}
}
)
.exec()
.then(() => {
res.send('deleted');
})
.catch(next);
};
This ^ is obviously not working but I'm wondering if there is a way I can do this?
UPDATE
As per suggestions below I am doing the following to manage only one Schema. This works, however only the first post's comments get updated regardless of which posts comments I am editing. I have checked and the return docs are always correct. There must be a problems with the doc.save() method.
controller.editComment = (req, res, next) => {
User.findOne(
{ id: req.query.userid, 'posts._id': req.params.postid },
{ 'posts.$.comments._id': req.body.commentID }
)
.exec()
.then((doc) => {
let thisComment = doc.posts[0].comments.filter((comment) => { return comment._id == req.body.commentID; });
thisComment[0].content = req.body.edited;
doc.save((err) => { if (err) throw err; });
res.send('edited');
})
.catch(next);
};
I don't know a simple (or even tough :P) way to achieve what are trying to do. In mongo, manipulation is comparatively tough in doubly nested arrays and hence, best to be avoided.
If you are still open for schema changes, I would suggest you create a different schema for comments and refer that schema inside user schema.
So your comment schema will look like this:
let commentSchema = new mongoose.Schema({
content: String,
date: String,
author: {
id: String,
displayName: String,
displayImage: String
}
});
And you user schema should look like this:
let userSchema = new mongoose.Schema({
id: String,
displayName: String,
displayImage: String,
posts: [{
url: String,
description: String,
likes: [String],
comments: [{
type: Schema.Types.ObjectId,
ref: 'comment' //reference to comment schema
}]
}]
});
This way your data manipulation will be a lot easier. You can populate comments while fetching user document. And, notice how easy update/delete operations are, given that you already know that _id of the comments you want to update.
Hope you find this answer helpful!
controller.editComment = (req, res, next) => {
User.findOneAndUpdate(
{ id: req.query.userid, 'posts._id': req.params.postid, 'comments._id':req.body.commentID },
{
$push: {
'posts.$.comments': { content: req.body.edited },
}
}
)
.exec()
.then(() => {
res.send('deleted');
})
.catch(next);
};

Resources