如何对 mongoose/mongodb 和 nodejs 中的对象数组进行排序?
How to sort array of object in mongoose/mongodb and nodejs?
我想找到一个品牌,然后将与该品牌关联的产品按照价格从高到低排序。我试过了,但它没有改变任何东西。是否可以使用猫鼬或我必须添加的其他代码?
控制器
exports.getBrandLowtoHighPrice = (req, res, next) => {
const page = +req.query.page || 1; //convert to number
let totalItems;
let sessionUser = req.user;
if (typeof sessionUser == "undefined") {
sessionUser = "guest";
}
Brand.findById(req.params.id)
.sort({'product.createdAt' : -1})
.skip((page - 1) * ITEM_PER_PAGE)
.limit(ITEM_PER_PAGE)
.populate({
path: "product",
populate: {
path: "category",
model: "Category",
},
populate: {
path: "subCategory",
model: "SubCategory",
},
populate: {
path: "brand",
model: "Brand",
},
populate: {
path: "rating",
model: "Rating",
},
})
.then((products) => {
console.log(products)
res.render("user/single-brand", {
categories: req.categories,
subCategories: req.subCategories,
bestSeller: req.bestSeller,
products: products.product,
brand: req.brand,
user: sessionUser,
banner: req.banner,
brandId: req.params.id,
currentPage: page,
hasNextPage: ITEM_PER_PAGE * page < totalItems,
hasPreviousPage: page > 1,
nextPage: page + 1,
previousPage: page - 1,
lastPage: Math.ceil(totalItems / ITEM_PER_PAGE),
});
})
.catch((err) => {
console.log(err);
})
}
这里我试过排序但没有用。我的数据结构是这样的
数据
{
product:[
{
category: [Array],
subCategory: [Array],
brand: [Array],
rating: [],
sellCount: '1',
color: [Array],
size: [Array],
image: [Array],
_id: 60f444ace8568b2fec481dcf,
title: 'nvhjm m',
description: 'hello',
price: 200,
discount: 2,
quantity: 9,
feature: false,
active: true,
stock: true,
createdAt: 2021-07-18T15:11:40.047Z,
updatedAt: 2021-07-20T01:23:06.113Z,
__v: 0
}
],
_id: 60e14e4a94e27f3a2c93a26f,
brand: 'Acer',
image: 'images\2021-07-04T05-59-38.028Z-download.png',
createdAt: 2021-07-04T05:59:38.042Z,
updatedAt: 2021-07-18T15:11:40.056Z,
__v: 0
}
我想根据产品价格对我的价值进行排序。
谢谢!
查看 $expr,它使您能够将两个字段值一起比较。
或检查数组 mql 运算符,它可能会有帮助。
这可能会对您有所帮助:
const Brand = require('../../model/admin/brand')
exports.getBrandLowtoHighPrice = (req, res, next) => {
const page = +req.query.page || 1; //convert to number
let totalItems;
let sessionUser = req.user;
if (typeof sessionUser == "undefined") {
sessionUser = "guest";
}
Brand.findById(req.params.id)
.sort({'product.createdAt' : -1})
.skip((page - 1) * ITEM_PER_PAGE)
.limit(ITEM_PER_PAGE)
.populate({
path: "product",
populate: {
path: "category",
model: "Category",
},
populate: {
path: "subCategory",
model: "SubCategory",
},
populate: {
path: "brand",
model: "Brand",
},
populate: {
path: "rating",
model: "Rating",
},
options: { sort: { 'price': 1 } }
})
/* .populate({path: 'housingposts', options: { sort: { 'date': -1 } } }) */
.then((products) => {
console.log(products)
res.render("user/single-brand", {
categories: req.categories,
subCategories: req.subCategories,
bestSeller: req.bestSeller,
products: products.product,
brand: req.brand,
user: sessionUser,
banner: req.banner,
brandId: req.params.id,
currentPage: page,
hasNextPage: ITEM_PER_PAGE * page < totalItems,
hasPreviousPage: page > 1,
nextPage: page + 1,
previousPage: page - 1,
lastPage: Math.ceil(totalItems / ITEM_PER_PAGE),
});
})
.catch((err) => {
console.log(err);
})
}
我想找到一个品牌,然后将与该品牌关联的产品按照价格从高到低排序。我试过了,但它没有改变任何东西。是否可以使用猫鼬或我必须添加的其他代码?
控制器
exports.getBrandLowtoHighPrice = (req, res, next) => {
const page = +req.query.page || 1; //convert to number
let totalItems;
let sessionUser = req.user;
if (typeof sessionUser == "undefined") {
sessionUser = "guest";
}
Brand.findById(req.params.id)
.sort({'product.createdAt' : -1})
.skip((page - 1) * ITEM_PER_PAGE)
.limit(ITEM_PER_PAGE)
.populate({
path: "product",
populate: {
path: "category",
model: "Category",
},
populate: {
path: "subCategory",
model: "SubCategory",
},
populate: {
path: "brand",
model: "Brand",
},
populate: {
path: "rating",
model: "Rating",
},
})
.then((products) => {
console.log(products)
res.render("user/single-brand", {
categories: req.categories,
subCategories: req.subCategories,
bestSeller: req.bestSeller,
products: products.product,
brand: req.brand,
user: sessionUser,
banner: req.banner,
brandId: req.params.id,
currentPage: page,
hasNextPage: ITEM_PER_PAGE * page < totalItems,
hasPreviousPage: page > 1,
nextPage: page + 1,
previousPage: page - 1,
lastPage: Math.ceil(totalItems / ITEM_PER_PAGE),
});
})
.catch((err) => {
console.log(err);
})
}
这里我试过排序但没有用。我的数据结构是这样的 数据
{
product:[
{
category: [Array],
subCategory: [Array],
brand: [Array],
rating: [],
sellCount: '1',
color: [Array],
size: [Array],
image: [Array],
_id: 60f444ace8568b2fec481dcf,
title: 'nvhjm m',
description: 'hello',
price: 200,
discount: 2,
quantity: 9,
feature: false,
active: true,
stock: true,
createdAt: 2021-07-18T15:11:40.047Z,
updatedAt: 2021-07-20T01:23:06.113Z,
__v: 0
}
],
_id: 60e14e4a94e27f3a2c93a26f,
brand: 'Acer',
image: 'images\2021-07-04T05-59-38.028Z-download.png',
createdAt: 2021-07-04T05:59:38.042Z,
updatedAt: 2021-07-18T15:11:40.056Z,
__v: 0
}
我想根据产品价格对我的价值进行排序。 谢谢!
查看 $expr,它使您能够将两个字段值一起比较。 或检查数组 mql 运算符,它可能会有帮助。
这可能会对您有所帮助:
const Brand = require('../../model/admin/brand')
exports.getBrandLowtoHighPrice = (req, res, next) => {
const page = +req.query.page || 1; //convert to number
let totalItems;
let sessionUser = req.user;
if (typeof sessionUser == "undefined") {
sessionUser = "guest";
}
Brand.findById(req.params.id)
.sort({'product.createdAt' : -1})
.skip((page - 1) * ITEM_PER_PAGE)
.limit(ITEM_PER_PAGE)
.populate({
path: "product",
populate: {
path: "category",
model: "Category",
},
populate: {
path: "subCategory",
model: "SubCategory",
},
populate: {
path: "brand",
model: "Brand",
},
populate: {
path: "rating",
model: "Rating",
},
options: { sort: { 'price': 1 } }
})
/* .populate({path: 'housingposts', options: { sort: { 'date': -1 } } }) */
.then((products) => {
console.log(products)
res.render("user/single-brand", {
categories: req.categories,
subCategories: req.subCategories,
bestSeller: req.bestSeller,
products: products.product,
brand: req.brand,
user: sessionUser,
banner: req.banner,
brandId: req.params.id,
currentPage: page,
hasNextPage: ITEM_PER_PAGE * page < totalItems,
hasPreviousPage: page > 1,
nextPage: page + 1,
previousPage: page - 1,
lastPage: Math.ceil(totalItems / ITEM_PER_PAGE),
});
})
.catch((err) => {
console.log(err);
})
}