I'm having trouble dealing with populate within object, so I have a SosmedPost schema. I want to populate the Comments field, which is an array. Likewise, I have 2 function create comment and getPost, the populate working in create comment but in getPost it's not working
const SosmedPostSchema = new mongoose.Schema({
Username : {
type : String
},
Content : {
type : String
},
Documents : {
type : String
},
Author : {
type : mongoose.Schema.Types.ObjectId,
ref : 'sosmedprofiles'
},
Comments : [{
PostedBy : {
type : mongoose.Schema.Types.ObjectId,
ref : "sosmedprofiles"
},
Text : {
type : String
}
}],
Likes : [{
type : mongoose.Schema.Types.ObjectId,
ref : "sosmedprofiles"
}]
}, {timestamps : true});
I have a function to create comment, and it's working perfectly fine
const createComment = (req, res) => {
const Id = req.params.id;
const comment = {
Text : req.body.Text,
PostedBy : req.body.PostedBy
}
SosmedPostModel.findByIdAndUpdate({_id : Id}, {
$push : {Comments : comment}
}, {
new : true
}).populate("Comments.PostedBy")
.exec((err, result) => {
if (err) {
return res.status(422).json({error : err})
} else {
res.json(result)
}
})
}
But in another function getPostById the Comments.PostedBy populate its not working. Is there something wrong with how I populate the array ?
const getPostById = (req, res) => {
const Id = req.params.id;
SosmedPostModel.findById({_id : Id})
.populate("Author")
.populate("Comments.PostedBy")
.populate("Likes")
.exec(function (err, posts) {
if (err) return handleError(err);
res.send(posts)
})
}
This is the result in create comment function :
"Comments": [
{
"Text": "Cool Post",
"PostedBy": {
"_id": "63a116dc3c759e4437fabacb",
"Username": "rahmandhika5#gmail.com",
"FullName": "Rahmandhika",
"ProfilePicture": "https://example.jpg",
"Bio": "Live in Tangerang",
"Post": [],
"createdAt": "2022-12-20T01:58:52.179Z",
"updatedAt": "2022-12-20T01:58:52.179Z",
"__v": 0
},
"_id": "63a1172f3c759e4437fabaf9"
},
But in getPostById function, the result is :
"Comments": [
{
"Text": "Cool Post",
"PostedBy": "63a116dc3c759e4437fabacb",
"_id": "63a1172f3c759e4437fabaf9"
},
{
"Text": "Yo",
"PostedBy": "63a116dc3c759e4437fabacb",
"_id": "63a11bab4aad531ee8ad8273"
},
{
"PostedBy": "63a116dc3c759e4437fabacb",
"Text": "YAAY",
"_id": "63a11fcdf5384ab89174ad7e"
}
],
Related
I have a board object and I want to edit it from express and mongoose, this is my object:
"boardMembers": [
"5f636a5c0d6fa84be48cc19d",
],
"boardLists": [
{
"cards": [
{
"_id": "5f7c9b77eb751310a41319ab",
"text": "card one"
},
{
"_id": "5f7c9bb524dd8d42d469bba3",
"text": "card two"
}
],
"_id": "5f7c9b6b02c19f21a493cb7d",
"title": "list one",
"__v": 0
}
],
"_id": "5f63877177beba2e3c15d159",
"boardName": "board1",
"boardPassword": "123456",
"boardCreator": "5f636a5c0d6fa84be48cc19d",
"g_createdAt": "2020-09-17T15:57:37.616Z",
"__v": 46
}
Now this is my code, I tried to do it with $pull, but nothing happend when I check it on Postman
router.put("/delete-task/:list/:task", auth, boardAuth, async (req, res) => {
const listId = req.params.list;
const task = req.params.task;
const board = await Board.findOne({ _id: req.board._id });
if (!board) return res.status(404).send("no such board");
Board.findOneAndUpdate(
{ _id: req.board._id },
{ $pull: { "boardLists.$[outer].cards": { _id: task } } },
{
arrayFilters: [{ "outer._id": listId }],
}
);
await board.save();
res.send(board);
});
what I am missing here?
Hope this will work in your case, you just need to convert your ids into mongo objectId. So your code will look something like this:
import mongoose from "mongoose";
const task = mongoose.Types.ObjectId(req.params.task);
const listId = mongoose.Types.ObjectId(req.params.list);
board = await Board.findOneAndUpdate(
{ _id: req.board._id},
{ $pull: {
"boardLists.$[outer].cards": { _id: task }
}
},
{
arrayFilters: [{ "outer._id": listId }],
returnNewDocument: true
}
);
res.send(board);
This is how my "Users" databse looks like:
{
"_id" : ObjectId("5f1408d3c0e8c130503daafd"),
"username" : "Akhil Jagga",
"data" : [
{
"_id" : ObjectId("5f15cde9329fc9300c7f3843"),
"nameOfList" : "home",
"items" : [
{
"_id" : ObjectId("5f15cde9329fc9300c7f3844"),
"nameOfItem" : "this is the list item"
}
]
},
{
"_id" : ObjectId("5f15cebd9a97051d80c6c6ad"),
"nameOfList" : "personal",
"items" : [
{
"_id" : ObjectId("5f15cebd9a97051d80c6c6ae"),
"nameOfItem" : "this is the list item"
},
{
"_id" : ObjectId("5f15cfd0d73dc330dc8e7fd1"),
"nameOfItem" : "lol"
}
]
}
],
"__v" : 48
}
I want to delete a specific object from "items" array say with ObjectId("5f15cebd9a97051d80c6c6ae"), using mongoose in nodejs .
I tried by writting the following code but it does't work:
app.post("/delete", redirectLogin, function(req, res) {
const checkedItemId = req.body.checkbox;
const checkedItemName = req.body.hiddenInput;
const listName = req.body.listName;
console.log("item id: " + checkedItemId);
User.findOneAndUpdate(
{
username: req.session.userId
},
{
$pull: {
data: {
items: {
_id: checkedItemId
}
}
}
},
function(err, foundList) {
console.log(foundList);
res.redirect("/");
});
});
I have tried using findOneAndUpdate, if I write name of item with the
id it deletes the whole list from data array containing that item
name and id.
Thankyou for your time.
Try this one,
const username = req.session.userId;
const listName = req.body.listName;
const checkedItemId = req.body.checkbox;
User.update({ "username": username}, { "$pull": { `data.${listName}.items`: { "_id": checkedItemId } }}, { safe: true, multi:true }, function(err, obj) {
//do rest
});
CartController.prototype.addToCart =(req, res, next) => {
// Using mongoose in-built function "findbyID"
Product.findById({_id : req.params.productId}).then( item =>
{
if (!item) {res.status(400).send({message : "item not found"})}
Cart.findByIdAndUpdate(req.params.userId,
{
total_quantity : 0,
total_price : 0,
final_price : 0,
"$push": {"products": {
// Passing Data
productid : item._id,
MRP : item.offers.MRP,
finalprice : item.offers.finalprice,
discount : item.offers.discount,
organization : item.property.organization,
brand : item.property.brand,
color : item.property.color,
weight : item.property.weight,
category : item.property.category
}
},
userid : req.params.userId
},
{ upsert: true, returnNewDocument : true}
).then(() => {
res.status(200).send({message: "product added to cart"});
}).catch(err => {
res.status(500).send(err);
});
}).catch (err => {
res.status(500).send("item fetch related issue found", err);
});
};
//json output
[
{
"_id": "5e5a58843ed45a235c32ac8c",
"__v": 0,
"createdAt": "2020-03-16T18:04:31.370Z",
"updatedAt": "2020-03-16T18:41:23.068Z",
"userid": "5e5a58843ed45a235c32ac8c",
"inCart": false,
"products": [
{
"category": "Home Decor",
"weight": 300,
"color": "Multicolor",
"MRP": 5000,
"productid": "5e6f4234564e2d1b74ba3383",
"_id": "5e6fbfaf3818775ac010187e",
"quantity": 1,
"brand": "",
"organization": "",
"discount": 20
},
{
"category": "Home Decor",
"weight": 300,
"color": "Multicolor",
"MRP": 5000,
"productid": "5e6b21458f7019378c4981ff",
"_id": "5e6fbff53818775ac0101881",
"quantity": 1,
"brand": "",
"organization": "",
"discount": 20
}
],
"final_price": 0,
"total_price": 0,
"total_quantity": 0
}
]
//cart model
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var product = new Schema({
productid : {
type: Schema.Types.ObjectId,
// required: true,
ref: 'Product'
},
MRP : {
type : Number,
// required : true
},
finalPrice : {
type : Number
},
discount : {
type : Number, default : 0
},
organization : {
type : String, default : null
},
brand : {
type : String, default : null
},
color : {
type : String
},
weight : {
type : Number
},
category : {
type : String,
// required :true
},
quantity : {
type : Number,
// required : true,
default : 1
}
});
var cartSchema = new Schema({
total_quantity : {type : Number, required : true, default: 0},
total_price : {type : Number, required : true, default: 0},
final_price : {type : Number, required : true, default: 0},
products : [product],
userid : {
type: Schema.Types.ObjectId,
required: true,
ref: 'Pujari'
},
inCart : {type : Boolean, default : 0}
},
{
timestamps:true
}
);
module.exports = mongoose.model('Cart', cartSchema);
//delete cart item
CartController.prototype.deleteCartItem = (req, res, next) => {
Cart.update({userid : req.params.userId}, { $pull: { products : {productid: req.params.productId }}}, {multi: true})
};
So, basically I have to collection one for product and another for cart, my question is how to delete particular product from cart like in bellow json I have to product. I just want to delete object where "productid": "5e6b21458f7019378c4981ff", I tried mongodb $pull operator but its not working for this data.
finally I found the solution, there was a small mistake I was doing
//delete cart item
CartController.prototype.deleteCartItem = (req, res, next) => {
Cart.update({userid : req.params.userId}, { $pull: { products : {category: "Home" }}}, function (err, data) {
if (err) res.send(err)
else res.send(data)
})
For me your update is working (for the given document):
db.collection.updateMany(
{ userid: "5e5a58843ed45a235c32ac8c" },
{ $pull: { "products": { productid: "5e6b21458f7019378c4981ff" } } }
)
{
"acknowledged" : true,
"matchedCount" : 1.0,
"modifiedCount" : 1.0
}
my Test Schema:
var TestSchema = new Schema({
testName: String,
topic: {
topicTitle: String,
topicQuestion: [
{
questionTitle: String,
choice: [
{
name: String
age: Number
}
]
}
]
}
}, { collection: 'test' });
var Test = mongoose.model('test', TestSchema);
I want to update one age ($inc)value which I have the choice id.
I can have test id, topicQuestion id and choice id.
How to write this query in mongoose in NodeJS?
Normally I use the below query to update a value:
Test.findOneAndUpdate({ _id: testId }, { $inc: { ... } }, function (err, response) {
...
});
but it is so difficult to get in array and one more array. Thanks
You can use the $[] positional operator to update nested arrays.
router.put("/tests/:testId/:topicQuestionId/:choiceId", async (req, res) => {
const { testId, topicQuestionId, choiceId } = req.params;
const result = await Test.findByIdAndUpdate(
testId,
{
$inc: {
"topic.topicQuestion.$[i].choice.$[j].age": 1
}
},
{
arrayFilters: [{ "i._id": topicQuestionId }, { "j._id": choiceId }],
new: true
}
);
res.send(result);
});
Let's say we have this existing document:
{
"_id" : ObjectId("5e53e7d9bf65ac4f5cbf2116"),
"testName" : "Test 1",
"topic" : {
"topicTitle" : "Title",
"topicQuestion" : [
{
"_id" : ObjectId("5e53e7d9bf65ac4f5cbf211a"),
"questionTitle" : "Question 1 Title",
"choice" : [
{
"_id" : ObjectId("5e53e7d9bf65ac4f5cbf211c"),
"name" : "A",
"age" : 1
},
{
"_id" : ObjectId("5e53e7d9bf65ac4f5cbf211b"),
"name" : "B",
"age" : 2
}
]
},
{
"_id" : ObjectId("5e53e7d9bf65ac4f5cbf2117"),
"questionTitle" : "Question 2 Title",
"choice" : [
{
"_id" : ObjectId("5e53e7d9bf65ac4f5cbf2119"),
"name" : "C",
"age" : 3
},
{
"_id" : ObjectId("5e53e7d9bf65ac4f5cbf2118"),
"name" : "D",
"age" : 4
}
]
}
]
},
"__v" : 0
}
If we want to increment age value of a given choice, we send a PUT request using endpoint like this http://.../tests/5e53e7d9bf65ac4f5cbf2116/5e53e7d9bf65ac4f5cbf211a/5e53e7d9bf65ac4f5cbf211b where
"testId": "5e53e7d9bf65ac4f5cbf2116"
"topicQuestionId": "5e53e7d9bf65ac4f5cbf211a"
"choiceId": "5e53e7d9bf65ac4f5cbf211b"
You need to inform what choice you want and, on the update section, you need change the way you do increment.
Example:
Test.findOneAndUpdate({ _id: testId, topicQuestion.choice._id: choiceId}, { 'topicQuestion.$.choice': {$inc: { age: <numberToIncrement> }}}, {new: true}, function (err, response) {
...
});
Schema Definitions
Team.js
var TeamSchema = new Schema({
// Team Name.
name: String,
lead: String,
students :type: [{
block : Number,
status : String,
student : {
type: Schema.ObjectId,
ref: 'Student'
}]
});
Student.js
var StudentSchema = new Schema({
name: String,
rollNo : Number,
class : Number
});
How I can populate "student" to get output, as below:
team
{
"__v": 1,
"_id": "5252875356f64d6d28000001",
"students": [
{
"__v": 1,
"_id": "5252875a56f64d6d28000002",
block : 1,
status : joined,
"student": {
"name": Sumeeth
"rollNo" : 2
"class" : 5
}
},
{
"__v": 1,
"_id": "5252875a56f64d6d28000003",
block : 1,
status : joined,
"student": {
"name": Sabari
"rollNo" : 3
"class" : 4
}
}
],
"lead": "Ratha",
}
This is JS I use to get the document using Mongoose:
Team.findOne({
_id: req.team._id
})
.populate('students')
.select('students')
.exec(function(err, team) {
console.log(team);
var options = {
path: 'students.student',
model: 'Student'
};
Student.populate(team.students,options,function(err, students) {
console.log(students);
if (err) {
console.log(students);
res.send(500, {
message: 'Unable to query the team!'
});
} else {
res.send(200, students);
}
});
});
In my console output I get the following:
{ _id: 53aa434858f760900b3f2246,
students
[ { block : 1
status: 'joined'
_id: 53aa436b58f760900b3f2249 },
{ block : 1
status: 'joined'
_id: 53aa436b58f760900b3f2250 }]
}
And the expected output is:
{ _id: 53aa434858f760900b3f2246,
students
[ { block : 1
status: 'joined'
student :{
"name": Sumeeth
"rollNo" : 2
"class" : 5
}
},
{ block : 1
status: 'joined'
student :{
"name": Sabari
"rollNo" : 3
"class" : 4
}
}
]
}
Some one please help me where I am wrong. How should I make use of .populate, so that , I can get the entire student object and not only its id.
Reference :
Populate nested array in mongoose
I have been facing same issue. I have use this code for my rescue :
Team.findOne({_id: req.team._id})
.populate({ path: "students.student"})
.exec(function(err, team) {
console.log(team);
});
Here is a simplified version of what you want.
Basic data to set up, first the "students":
{
"_id" : ObjectId("53aa90c83ad07196636e175f"),
"name" : "Bill",
"rollNo" : 1,
"class" : 12
},
{
"_id" : ObjectId("53aa90e93ad07196636e1761"),
"name" : "Ted",
"rollNo" : 2,
"class" : 12
}
And then the "teams" collection:
{
"_id" : ObjectId("53aa91b63ad07196636e1762"),
"name" : "team1",
"lead" : "me",
"students" : [
{
"block" : 1,
"status" : "Y",
"student" : ObjectId("53aa90c83ad07196636e175f")
},
{
"block" : 2,
"status" : "N",
"student" : ObjectId("53aa90e93ad07196636e1761")
}
]
}
This is how you do it:
var async = require('async'),
mongoose = require('mongoose');
Schema = mongoose.Schema;
mongoose.connect('mongodb://localhost/team');
var teamSchema = new Schema({
name: String,
lead: String,
students: [{
block: Number,
status: String,
student: {
type: Schema.ObjectId, ref: 'Student'
}
}]
});
var studentSchema = new Schema({
name: String,
rollNo: Number,
class: Number
});
var Team = mongoose.model( "Team", teamSchema );
var Student = mongoose.model( "Student", studentSchema );
Team.findById("53aa91b63ad07196636e1762")
.select('students')
.exec(function(err, team) {
console.log( team );
async.forEach(team.students, function(student,callback) {
Student.populate(
student,
{ "path": "student" },
function(err,output) {
if (err) throw err;
callback();
}
);
},function(err) {
console.log( JSON.stringify( team, undefined, 4 ) );
});
});
And it gives you the results:
{
"_id": "53aa91b63ad07196636e1762",
"students": [
{
"block": 1,
"status": "Y",
"student": {
"_id": "53aa90c83ad07196636e175f",
"name": "Bill",
"rollNo": 1,
"class": 12
}
},
{
"block": 2,
"status": "N",
"student": {
"_id": "53aa90e93ad07196636e1761",
"name": "Ted",
"rollNo": 2,
"class": 12
}
}
]
}
You really do not need the "async" module, but I am just "in the habit" as it were. It doesn't "block" so therefore I consider it better.
So as you can see, you initial .populate() call does not do anything as it expects to "key" off of an _id value in the foreign collection from an array input which this "strictly speaking" is not so as the "key" is on "student" containing the "foreign key".
I really did cover this in a recent answer here, maybe not exactly specific to your situation. It seems that your search did not turn up the correct "same answer" ( though not exactly ) for you to draw reference from.
You are overthinking it. Let Mongoose do the work for you.
Team.findOne({
_id: req.team._id
})
.populate({path:'students'})
.exec(function(err, team) {
console.log(team);
});
This will return students as documents rather than just the ids.
TL DR
const team = await Team.findById(req.team._id)
.populate("students");
team.students = await Student.populate(team.students, {path: "student"});
Context
Reading from all the answers I went testing everything and just Neil Lun's answer worked for me. The problem is it was on the path to a cb hell. So I cracked my head a little and 'refactored' to an elegant one-liner.
const foundPost = await Post.findById(req.params.id)
.populate("comments")
.populate("author");
foundPost.comments = await User.populate(foundPost.comments, {path: "author"});
My initial problem:
{
title: "Hello World",
description: "lorem",
author: {/* populated */},
comments: [ // populated
{text: "hi", author: {/* not populated */ }}
]
};
How my models basically are:
User = {
author,
password
};
Post = {
title,
description,
author: {}, //ref User
comments: [] // ref Comment
};
Comment = {
text,
author: {} // ref User
};
The output after problem solved:
{
comments: [
{
_id: "5dfe3dada7f3570b60dd977f",
text: "hi",
author: {_id: "5df2f84d4d9fcb228cd1df42", username: "jo", password: "123"}
}
],
_id: "5da3cfff50cf094c68aa2a37",
title: "Hello World",
description: "lorem",
author: {
_id: "5df2f84d4d9fcb228cd1aef6",
username: "la",
password: "abc"
}
};