gpt4 book ai didi

javascript - 如何在 mongodb v3.6 中排序

转载 作者:可可西里 更新时间:2023-11-01 10:01:56 25 4
gpt4 key购买 nike

我在将最后添加的项目放置在对象列表中时遇到问题/项目出现在顶部。基本上它就像一个工作板,我希望最新的工作出现在顶部而不是底部。我试过使用 Post.find({}).sort({ date: -1}).exec(function (err, posts) {... });但它没有似乎在工作,或者我可能没有正确实现它,我对此还是很陌生。

// Example code, but similar to actual code

//models config
let jobSchema = new mongoose.Schema({
title: String,
category: String,
description: String,
type: String,
url: String,
email: String,
apply: String,
location: String,
company: String,
path: String,
created: {type: Date, default: Date.now}
})

let Job = mongoose.model('job', jobSchema);
// {type: String, default: "placeholdeimage.jpg"}

//routes config

var storage = multer.diskStorage({
destination: function (req, file, cb) {
cb(null, './public/uploads/');
},

filename: function (req, file, cb) {
var originalname = file.originalname;
var extension = originalname.split(".");
filename = Date.now() + '.' + extension[extension.length-1];
cb(null, filename);
}
});


app.get('/', (req, res) =>{
res.redirect('/jobs')
})

app.get('/jobs', (req, res) =>{
Job.find({}).sort({ date: -1}).find(function(err, jobs){
if(err){
console.log('error')
}

return res.render('index', {jobs: jobs})

})

})

//add form route
app.get('/jobs/add', (req, res) => {
res.render('add')
})

app.post('/jobs', multer({storage: storage, dest:
'./public/uploads/'}).single('file'), (req, res) => {

req.body.description = req.sanitize(req.body.description);

Job.create( (err, addJob) =>{

if(req.file){
let fullPath = "uploads/"+req.file.filename;
let document = {
title: req.body.title,
category: req.body.category,
description: req.body.description,
type: req.body.type,
url: req.body.url,
email: req.body.email,
apply: req.body.apply,
location: req.body.location,
company: req.body.company,
path: fullPath
};

let job = new Job(document);
job.save()
}else{
console.log('file not uploaded')
logo = 'noimage.jpg'
}

//redirect to index page
return res.redirect('/jobs')
})
})

app.get('/jobs/:id', (req, res) => {
Job.findById(req.params.id, (err, jobDetails) => {
if(err){
res.redirect('/jobs')
}else{
res.render('details', {job: jobDetails});
}
})
})


app.listen(port, process.env.PORT, process.env.IP, ()=> console.log(`Server
is running on ${port}`))
<!--writen in EJS-->
<div class="container">

<% for (const job of jobs) { %>
<div class="row">
<div class="column logo" style="width:10%;">
<img src='<%= job.path %>'>
</div>
<div class="column title">
<h2>
<%=job.title%>
</h2>
<p>
<%=job.company%>
</p>
</div>
<div class="column type">
<h2>
<%=job.type%>
</h2>
</div>
<div class="column location">
<h2>
<%=job.location%>
</h2>
</div>
<div class="column">
<h2><a href="/jobs/<%= job._id %>"><button>Apply</button></a></h2>
</div>
</div>
<hr>
<% } %>

</div>

最佳答案

MongoDB 中的排序是这样完成的:

db.Post.find({ MATCHING CRITERIA }).sort({ date: 1 })

其中 1 为升序,-1 为降序。

关于javascript - 如何在 mongodb v3.6 中排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52108913/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com