我在伪代码中执行以下查询:
如果我浏览了1,000个用户,然后有人刷了所有相同的1,000个用户,则会添加一个新用户,使其成为1,001。由于所有其他1,000个用户最近都是最近的,因为每个新用户最近都是他们创建帐户的日期,所以它必须经过1000才能达到1,001,导致3-4分钟的查询呼叫,这是不行的
我当前的堆栈包含用于我的网站的Javascript,Backend将NodeJs与Mongodb一起使用。
这三个查询如下所示:
var minAge = (req.body.min);
var maxAge = (req.body.max);
var swipersCount = 0;
verifyLoginFilter(req, function(loggedIn, userObj, uid) {
if (loggedIn && !userObj.accountFrozen) {
//doing -1 on min and +1 on max for inclusive
var minAgeMinusOne = minAge - 1;
if (minAge === 0) {
minAgeMinusOne = 0;
}
var maxAgePlusOne = maxAge + 1;
var theSwipers, swipersInterests;
var x = 0;
var y = 0;
var skipCount = 0;
grabSwiperLastSwiped();
function grabSwiperLastSwiped() {
User.find({
"$and": [
{ accountFrozen: false },
{ creditsAvailable: { $gt: minAgeMinusOne } },
{ userId: { $ne: uid } }, {
"$or": [
{ "minAge": { "$lt": maxAgePlusOne, "$gt": minAgeMinusOne } },
{ "maxAge": { "$lt": maxAgePlusOne, "$gt": minAgeMinusOne } }
]
}
]
})
.limit(10).sort({ dateLastSwiped: 1 }).skip(skipCount).select({ userId: 1, activeInterests: 1, creditsAvailable: 1, dateLastSwiped: 1 })
.exec(function(err, swipers) {
if (err) {
return res.json({ statusCode: alertErrors.mongodb });
} else {
if (swipers.length > 0) {
theSwipers = swipers;
grabSwiperInterests();
} else {
return res.json({ statusCode: alertErrors.noSwipers });
}
}
});
}
function grabSwiperInterests() {
var today = new Date();
var finalMax;
if (theSwipers[x].creditsAvailable > maxAgePlusOne) {
finalMax = maxAgePlusOne;
} else {
finalMax = theSwipers[x].creditsAvailable;
}
Interests.
find({
"$and": [
{ userId: theSwipers[x].userId },
{ paused: false },
{ _id: { $nin: userObj.personSwiped } }
]
}).limit(10)
.sort({ dailyCredits: 1 })
.exec(function(err, interests) {
if (err) {
return res.json({ statusCode: alertErrors.mongodb });
} else {
if (interests.length > 0) {
swipersInterests = interests;
checkInterst();
} else {
nextSwiper();
}
}
});
}
function nextSwiper() {
swiperCount++;
console.log("nextSwiper " + theSwipers[x].userId + " number " + swiperCount);
if (x === theSwipers.length - 1) {
x = 0;
skipCount = skipCount + theSwipers.length;
grabSwiperLastSwiped();
} else {
console.log("nextSwiper " + theSwipers.length + " x " + x);
x++;
grabSwiperInterests();
}
}
function nextInterest() {
console.log("nextInterest");
if (y === swipersInterests.length - 1) {
nextSwiper();
} else {
y++;
checkInterst();
}
}