查找一个和更新 - 文档查询 > 1000



我有一个超过100万个文档的集合,显然,我不需要搜索1000个或更多的文档,因为它应该只更新最新的文档。

const nowplayingData = {"type":"S",
"station": req.params.stationname, 
"song": data[1], 
"artist": data[0], 
"timeplay":npdate};
LNowPlaying.findOneAndUpdate( nowplayingData,
{ $addToSet: { history: [uuid] } },
{ upsert: true, sort: {_id:-1} }, 
function(err) {
if (err) {
console.log('ERROR when submitting round');
console.log(err);
}
});

我已经在它上面添加了一个排序,以便它能够首先获得最新的一个,但是如果文档不在那里并且它是第一次添加文档,那么脚本编写的方式将查询所有文档来查找。

实际上,它只需要查看最近的100个文档,如果timeplay是在最近的5分钟内,甚至更好。

你可以这样做:

const nowplayingData = {
"type":"S","station": req.params.stationname, "song": data[1], "artist": data[0], 
"timeplay":{$gte: beforeFiveMin}};

但是这几乎每次都会创建一个新文档…所以你需要维护它…

最新更新