问题描述
我正在使用MERN来开发我的项目,我具有这种结构的树类别:
I'm using MERN to develop my project, I have tree category with this structure:
{id: {
type: Number
},
parent_id: {
type: Number
},
name: {
type: String
},
sub: {
type: Boolean
}}
例如:
{
"_id": "5dfa22dbb04cee3960868fd8",
"id": 1,
"name": "Code",
"parent_id": 0,
"sub": true,
"__v": 0
},
{
"_id": "5dfa2358b04cee3960868fda",
"id": 101,
"parent_id": 1,
"name": "JavaScript",
"sub": true,
"__v": 0
},
{
"_id": "5dfa68735dc1004134b259ab",
"id": 1001,
"parent_id": 101,
"name": "React",
"sub": false,
"__v": 0
},
对于每个帖子,我都具有以下结构:
and for each post I have this structure:
{
"_id": "5dfd3b918937d40b98afd3f8",
"user": "5deea38cfc84f42590e01942",
"title": "test",
"description": "description test",
"category":
{
"0": "1",
"1": "101"
},
"phone": "+1",
"country": "USA",
"city": "NY",
"options": {
"transaction_type": ""
},
"date": "2019-12-20T21:22:25.940Z",
"__v": 0
}
例如,我们将ID为1的类别作为父项,将ID 101作为其子项作为1的类别101的孩子像树一样是1001
for example we have category with id 1 as parent and category with id 101 as child of 1and child of 101 is 1001 like a tree
现在我有一个类别为1001的帖子,因此我为该帖子设置了类别,如底部:
now i have a post that category is 1001 so I set category for this post like bottom:
"category": [
{
"0": "1",
"1": "101",
"2": "1001"
}
]
,其他帖子类别为101,因此我将其设置为:
and other post category is 101 so i set like this:
"category": [
{
"0": "1",
"1": "101",
}
]
当用户从菜单中选择ID为1的类别时,我希望返回所有帖子的类别分别为1、101和1001,因此我使用类似顶部代码的结构来为帖子类别设置父ID和子ID
I want when user select category with id 1 from menu, return all posts have category 1, 101 and 1001 so i use structure like top code to set parent id and child id for post category
此方法正确还是您建议一种更好的方法?
在后端中,我使用底层代码在每个类别中查找帖子,但不起作用我该怎么做?
and in backend with express I using bottom code for find posts in each category but not working what I should to do?
router.get('/category/:category', async (req, res) => {
try {
const posts = await Post.find({'category': {$all: req.params.category}}).sort({ date: -1 });
if (!posts) {
return res.status(404).json({ msg: 'Ads not found' });
}
const resultPosts = posts.slice(req.query.start, req.query.count)
res.json(resultPosts);
} catch (err) {
console.error(err.message);
if (err.kind === 'ObjectId') {
return res.status(404).json({ msg: 'Ads not found' });
}
res.status(500).send('Server Error!');
}});
推荐答案
此问题已通过以下方式解决:
The problem was solved this way: link
但是我仍然很乐意分享您有关内容分类的经验
But I'm still happy to share your experiences about content categorization
所以我最终获得的按类别分类的帖子是:
so my final get posts by category is:
router.get('/category/:category', async (req, res) => {
try {
const posts = await Post.find({category: {$elemMatch: {cat_name: 'req.params.category'}}})
if (!posts) {
return res.status(404).json({ msg: 'Ads not found' });
}
console.log(posts)
res.json(posts);
} catch (err) {
console.error(err.message);
if (err.kind === 'ObjectId') {
return res.status(404).json({ msg: 'Ads not found' });
}
res.status(500).send('Server Error!');
}
});
这篇关于创建树类别并在帖子中设置的哪种结构更好?并在MERN中按选定类别查找帖子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!