Mongoose populate with pagination - node.js

Trying to implement pagination on my populated query. I'm successful populating with mongoose populate but limit or skip doesn't work. Also tried mongoose-paginate library, pagination work but population not.
As an output I need only array of missions by user id that is paginated. Thank you.
User schema:
var mongoosePaginate = require('mongoose-paginate');
const userSchema = new mongoose.Schema({
name: {
type: String,
required: true,
},
email: {
type: String,
unique: true,
required: true,
},
password: {
type: String,
required: true,
},
uavs: [uavSchema],
missions: [{ type: mongoose.Schema.Types.ObjectId, ref: 'Mission' }],
});
userSchema.plugin(mongoosePaginate);
This works but limit nor skip doesnt work
const getMissions = async (_id) => {
let missions = [];
try {
await User.findOne({ _id: _id })
.lean()
.populate('missions')
.then((usr) => {
console.log(usr);
Object.assign(missions, usr.missions);
});
return { success: true, missions };
} catch (err) {
return { success: false, err };
}
};
output when using mongoose populate
Then tried using mongoose-paginate
const getMissions = async (_id) => {
try {
var options = {
populate: 'missions',
page: 1,
limit: 4,
lean: true,
};
await User.paginate({ _id: _id }, options, function (err, result) {
console.log(result);
});
} catch (err) {
console.log(err);
}
};
output when using mongoose-paginate

Solution - searching missions colection
const getMissions = async (_id, page, limit) => {
try {
let missions = {};
var options = {
page,
limit,
lean: true,
};
await Mission.paginate({ _owner: _id }, options, function (err, result) {
Object.assign(missions, result);
});
return { success: true, missions };
} catch (err) {
return { success: false, err };
}
};

Related

Saving an item that is specific to a user schema but not able to retrieve it back - mongoose

I have creared two schemas, user and medicine.
If a user adds medicines it should show up only in his/her account.
I am able to save the medicine ids to that specific user but i'm not able to get those medicines back i.e: medicines show for all the other users as well.
Here's the code snippet that saves meds to specific user:
const {userId, medName, medDescription, dose, medType, date, time} = req.body;
try {
const newMed = new MedsSchema({
userId,
medName,
medDescription,
dose,
medType,
date,
time,
});
await newMed.save().then(() => res.send({response: 'ok'}));
const specificUser = await User.findById({_id: userId});
specificUser.medicines.push(newMed);
await specificUser.save().then(
User.findOne(specificUser)
.populate('medicines')
.exec(function (err, docs) {
if (err) return handleError(err);
console.log(docs);
}),
);
Here's the userSchema:
const userSchema = new mongoose.Schema(
{
username: {
type: String,
required: true,
unique: true,
},
password: {
type: String,
required: true,
},
phone: {
type: Number,
required: true,
},
email: {
type: String,
unique: true,
required: true,
},
medicines: [{type: mongoose.Schema.Types.ObjectId, ref: 'MedsSchema'}],
},
{
toJSON: {
virtuals: true,
},
},
);
router.get('/getMeds/:Id', (req, res) => {
console.log(req.params.Id);
MedsSchema.find({userId: req.params.Id}, function (err, result) {
if (err) {
res.send(err);
} else {
res.send(result);
}
});
});
what do i add to this that will make me get only specific medicines for that specific user instead of getting all medicines?
Can you edit your first code snippet to
const { userId, medName, medDescription, dose, medType, date, time } = req.body;
try {
const user = await User.findOone({ _id: userId });
const newMed = await MedsSchema.create({
userId: user,
medName,
medDescription,
dose,
medType,
date,
time,
});
const specificUser = await User.findByIdAndUpdate({ _id: userId }, { $push: { medecines: newMed } });
return res.json({ newMed, specificUser })
};
and in the router
router.get('/getMeds/:Id', async (req, res) => {
console.log(req.params.Id);
const user = await User.findOne({ _id: req.params.Id }).populate({ path: 'medicines' })
console.log(user.medicines)
return res.json({meds: user.medicines})
});
also check the console results to see if everything is working

updating document inside array in mongoose

I want to update my answer object inside answers array. I am using following schema
const mongoose = require("mongoose");
const { ObjectId } = mongoose.Schema;
const questionSchema = new mongoose.Schema(
{
postedBy: {
type: ObjectId,
required: true,
ref: "User",
},
question: {
type: String,
required: true,
},
photo: {
data: String,
required: false,
},
answers: [
{
userId: { type: ObjectId, ref: "User" },
answer: String,
},
],
questionType: {
data: String,
required: false,
},
},
{ timeStamps: true }
);
module.exports = mongoose.model("Question", questionSchema);
I am using updateOne method to update my answer in my db. Can anyone explain what is missing here. I am been trying to solve this since hours
exports.updateAnswer = (req, res) => {
const questionId = req.body.questionId;
const answerId = req.body.answerId;
Question.findOne({ _id: questionId }).exec((err, question) => {
if (err) {
res.status(400).json({
error: errorHandler(err),
});
return;
}
if (!question) {
res.status(400).json({
error: "question not found",
});
return;
}
});
Question.updateOne(
{ _id: answerId },
{
$set: {
"answers.$.answer": "This is update answer. My name is Ravi Dubey",
},
},
{ new: true },
(err, success) => {
if (err) {
res.status(400).json({
error: errorHandler(err),
});
}
res.json({
msg: "answer updated successfully",
success,
});
}
);
};
My result is coming successful but answer is not updating in db.
I am confused on Question.updateOne method.
Any help appreciated.
If you trying to query based on id of one of the documents in the answers array then instead of {_id: answerId} you need to provide {'answers._id': answerId}. And also if you need the updated document as result then you should use the findOneAndUpdate method.
Question.findOneAndUpdate(
{ "answers._id": answerId },
{ $set: { "answers.$.answer": "some answer" } },
{ new: true },
(err, data) => {
// handle response
}
);

Mongoose: how to only populate, sort and return a nested object?

I have a User schema, with a messages array. The message array is filled by conversations id and referenced to a Conversation schema.
I want to fetch all conversations from a user, sort them by unread and then most recent messages. Finally, I must only return an array of lastMessage object.
For the moment, I have only managed to populate the whole user object.
Here is the Conversation Schema:
const conversationSchema = new mongoose.Schema(
{
name: { type: String, required: true, unique: true },
messages: [{ message: { type: String }, authorId: { type: String } }],
lastMessage: {
authorId: { type: String },
snippet: { type: String },
read: { type: Boolean },
},
},
{ timestamps: true }
);
conversationSchema.index({ name: 1 });
module.exports = mongoose.model("Conversation", conversationSchema);
And here is my code:
router.get("/conversations", async (req, res) => {
try {
const { userId } = req.query;
const user = await User.findById({ _id: userId }).populate("messages");
.sort({ updatedAt: 1, "lastMessage.read": 1 });
return res.json({ messages: user.messages });
} catch (err) {
console.log("error", err);
return res.json({ errorType: "unread-messages-list" });
}
});
How to do this?

How to implement a follow system, return a list of followers and following users

In my application there are 4 features I need to implement:
A user can follow another user.
A user can unfollow another user.
A user can see a list of all of their followers.
A user can see a list of all whom they are following.
I believe I have implemented 1. and 2. correctly. I created a follow schema as you can see below in my follow.model and I have created follow.controller with two methods, to store (follow) and destroy (unfollow).
Now I want to to implement 3. and 4. I created two arrays in the user.model schema, one for following and one for followers. When I return the user in my user.controller, how do I populate the following and followers array? At the moment they are empty.
follow.model.js
var mongoose = require('mongoose'),
Schema = mongoose.Schema;
var FollowSchema = new Schema({
follower: {
type: Schema.Types.ObjectId,
ref: 'User'
},
followee: {
type: Schema.Types.ObjectId,
ref: 'User'
}
},
{
timestamps: {createdAt: 'created_at', updatedAt: 'updated_at'}
});
module.exports = mongoose.model('Follow', FollowSchema);
follow.controller.js
'use strict';
const User = require('../models/user.model');
const Follow = require('../models/follow.model');
class FollowController {
constructor() {
}
store(req, res) {
let follower = req.body.follower;
let followee = req.params.id;
let follow = new Follow({
follower: follower,
followee: followee,
});
follow.save(function (err) {
if (err) {
return res.status(404).json({
succes: false,
status: 404,
data: {},
message: "There was an error trying follow the user."
});
}
return res.status(200).json({
success: true,
status: 200,
data: follow,
message: 'Successfully followed user'
});
});
}
destroy(req, res) {
let follower = req.params.followerid;
let followee = req.params.id;
Follow.remove({ 'follower': follower, 'followee': followee }, (err, result) => {
if (err) {
return res.status(404).json({
success: false,
status: 404,
data: {},
message: "Error removing record"
});
}
return res.status(201).json({
success: true,
status: 201,
data: {},
message: "Successfully unfollowed user"
})
});
}
}
module.exports = FollowController;
user.model.js
let UserSchema = new Schema({
email: {
address: {
type: String,
lowercase: true,
//unique: true,
},
token: String,
verified: {
type: Boolean,
default: false,
},
},
password: {
type: String,
},
following: [{
type: Schema.Types.ObjectId, ref: 'Follow'
}],
followers: [{
type: Schema.Types.ObjectId, ref: 'Follow'
}],
{
timestamps: {createdAt: 'created_at', updatedAt: 'updated_at'}
});
user.controller.js
show(req, res) {
let id = req.params.id;
User.findOne({ '_id': id },
function (err, user) {
if (err) {
return res.json(err);
}
return res.json(user);
});
}
You just need to populate these fields:
User.findOne({ '_id': id }, (err, user) => {
if (err) return res.json(err);
return res.json(user);
}).populate([
{ path: 'following' },
{ path: 'followers' }
]);

Mongoose findOneAndUpdate: Is there a way to update object in subdocument?

I have a model like this:
// Document
var programSchema = new Schema({
name: {
type: String
},
session: [sessionSchema]
}, {
timestamps: true
});
// Subdocument
var sessionSchema = new Schema({
name: {
type: String
},
info: {
type: String
},
order: {
type: Number
}
}, {
timestamps: true
});
Is there a way to access the subdocuments object and edit if exists, else create new?
I figured something like this:
router.post('/createsession', function (req, res) {
var options = { upsert: true, new: true, setDefaultsOnInsert: true };
var SessionData = req.body.session;
if (!SessionData.id) {
SessionData.id = mongoose.Types.ObjectId();
}
Program.findOneAndUpdate({ _id: req.body.session.id }, { $push: { session: SessionData } }, options, function (err, session) {
if (err) {
return res.status(409).json({
success: false,
message: 'Error creating/updating session'
});
} else {
return res.status(200).json({
success: true,
session: session
});
}
});
});
This only creates a new document. Would I be able to edit existing with this same query?
Try like this
var options = { upsert: true};
Program.findOneAndUpdate({ _id: req.body.session.id }, { $set: {
//Set individually values
name: req.nnae,
} },
options, function (err, session) {
if (err) {
return res.status(409).json({
success: false,
message: 'Error creating/updating session'
});
} else {
return res.status(200).json({
success: true,
session: session
});
}
});
});

Resources