私はNode/MongoDB/Mongoose ODMを持つExpressアプリケーションを持っています。アプリケーションはDBに格納されたプロジェクトに関するデータを表示し、各プロジェクトページには前と次のプロジェクトへのリンクがあります(リンクもDBに保存されています)。アプリケーションがDBの最後のプロジェクトにある場合、次のものを探すのではなく、最初のプロジェクトのデータベースをどのようにクエリできますか?MongoDB次のドキュメントを見つけるか最初に移動
app.get('/projects/:_id', function(req, res){
var nextProject = Project.find({_id: {$gt: req.params._id}}).sort({_id: 1 }).limit(1)
var prevProject = Project.find({_id: {$lt: req.params._id}}).sort({_id: -1 }).limit(1)
Project.findById(req.params._id, function(err, foundProject){
if(err){
console.log('Error finding project');
console.log(err);
} else {
nextProject.exec(function(err, next){
if(err){
console.log('err');
console.log(err);
} else {
prevProject.exec(function(err, prev){
if(err){
console.log('err');
console.log(err);
} else {
console.log(prev[0].title);
console.log('==============================================');
console.log(foundProject);
console.log('==============================================');
console.log(next[0].title);
res.render('project', {project: foundProject, nextProject: next[0], prevProject: prev[0]});
}
});
}
});
}
});
});
ありがとうございます!
app.get('/projects/:_id', function(req, res){
var nextProject = Project.find({_id: {$gt: req.params._id}}).sort({_id: 1 }).limit(1)
var prevProject = Project.find({_id: {$lt: req.params._id}}).sort({_id: -1 }).limit(1)
// find the project for the clicked link
Project.findById(req.params._id, function(err, foundProject){
if(err){ // handle any errors
console.log('Error finding project');
console.log(err);
} else {
nextProject.exec(function(err, next){ // if no errors, execute the query object for the next project
if(err){ // handle any errors
console.log('Error getting next project');
console.log(err);
} else if(next){ // if next has a value...
prevProject.exec(function(err, prev){ // ... execute the query object for the previous project
if(err){ // handle any errors
console.log('error getting previous project');
console.log(err);
} else if(prev){ // if previous has a value...
console.log('prev and next have a value');
console.log('============================foundProject============================');
console.log(foundProject);
console.log('============================prevProject============================');
console.log(prev);
console.log('============================nextProject============================');
console.log(next);
res.render('project', {project: foundProject, nextProject: next[0], prevProject: prev[0]});//... render the project page with current project, next project and previous project
} else if(prev){ // if previous doesn't have a value...
var prevProject = Project.find().sort({_id: -1}).limit(1);//... get a query object for the last document in the database
prevProject.exec(function(err, prev){ // execute the query object for the last document
if(err){ // handle any errors
console.log('error getting last document');
console.log(err);
} else { // with no errors...
console.log('next has a value, but previous does not. Gone to last project');
res.render('project', {project: foundProject, nextProject: next[0], prevProject: prev[0]}); // ... render the page
}
})
}
})
} else if(next){ //if next doesn't have a value...
var nextProject = Project.find().sort({_id: 1 }).limit(1);//get a query object for the first document
nextProject.exec(function(err, next){ // execute the query object
if(err){
console.log('error getting first project');
console.log(err);
} else { // with no errors...
console.log('previous has a value, but next does not. Gone to first project');
res.render('project', {project: foundProject, nextProject: next[0], prevProject: prev[0]});//render the page
}
});
}
});
}
:動作していない多くのコードで '次へ' の空に戻った...
app.get('/projects/:_id', function(req, res){
Project.find({_id: {$gt: req.params._id}}).sort({_id: 1 }).limit(1), function(err, next){
if(err){
console.log('err getting next project');
console.log(err);
} else if(next){
var nextProject = next
console.log('this is the next project');
console.log(nextProject);
} else {
var nextProject = Project.find().sort({_id: 1 }).limit(1);
console.log('this is the first project');
console.log(nextProject);
}
}
Project.find({_id: {$lt: req.params._id}}).sort({_id: -1 }).limit(1), function(err, prev){
if(err){
console.log('error getting previous project');
console.log(err);
} else if(prev){
var prevProject = prev
console.log('this is the previous project');
console.log('prevProject');
} else {
var prevProject = Project.find().sort({_id: -1}).limit(1);
console.log('this is the last project');
console.log(prevProject);
}
}
Project.findById(req.params._id, function(err, foundProject){
if(err){
console.log('Error finding project');
console.log(err);
} else {
nextProject.exec(function(err, next){
if(err){
console.log('err');
console.log(err);
} else {
prevProject.exec(function(err, prev){
if(err){
console.log('err');
console.log(err);
} else {
console.log(prev[0].title);
console.log('==============================================');
console.log(foundProject);
console.log('==============================================');
console.log(next[0].title);
res.render('project', {project: foundProject, nextProject: next[0], prevProject: prev[0]});
}
});
}
});
}
});
第二編集場合は、最初のドキュメントを取得しようとしている
EDIT(新コード
}); });
こんにちは、ありがとう、アレックス!あなたのスニペットは最初のエントリを取得するために働いていますが、もしあれば次のエントリを取得する方法を見つけられませんし、そうでなければ最初のエントリを取得します...私はMongoDBをかなり新しく、構文がわからない。私は次のようなものを試しています: 'var nextProject = Project.find({$ or:{{式1>}、{<式2>}}})' しかし、おそらく私は間違った木...? –
私は最初のクエリを実行し、結果が空の場合は2番目のクエリを実行します。 – alexbt
は集計の例で更新されました。しかし、私はあなたが既に最初の文書のIDを知っている場合にのみ動作させることができます。そうであれば、最初の文書を簡単にメモリに保存することもできます。 – alexbt