last day (15 days later) » 

3:32 PM
0
A: How to sort Arrays of Object using aggregation in mongoDB

eolIf your goal is to group the tasks elements into separate groups by their dueDate and then sort those groups, here's what you can do: db.collection.aggregate([ { "$unwind": "$todos" }, { $group: { _id: "$todos.dueDate", todos: { $push: "$$ROOT" } } },...

 
I tried this but nothing changed
 
eol
See edit .. note that you should always include expected results to your question to make it easier to help you ..
 
I dont know why but it is still now working
 
eol
Well, it works as expected on the mongoplayground, right? Or is this not what you want?
 
Ya, it was working on the mongoplayground but it is not working in my project :(
 
eol
3:32 PM
Note that you need to use "todos.dueDate": 1 if you want the record to be sorted ascending. Is that the problem?
 
No, I think the problem is something else
because my files are in different manner
 
eol
Well what exactly is happening? No sorting at all?
You just say "it is not working" but it's hard to know what you mean
 
no not shorting at all
*sorting
 
eol
Copy a sample document here please
 
const mongoose = require('mongoose');
const User = mongoose.model('User');

exports.todo = async (req, res) => {
res.render('layout');
// await User.aggregate([
// { $unwind: '$todos' },
// { $sort: { 'todos.dueDate': -1 } },
// ]);
};

exports.addTodo = async (req, res) => {
const add = {
todos: {
task: req.body.task,
dueDate: req.body.dueDate,
dueTime: req.body.dueTime,
},
};
const user = await User.findOneAndUpdate(
{ _id: req.user._id },
{ $push: add },
{ new: true }
).exec();

await User.aggregate([
this is my todoContorller
const mongoose = require('mongoose');
mongoose.Promise = global.Promise;
const slug = require('slugs');
const md5 = require('md5');
const validator = require('validator');
const mongodbErrorHandler = require('mongoose-mongodb-errors');
const passportLocalMongoose = require('passport-local-mongoose');
// const { date } = require('faker');

const userSchema = new mongoose.Schema({
username: {
type: String,
unique: true,
lowercase: true,
trim: true,
required: 'Please Enter a username',
},
email: {
and this is my Use model
const express = require('express');
const router = express.Router();
const todoControllers = require('../controllers/todoController');
const userController = require('../controllers/userController');
const authController = require('../controllers/authController');
const { catchErrors } = require('../handlers/errorHandlers');

router.get('/', authController.isLoggedIn, todoControllers.todo);
router.post(
'/',
authController.isLoggedIn,
catchErrors(todoControllers.addTodo)
);

router.get('/login', userController.loginForm);
and this is my routes
 
eol
3:36 PM
Well of course it's not doing anything, you are not using the result of await User.aggergate :D
 
ohh, how to do that?
i didn't know about that
 
eol
You need to do something like:

const result = await User.aggregate(...) and send the response
res.json(result);
for example
 
can't i just replace the current todos array?
 
eol
No .aggregate performs aggregations and returns a new result set
 
so i have to pass that return result to the render?
 
eol
3:40 PM
yes
 
exports.todo = async (req, res) => {
const user = await User.aggregate([
{ $unwind: '$todos' },
{ $sort: { 'todos.dueDate': -1 } },
]);

res.render('layout', user);
};
like this?
 
eol
Looks ok, user will now be an array of the sorted documents that is passed to the template. You need to make sure you use it correctly in the frontend ..
 
I tried this first
exports.todo = async (req, res) => {
const newUser = await User.aggregate([
{ $unwind: '$todos' },
{ $sort: { 'todos.dueDate': -1 } },
]);

res.json(newUser);

};
it worked
but when i tried this
exports.todo = async (req, res) => {
const newUser = await User.aggregate([
{ $unwind: '$todos' },
{ $sort: { 'todos.dueDate': -1 } },
]);

// res.json(user);
res.render('layout', newUser);
};
it didnt worked
in layout.pug
i used
pre= newUser
which should have worked
 
eol
no offense but this now a totally different question - your question regarding the sorting was solved. so please accept my answer and post a new one regarding the pug problem, I'm not expert with pug unfortunately
 
oh ya my bad, Thanks a lot man
 
eol
3:49 PM
np
In case you don't know you can accept my answer by clicking the checkmark on the left next to my answer. And I'd appreciate an upvote as well ;)
And you may want to read stackoverflow.com/help/someone-answers ...
 
4:33 PM
do you mind answering other question?
https://stackoverflow.com/questions/66408872/how-to-set-slug-in-mongodb
https://stackoverflow.com/questions/66398298/getting-error-while-updating-user-information-in-mongodb
 

last day (15 days later) »