Mongoose Set object property in array of objects - node.js

I have a document consisting of a Post. Each Post has an array of Comments, which are an object each. So my document looks like this
Now, I want to be able to update the message property in a given Comment object.
So I'll be using the $set method, but how would I be able to select the specific object. Currently, my unfinished method looks like this
export const editComment = async (req, res) => {
const { id } = req.body;
const post = await Post.findById(req.params.id);
const _id = post.id;
const postComments = post.comments.map((comment) => comment._id);
const commentIndex = postComments.indexOf(id.id);
const message = post.comments[commentIndex].message;
try {
await Post.updateOne(
{ _id },
{
$set: {
// Action to update the comment
},
},
{ new: true }
);
res.status(200).json({ message: post });
} catch (error) {
res.status(400).json({ error: error.message });
}
}
I figured selecting the right index of the comment was a good start, but how would I, in the $set method, select the correct Comment object, and then update the message property?

You have to find the right data in database and update its required property. You can do it by the following method
exports.updateMessage= async (_id,objectId, newMessage) => {
return await TableName.updateOne({_id: _id},{{comments: {$elemMatch: {_id: objectId}$set:{message:newMessage}}});
};

Post.updateOne(_id,
{
$set: {
"comments.$[elem].message": "the value you want to set for message"
}
},
{
arrayFilters: [
{
"elem._id": 1 // _id of comment object you want to edit
}
]
})

Related

Check if the body parameter is not null and update on MongoDB

I'm trying to update a document in MongoDB using NodeJS (NextJS). My current code is:
import connect from "../../util/mongodb";
async function api(req, res) {
if (req.method === "POST") {
const { id } = req.body;
const { name } = req.body;
const { email} = req.body;
const { anything1 } = req.body;
const { anything2 } = req.body;
if (!id) {
res.status(400).json({ "error": "missing id param" });
return;
}
const { db } = await connect();
const update = await db.collection("records_collection").findOneAndUpdate(
{ id },
{
$set: {
name,
email,
anything1,
anything2
}
},
{ returnOriginal: false }
);
res.status(200).json(update);
} else {
res.status(400).json({ "error": "wrong request method" });
}
}
export default api;
Everything is working. But I would like to request only the ID as mandatory, and for the other information, leave optional.
In this code, passing the id and name for example, the other three fields (email, anything1 and anything2) will be null in the document.
It is possible to implement the update without requiring all document information and ignore when body fields are null? (As a beginner in NodeJS and MongoDB, the only way to do that that comes to my head now is to surround it all by a lot of if...)
If I've understood your question correctly you can achieve your requirement using the body object in $set stage.
If there is a field which not exists in the object, mongo will not update that field.
As an example check this example where only name field is updated and the rest of fields are not set null.
Another example with 2 fields updated and 3 fields.
You can see how only is updated the field into object in the $set.
So you only need to pass the object received into the query. Something like this:
const update = await db.collection("records_collection").findOneAndUpdate(
{ id },
{
$set: req.body
},
{ returnOriginal: false }
);

How I can post Request Body array to MongoDb using Nodejs and Mongoose

I am trying the following things:
1 Getting request body (Which is embedded with an Array).
2 Then After getting the request body I am trying to save all the objects in the array into my key of schema called "ExtraInfo".But When I run the API My "ExtraInfo" key has only object id without data.
I have Explained my whole question with my code and in my API Code, I have mentioned my logic with a comment where I am trying to push my array data to my "ExtraInfo" key.
Model:
const mongoose = require('mongoose');
const ExtraInfoModel = mongoose.Schema({
title:String,
body:String,
date: Date
})
const EmbeddedDataModel = mongoose.Schema({
class:Number,
ExtraInfo:[ExtraInfoModel],
})
module.exports = mongoose.model('EmbeddedDataCluster',EmbeddedDataModel);
RequestBody:
{
"class":"96",
"ExtraInfo":[
{
"title":"new1",
"body":"dummy1"
},
{
"title":"new2",
"body":"dummy2"
},
{
"title":"new3",
"body":"dummy3"
},
{
"title":"new4",
"body":"dummy4"
},
{
"title":"new5",
"body":"dummy5"
}
]
}
Now My Api
Router.post('/Embed', async (req, res) => {
const _ExtraInfo = await req.body.ExtraInfo;
console.log(_ExtraInfo);
try {
const _EmbeddedDataModel = await new EmbeddedDataModel({
class: req.body.class,
ExtraInfo:{$push:{$in:_ExtraInfo}}//Here is trying to loop through
//each object from array and trying to push in Key But when the Data
// saved the "ExtraInfo" key is empty and only have object id
});
_EmbeddedDataModel.save();
console.log(_EmbeddedDataModel);
res.json(_EmbeddedDataModel);
} catch (error) {
console.log(error);
res.json(error);
}
})
Result of DB:
{
"_id": "601a1f0e9fcda33570f9e26b",
"class": 96,
"ExtraInfo": [
{
"_id": "601a1f0e9fcda33570f9e26c"
}
]
}
Please Help me

mongoose filter by multiple conditions and execute to update data

I am wondering what would be the best approach to make schema functions using mongoose. I have never used this so the way I think is somewhat limited, same goes for looking for docs, without knowing what's available, is not very efficient.
Through docs I found that either using findOneAndUpdate might solve the problem; but there are some constraints.
Here is the code I am planning to run:
models/Bookmark.js
const mongoose = require('mongoose')
const bookmarkItemSchema = new mongoose.Schema({
restaurantId: String,
cachedAttr: {
name: String,
latitude: Number,
longitude: Number,
},
})
const bookmarkListSchema = new mongoose.Schema({
listName: String,
items: [bookmarkItemSchema],
})
const bookmarkSchema = new mongoose.Schema({
userId: {
type: mongoose.Schema.Types.ObjectId,
ref: 'User',
},
lists: [bookmarkListSchema],
})
// const add = (lists, userId) => {
// let bookmark = Bookmark.findOne({userId})
// bookmark.lists.listName === lists.listName //current, new
// ? bookmark.lists.items.push(lists.items)
// : bookmark.lists.push(lists)
// return bookmark
// }
mongoose.model('Bookmark', bookmarkSchema)
Routes/bookmark.js
router.post('/bookmarks', async (req, res) => {
const {lists} = req.body
console.log(lists)
if (!lists) {
return res.status(422).send({error: 'You must provide lists'})
}
let bookmark = Bookmark.findOne({"userId": req.user._id})
if (bookmark.lists.listName === lists.listName){
let item = lists.items
bookmark.lists.items.push(item)
await bookmark.save()
res.send(bookmark)
}
try {
// const bookmark = Bookmark.add(lists, req.user._id, obj)
// await bookmark.save()
// res.send(bookmark)
let bookmark = Bookmark.findOne({"userId": req.user._id})
if (bookmark.lists.listName === lists.listName){ // THIS IS UNDEFINED. How to get this object?
let item = lists.items
bookmark.lists.items.push(item)
await bookmark.save()
res.send(bookmark)
}
} catch (e) {
res.status(422).send({error: e.message})
}
})
The req.body looks like this:
{
"lists": {
"listName": "My Saved List",
"items": {
"restaurantId": "abcdefg",
"cachedAttr": {
"name": "abcdefg",
"latitude": 200,
"longitude": 200
}
}
}
}
Basically what I commented out in the models/Bookmark.js file is what I would really like to do.
If the userId's list name already exists, then I would like to just add an item to the list.
Otherwise, I would like to add a new list to the object.
What is the best approach for doing this? Is there a straight forward mongoose api that I could use for this problem? or do I need to make two separated function that would handle each case and make that as schema methods and handle it in the routes file?

Mongoose - updating object inside an array

I have a Schema that looks like this:
const RefSchema = {
active: Boolean,
items: [{}],
};
const TopLevelSchema = new mongoose.Schema({
refs: [RefSchema],
...
}, { timestamps: true });
I'm making an API call to update this one of the refs using its id (below its rid) and some data that's inside the API call:
async function updateRef(id, rid, data) {
// First get the TopLevelSchema by the ID - this is OK
const instance = await this.findById(id).exec();
// Prepare the data:
const $set = _.mapKeys(data, (v, k) => `refs.$.${k}`);
// Update the data
await instance.update(
{ 'refs.id': rid },
{ $set },
);
What's happening is that the data (and e.g. I'm passing { active: true }) is not updated.
What am I doing wrong?
There is no need to first get the TopLevelSchema etc. You can update the child like this:
async function updateRef(rid, data) {
let $set = _.mapKeys(data, (v, k) => `refs.$.${k}`)
await TopLevelSchema.updateOne(
{ 'refs._id' : mongoose.Types.ObjectId(rid) },
{ $set })
}
are you using custom ids? because you should do { '_id': rid } instead { 'refs.id': rid }

How to delete an element from an array using mongoose

I'm a little bit stuck. I'm trying to delete an element from an array using mongoose.
I used :
my_collection.update({
user: req.query.user
}, {
$pullAll: { //or $pull
my_array: array[index] //= "elem1"
}
});
Unfortunately it really doesn't work...
Here is my document, if it could help :
{
"_id":"5a997cde9872f41085391f51",
"my_array":
["elem1",
"elem2",
"elem3",
"elem4"],
"user":"rodolphe",
"__v":0
}
Thank you for your help!
See $pullAll, it requires an array argument, you passed a string.
This is the error I get when I run your code:
MongoError: $pullAll requires an array argument but was given a string
Make sure you console.log your errors with .catch()
// mock data
const req = { query: { user: "rodolphe" } }
const array = ["elem1"];
const index = 0;
// update record
Collection.update({
user: req.query.user
}, {
$pullAll: { //or $pull
my_array: [array[index]] // WRAP WITH AN ARRAY
}
})
.then(res => console.log(res))
.catch(err => console.log(err));

Resources