Mongoose return array $size - javascript

I have a document :
{
_id: 98556a665626a95a655a,
first_name: 'Panda',
last_name: 'Panda,
notifications: [{
_id: '',
// ...
}]
}
I want to return the following response :
{
_id: 98556a665626a95a655a,
first_name: 'Panda',
last_name: 'Panda',
notifications: 2
}
The problem is about notifications count field,
I used Mongoose NodeJS package and I tried the following :
UserDBModel.findOne({_id: uid}, {notifications: {$size: '$notifications'}}, function(err, user){ });
But it seems to not work.
Someone can help me ? :)
Thanks in advance.

Use aggregate with a project pipeline operator.
UserDBModel.aggregate()
.match({_id: uid})
.project({
first_name: 1,
last_name: 1,
notifications: {$size:"$notifications"}
})
.exec(function(err, notifications) {
// notifications will be an array, you probably want notifications[0]
});
Note that you will have to explicitly specify the fields for the project operator.

Maybe, you could do something like this in your node.
UserDBModel.findOne({ '_id': uid }, 'notifications', function (err, notifications) {
if (err) return handleError(err);
console.log(notifications.length);
})
Since you are using JS anyways maybe use its power! :)

What I found to work for me is creating a mongoose virtual attribute.
Schema.virtual('notifications_count').get(function() {
if (this.notifications) {
return this.notifications.length;
}
});

Related

How to delete items out of a nested array by their _id in Mongoose

I have a Model, Users, and each user has an array of objects called habits.
{
_id: 606f1d67aa1d5734c494bf0a,
name: 'Courtney',
email: 'c#gmail.com',
password: '$2b$10$WQ22pIiwD8yDvRhdQ0olBe6JnnFqV2WOsC0cD/FkV4g7LPtUOpx1C',
__v: 35,
habits: [
{
_id: 6081d32580bfac579446eb81,
name: 'first',
type: 'good',
days: 0,
checked: false
},
{
_id: 6081d32f80bfac579446eb82,
name: 'seconds',
type: 'bad',
days: 0,
checked: false
},
]
}
From my client side, I send over a list of ids of the habits I want to delete out of the array, that looks like this..
[
'6081d32580bfac579446eb81',
'6081d32f80bfac579446eb82',
]
I am trying to find a way to delete the IDs in the habits array, deleting only the habits whose IDs are sent in the array above.
Here is what I have tried....
router.post('/delete', validate, async (req, res) =>{
const user = await User.findById(req.user._id)
const idList = req.body.ids.checkedItems
const updatedList = user['habits'].filter(habit=> {
return !idList.includes(`${habit._id}`)
})
user['habits'] = updatedList;
try {
await user.save()
res.send(updatedList)
} catch (err) {
res.status(400).send(err)
}
})
-idList is the array of ids as strings.
-user['habits'] accesses the list from the document, user.
-my filter method only returns the habits that are NOT included in the idList array. Because the Ids in the array are the ones to be deleted.
This solution is obviously just vanilla javascript, what I am looking for is if anyone knows how to achieve this using mongoose.js syntax.
I think you could do this by using deleteMany or deleteOne on the Model, but I am not sure how to achieve this.
Thank you for taking the time to help or give suggestions.
The solution that worked for me in the end is to use the Model method 'findByIdAndUpdate'.
const { itemsToDelete } = req.body
User.findByIdAndUpdate(req.user._id,
{ $pull: { habits: { _id: itemsToDelete } } },
{ new: true , useFindAndModify: false},
function (err, data) {
if (err) {
res.send(err)
} else {
res.send(data.habits)
}
}
)
You can use a combination of $pull and $in for this.
User.update({_id: userId},
{
$pull: {
habits: {
_id: {
$in: [
ObjectId("6081d32580bfac579446eb81"),
ObjectId("6081d32f80bfac579446eb82")
]
}
}
}
})

Mongoose - Cannot populate with sort on path notifications.from because it is a subproperty of a document array

I have a very simple mongo scheme I'm accessing with mongoose
I can map the username and firstname to each notification's from field by using populate, the issue is I can't seem to get any sorting to work on the date field
With this code I get an error of
MongooseError: Cannot populate with sort on path notifications.from
because it is a subproperty of a document array
Is it possible to do this a different way, or newer way (deep populate, virtuals)? I'm on Mongoose 5.
I'd rather not use vanilla javascript to sort the object afterwards or create a separate schema
var UserSchema = new Schema({
username: String,
firstname: String,
notifications: [
{
from: { type: Schema.Types.ObjectId, ref: 'User'},
date: Date,
desc: String
}
]
});
app.get('/notifications', function(req, res) {
User.findOne({ _id: req._id }, 'notifications')
.populate({
path: 'notifications.from',
populate: {
path: 'from',
model: 'User',
options: { sort: { 'notifications.date': -1 } }
}
})
.exec(function(err, user) {
if (err) console.log(err)
})
});
That possible duplicate is almost 2 years old about Mongo. I'm asking if there are newer or different ways of doing this in Mongoose as it has changed a bit since 2016 with newer features.
From Mongoose V5.0.12 FAQ : http://mongoosejs.com/docs/faq.html#populate_sort_order
Q. I'm populating a nested property under an array like the below
code:
new Schema({
arr: [{
child: { ref: 'OtherModel', type: Schema.Types.ObjectId }
}] });
.populate({ path: 'arr.child', options: { sort: 'name' } }) won't sort by arr.child.name?
A. See this GitHub issue. It's a known issue but one that's
exceptionally difficult to fix.
So unfortunately, for now, it's not possible,
One way to achieve this is to simply use javascript's native sort to sort the notifications after fetching.
.exec(function(err, user) {
if (err) console.log(err)
user.notifications.sort(function(a, b){
return new Date(b.date) - new Date(a.date);
});
})
It can be achievable using nesting populate like this -
eg - schema - {donationHistory: {campaignRequestId: [ref ids]}}
await user.populate({
path: 'donationHistory.campaignRequestId',
populate: [{
path: 'campaignRequestId',
model: 'CampaignRequest',
options: { sort: { 'createdAt': -1 } },
}],
...deepUserPopulation,
}).execPopulate();

Node.js - Mongoose - Update nested array with all values in req.body

I have an object that looks like this.
{
_id: '577fe7a842c9b447',
name: 'Jacob\'s Bronze Badges',
competitors: [
{
_id: '577fe7a842c9bd6d',
name: 'Peter\'s Silver Badges',
sites: [
{
_id: '577fe7a842c9bd6d',
name: 'Facebook',
url: 'fb.com/peter'
},
{
_id: '577fe7a842c9bd6d'
name: 'Google',
url: 'google.com/peter'
}
]
},
{
_id: '599fe7a842c9bd6d',
name: 'Paul\'s Gold Badges',
sites: [
{
'_id': '577fe7a842c9bd6d',
name: 'Facebook',
url: 'fb.com/paul'
},
{
_id: '577fe7a842c9bd6d',
name: 'Google',
url: 'google.com/paul'
}
]
}
]
}
My goal is to reference the competitors array and update items inside with all of the values from req.body. I based this code off of this answer, as well as this other one.
Location.update(
{ 'competitors._id': req.params.competitorId, },
{ $set: { 'competitors.$': req.body, }, },
(err, result) => {
if (err) {
res.status(500)
.json({ error: 'Unable to update competitor.', });
} else {
res.status(200)
.json(result);
}
}
);
I send my HTTP PUT to localhost:3000/competitors/577fe7a842c9bd6d to update Peter's Silver Badges. The request body is:
{
"name": "McDonald's"
}
The problem is that when I use $set to set the competitor with _id: req.params.competitorId, I don't know what is in req.body. I want to use the entire req.body to update the object in the array, but when I do, that object is overwritten, so instead of getting a new name, Peter's Silver Badges becomes:
{
name: 'McDonald\'s',
sites: []
}
How can I update an object within an array when I know the object's _id with all of the fields from req.body without removing fields that I want to keep?
I believe that the sites array is empty because the object was reinitialized. In my schema I have sites: [sitesSchema] to initialize it. So I am assuming that the whole competitors[_id] object is getting overwritten with the new name and then the sites: [sitesSchema] from myschema.
You would need to use the $ positional operator in your $set. In order to assign those properties dynamically, based on what is in your req.body, you would need to build up your $set programmatically.
If you want to update the name you would do the following:
Location.update(
{ 'competitors._id': req.params.competitorId },
{ $set: { 'competitors.$.name': req.body.name }},
(err, result) => {
if (err) {
res.status(500)
.json({ error: 'Unable to update competitor.', });
} else {
res.status(200)
.json(result);
}
}
);
One way you might programatically build up the $set using req.body is by doing the following:
let updateObj = {$set: {}};
for(var param in req.body) {
updateObj.$set['competitors.$.'+param] = req.body[param];
}
See this answer for more details.
To update embedded document with $ operator, in most of the cases, you have to use dot notation on the $ operator.
Location.update(
{ _id: '577fe7a842c9b447', 'competitors._id': req.params.competitorId, },
{ $set: { 'competitors.$.name': req.body.name, }, },
(err, result) => {
if (err) {
res.status(500)
.json({ error: 'Unable to update competitor.', });
} else {
res.status(200)
.json(result);
}
}
);

Node.js: Asynchronous nested callback not working within each iteration in a for loop

I have used following code snippets to get followers of a particular user which are stored in mongodb
Saved user json like below
{ "_id":{
"$oid":"5440f606255cd4740e88ed21" }, "username":"test2", "email":"test2%40gmail.com", "name":"Test2", "version":null, "password":"2ea01e7a318f15378641f47469613ce6817cc91a", "gender":null, "dob":null, "profile_image":"1413543430090.jpg", "status":"1", "role_id":"2", "posts":[
], "followers":[
{
"_id":{
"$oid":"5440fb8fad74e87c0fdf22e5"
},
"user_id":"5440f0c3ae7a24ac0e47ca9e",
"unfollow":"0",
"created":{
"$date":"2014-10-17T11:20:47.107Z"
}
},
{
"_id":{
"$oid":"5440fc1e51a684e00b72db8a"
},
"user_id":"5440f9790dd5d4a40b6cec18",
"unfollow":0,
"created":{
"$date":"2014-10-17T11:23:10.645Z"
}
} ] }
to get all followers of a user i have used following code snippet. In this code followers get using foreach loop.
Main issue is that find query within foreach loop is not working because of a asynchronous callback function
db.collection('users').findOne({'_id': new mongo.ObjectID('5440f606255cd4740e88ed21'), "followers.unfollow": '0'}, {followers: 1, _id: 1}, function(err, user) {
var followers = user.followers;
var finalarray = [];
followers.forEach(function(follower, index) {
db.collection('users').find({"user._id": new mongo.ObjectID(follower._id)}, {_id: 1, username: 1, profile_picture: 1},function(err, users) {
if (user) {
var temp_follower = [];
temp_follower = {'follower_id': user._id, 'username': user.username,'profile_picture': user.profile_picture};
finalarray.push(temp_follower);
}
});
});
});
res.json({'replyCode': "success", 'replyMsg': "followers get successfully", 'followers': finalarray});
Finally in response JSON i have not find any follower user detail
Help me to get rid of this issue.
Thanks
Dinesh Prajapati
You can use the $in operator instead of looping. Here is a sample code skeleton
db.collection('users').findOne({'_id': new mongo.ObjectID('5440f606255cd4740e88ed21'), "followers.unfollow": '0'}, {followers: 1, _id: 1}, function(err, user) {
db.collection('users').find({"user._id":{$in: user.followers }, {_id: 1, username: 1, profile_picture: 1},function(err, users) {
res.json({'replyCode': "success", 'replyMsg': "followers get successfully", 'followers': users});
}
});
});
Note that to perform sql-join like operation you may have to use mapReduce kind of thing. You can refer this link.

Mongoose populate documents

I got 3 database models in mongoose that looks like this:
//profile.js
var ProfileSchema = new Schema({
username: { type: String, required: true },
password: { type: String, required: true },
matches: [{ type: Schema.Types.ObjectId, ref: 'Match' }]
});
//match.js
var MatchSchema = new Schema({
scores: [{ type: Schema.Types.ObjectId, ref: 'Score', required: true }],
});
//score.js
var ScoreSchema = new Schema({
score: {type: Number, required: true},
achivement: [{type: String, required: true}],
});
And I try to populate a profile with
Profile.findOne({ _id: mongoose.Types.ObjectId(profile_id) })
.populate('matches')
.populate('matches.scores')
.exec(function(err, profile) {
if (err) {...}
if (profile) {
console.log(profile);
}
});
The matches get populated but I dont get the scores in matches to populate. Is this not supported in mongoose or do I do something wrong? Populate gives me this:
{
user_token: "539b07397c045fc00efc8b84"
username: "username002"
sex: 0
country: "SE"
friends: []
-matches: [
-{
__v: 1
_id: "539eddf9eac17bb8185b950c"
-scores: [
"539ee1c876f274701e17c068"
"539ee1c876f274701e17c069"
"539ee1c876f274701e17c06a"
]
}
]
}
But I want to populate the score array in the match array. Can I do this?
Yes, you are right. I tried using Chaining of populate I got same output.
For your query please use async.js and then populate by the method mentioned below.
For more details please have a look at this code snippet. It is a working, tested, sample code according to your query. Please go through the commented code for better understanding in the code below and the link of the snippet provided.
//Find the profile and populate all matches related to that profile
Profile.findOne({
_id: mongoose.Types.ObjectId(profile_id)
})
.populate('matches')
.exec(function(err, profile) {
if (err) throw err;
//We got the profile and populated matches in Array Form
if (profile) {
// Now there are multiple Matches
// We want to fetch score of each Match
// for that particular profile
// For each match related to that profile
async.forEach(profile.matches, function(match) {
console.log(match, 'match')
// For each match related to that profile
// Populate score achieved by that person
Match.find({
_id:match.id
})
.populate('scores', 'score')
.exec(function (err, score) {
if (err) throw err;
// here is score of all the matches
// played by the person whose profile id
// is passed
console.log(score);
})
})
}
});
Profile.findOne({ _id: mongoose.Types.ObjectId(profile_id) })
.populate('matches.scores')
.exec(function(err, profile) {
if (err) {...}
if (profile) {
console.log(profile);
}
});

Categories

Resources