How can I sort and limit with Mongoose - node.js

I made an review app with Express and Mongoose. I have an review model like below:
var mongoose = require('mongoose');
var ReviewSchema = mongoose.Schema({
title: String,
description: String,
rating: Number
}, {
timestamps: true
}
);
module.exports = mongoose.model('Review', ReviewSchema);
In my controller I just get all reviews list as below. But now I want to get a list with 10 recently reviews & sort by (orderby timestamps). How can I do it with mongoose? Please help me! I am a newbie with NodeJS and Mongodb.
exports.findAll = function(req, res) {
console.log("Fetching Review...")
// Retrieve and return all reviews from the database.
Review.find(function(err, reviews){
if(err) {
console.log(err);
res.status(500).send({message: "Some error occurred while retrieving Review."});
} else {
res.send(reviews);
}
});
};
Thanks you so much

This should work for you:
Review.find()
.sort({_id: -1})
.limit(10)
.then(reviews => {
console.log(reviews)
});

you can try like this :
Review.find({}, function(err,reviews){}).sort({_id: -1}).limit(10);

Related

Subdocument does not update - mongoose

I'm trying to update a subdocument of the parent document.
I have a document called "Post" and I reference the "User" document like this:
const PostSchema = new mongoose.Schema({
title: String,
user: {
type: mongoose.Schema.Types.ObjectId,
ref: 'User',
required: true
},
},
{ collection: 'posts' })
const Post = mongoose.model('Post', PostSchema);
module.exports = Post;
I'm trying to change the name, for example of whoever posted it. The name field is in "User".
I'm trying to change it this way:
exports.update = async (req, res) => {
//find user by its id, update its post with what's in req.body
Post.findById(req.body.id, function(err, result) {
console.log(result)
if (!err) {
if (!result){
res.status(404).send('User was not found');
}
else{
result.user.nome = "User Name";
result.markModified("user");
result.save(function(saveerr, saveresult) {
if (!saveerr) {
res.status(200).send(saveresult);
} else {
res.status(400).send(saveerr.message);
}
});
}
} else {
res.status(400).send(err.message);
}
}).populate("user");
}
This is my route.js
app.put(
"/api/produtor/update",
controller.update
);
When I run it on the postman, I get status 200 and the name appears modified in the return, but it is not saved in the bank.
I would appreciate it if someone could help me analyze it!
This might be outdated but as per this GitHub comment, this might not be possible out of the box.
I would suggest to update the User by using User schema instead of going through the Post Schema.

Handling arrays with express post request

I'm using express to make the API for my web app. Below is the Schema for my annual budgets collection.
var {mongoose} = require('../db/mongoose');
var budgets = new mongoose.Schema({
year: Number,
categories: [{
name: String,
amount: Number
}]
});
var Budgets = mongoose.model('Budgets', budgets);
module.exports = {
Budgets
};
I am trying to passing in an array of categories using postman in the following way:
{
"year":2018,
"categories": [{
"name":"Logistics",
"amount":1500
}, {
"name":"Finance",
"amount":23030
}]
}
This the post request for my this collection. The request times out and is not saved in the database. I cannot seem to figure out what is wrong with the request. Please help
app.post('/annualBudgets', (req, res) => {
var categories = req.body.categories;
var budgets = new Budgets({
year : req.body.year,
});
budgets.categories.push(categories);
budgets.save().then((docs) => {
res.send(docs);
console.log(docs)
}).catch((e) => res.status(404));
});
The problem is here
budgets.categories.push(categories);
it should be
budgets.categories = categories;
// Alternatively
// budgets.categories.push(...categories);
because categories is already an array.

saving to mongodb with mongoose fails but no error shown

Am creating a nodejs bookstore app. Books details which are strings/numbers/booleans are to be saved in MongoDB using mongoose while the book cover image is to be saved in an uploads folder in my root directory using multer.Here is my mongoose schema:
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
//Creating schema and model
var BookDataSchema = new Schema({
title: String,
author: String,
isbn: String,
price: Number,
quantity: Number,
availability: Boolean,
description: String
});
var BookData = mongoose.model('BookData', BookDataSchema);
module.exports = BookData;
This is the code to perform the saving function:
router.post('/', function (req, res) {
var newBook = new BookData({
title: req.body.bkname,
author: req.body.bkauthor,
isbn: req.body.bkisbn,
price: req.body.bkprice,
quantity: req.body.bkquant,
description: req.body.bkdesc
});
newBook.save(function (err) {
if (err) {
console.log(err);
} else {
console.log(newBook);
console.log('Book Details saved successfully');
}
});
}, function(req, res) {upload(req, res, (err) => {
if (err) {
console.log(err);
res.render('admin', {
msg: err
});
} else {
console.log(req.file);
return res.render('admin');
}});}
);
The main problem is when I console.log(newBook) or console.log(result) or check in mongo shell all I see is { _id: 5b4fdba80420890764ce13bf, __v: 0 }, only the id that mongodb creates is displayed which means the other data is not saved and worse it does not proceed to the other callback function. Am not getting any error apart from this warning:
(node:1220) [DEP0079] DeprecationWarning: Custom inspection function on Objects via .inspect() is deprecated
I tested the code for saving the image excluding that for saving the other data and it worked fine. Kindly help on what could be the problem and also advise me on how I would ensure the admin page is rendered only after everything has been saved. See the whole project in_this_git_repo

Nodejs Duplicate Fields

I'm using a POST route to post data on a user's progress. I'm looking for a way to check if theres duplicate fields when posting, so I don't post multiple results
My route:
api.post('/progress', (req, res) => {
let progress = new Progress();
progress.user_id = req.body.user_id;
progress.level = req.body.level;
progress.save(function (err) {
if (err) {
res.send(err);
}
res.json({
message: 'progress saved!'
});
});
});
My Model
import mongoose from 'mongoose';
let Schema = mongoose.Schema;
let progressSchema = new Schema({
user_id: String,
level: String,
});
var levels = mongoose.model('Progress', progressSchema);
module.exports = mongoose.model('Progress', progressSchema);
Are you using MongoDB? If so, you can use mongoose module and add
unique: true,
to the field's attributes in your Progress schema. Check out the docs. Hope this helps.

Mongoose saving for populate

I'm new to Mongoose and Nodejs developement in general and I've got a bit of confusion around how to properly set up saving my records. Here are my two schemas:
Download
var mongoose = require("mongoose");
var Schema = mongoose.Schema;
var downloadSchema = Schema({
title : String,
description : String,
_project : { type: Schema.Types.ObjectId, ref: 'Project' }
});
module.exports = mongoose.model('Download', downloadSchema);
Project
...
var projectSchema = Schema({
name : String,
url : String,
pwd : String,
_downloads : [{type: Schema.Types.ObjectId, ref: 'Download' }]
});
module.exports = mongoose.model('Project', projectSchema);
This appears to be working correctly. The documentation explains my use-case of saving a download and linking a project, but I'm not sure how to properly populate the Project._downloads. Here's what I've done:
Express route handler:
function createDownload(req, res) {
// the Project Id is passed in the req.body as ._project
var dldata = req.body;
Project.findOne({ _id : dldata._project }, function(err, project) {
var dload = new Download(dldata);
dload.save( function (err, download) {
project._downloads.push(download._id);
project.save( function(err){
var msg = {};
if(err) {
msg.status = 'error';
msg.text = err;
}else {
msg.status = 'success';
msg.text = 'Download created successfully!';
}
res.json(msg);
});
});
});
}
This seems overcomplicated to me. Am I supposed to be manually pushing to the ._downloads array, or is that something Mongoose is supposed to handle internally based on the schema? Is there a better way to achieve it so that I can do:
Download.find().populate('_project').exec( ...
as well as:
Project.findOne({_id : _projectId}).populate('_downloads').exec( ...
According to the mongoose docs there are 2 ways to add subdocs to the parent object:
1) by using the push() method
2) by using the create() method
So I think that your code can be a bit simplified by eliminating the operation of saving a new Download item:
function createDownload(req, res) {
var dldata = req.body;
Project.findOne({ _id : dldata._project }, function(err, project) {
// handle error
project._downloads.push(dldata);
project.save(function(err) {
// handle the result
});
});
}
or
function createDownload(req, res) {
var dldata = req.body;
Project.findOne({ _id : dldata._project }, function(err, project) {
// handle error
project._downloads.create(dldata);
project.save(function(err) {
// handle the result
});
});
}

Resources