我使用什么查询来返回 <Model>.find() 中的唯一行



我正在制作一个node.js应用程序——使用MongoDB和mongoose的装饰管理器。

我有一个具有以下模式的集合:-

var DetailsSchema = new Schema({
owner: {type:Schema.Types.ObjectId, ref: 'owner', required:true},
ornament: {type:Schema.Types.ObjectId, ref:'Ornament', required:true},
location:{type:Schema.Types.ObjectId, ref:'Location', required:true},
date: {type:Date, required:true, default: Date.now()}

每次所有者从特定位置移除装饰品时,记录都会被插入到收藏中,而不是更新以使历史功能发挥作用。我想退回指定地点的所有饰品。

Details.find({location: req.params.id},'ornament')
.populate('ornament')
.exec((err,results)=>{
if(err){return next(err);}
res.render('location_detail',{results:results})
})

这将返回所有存在的饰品以及在某个时间点存在的饰品。为了消除冗余和不正确的数据,我只想返回那些具有最新日期的字段(对于具有相同所有者id和装饰id的文档(,并且存在于locationID:req.params.id 中

任何变通办法都将不胜感激。

添加了一个非活动日期字段,当事务发生时,该字段将自动更改为date.now((。问题已解决

相关内容

最新更新