我正在尝试根据对象子字段上应用的过滤器更新对象数组中的字段。
我有一个投票系统,我将总 up_votes
和每日投票数存储在类似这样的对象数组中
{ date: dayDate, count: dayCount }
,
dayDate
始终是通用的,不依赖于小时/分钟/秒。当用户发送赞成票时,我会增加主 count
并更新数组中的对象,该对象的日期为今天。
var mongoose = require('mongoose');
var tokenSchema = mongoose.Schema({
...
up_votes:{
count: {
type: Number,
default: 0
},
specific:[
{
_id: false,
date: Date,
count: Number
}
]
}
....
}, { timestamps: { createdAt: 'created_at' } });
module.exports = mongoose.model('Token', tokenSchema);
我将此模型字段初始化为:
function dateToString() {
var d = new Date(),
month = '' + (d.getMonth() + 1),
day = '' + d.getDate(),
year = d.getFullYear();
if (month.length < 2)
month = '0' + month;
if (day.length < 2)
day = '0' + day;
return [day, month, year].join('/');
}
function stringToDate( date ){
var dateParts = date.split("/");
var dateObject = new Date(+dateParts[2], dateParts[1] - 1, +dateParts[0]);
return dateObject;
}
let todayDate = stringToDate(dateToString(new Date())).toISOString();
let initialVotes = {
count: 0,
specific: [{
date: todayDate,
count: 0
}]
}
req.body.up_votes = initialVotes; // this is how I intialize the field
在mongodb中该字段显示为
"up_votes" : { "count" : 0, "specific" : [ { "date" : ISODate("2021-06-22T22:00:00Z"), "count" : 0 } ] },
所以它使用 ISOdates
,现在我试图检测 specific
字段中是否存在包含当前日期作为日期的对象,以及
{date: today, count: 0}
在数组中推送一个新对象 let todayDate = stringToDate(dateToString(new Date())).getTime();
let todayDateLower = new Date(todayDate -100).getTime();
let todayDateHigher = new Date(todayDate + 100).getTime();
try {
let token = await Token.findById(req.body.tokenId);
let lastDate = new Date(token.scam_votes.specific.pop().date).getTime();
console.log( lastDate );
console.log( todayDateLower, todayDateHigher )
if( token.scam_votes.specific &&
todayDateLower < lastDate < todayDateHigher ) {
console.log('Updating existing object')
await Token.findByIdAndUpdate(req.body.tokenId,
{
$inc:{
"scam_votes.count": 1,
"scam_votes.$[specific].count": 1
},
},
{
arrayFilters: [{ "specific.date": {
$gte: new Date(todayDateLower).toISOString(),
$lt: new Date(todayDateHigher).toISOString()}
}]
}
);
} else {
console.log('Creating new object')
let newScamVotesObject = { date: todayDate, count: 1};
await Token.findByIdAndUpdate(req.body.tokenId,
{
$inc:{ "scam_votes.count": 1 },
$push: { "scam_votes.specific": newScamVotesObject }
}
);
}
} catch (error) {
console.log( Date.now(), error);
return res.send({error: 'Cannot register your vote, please contact the support'});
}
此代码导致以下错误:
todayDate == lastDate
将我带出第一个 if 语句,并将代码始终推送到数组内的新对象,为避免这种情况,我认为在比较日期时添加垃圾桶可以工作todayDateLower < lastDate < todayDateHigher
count
字段,但它不会更新包含今天日期的对象内的 count
字段,也在这里,我尝试使用 arrayFilters: [{ "specific.date": todayDate.toISOString()}]
但也没有奏效