I am updating my value in Mongo collection like so
if(potentialUpcomingMatchSlot1SaturdayArray.length === 1){
for(let i=0; i<potentialUpcomingMatchSlot1SaturdayArray.length; i++){
try{
const matchEntries = await MatchRegister.findOneAndUpdate({
$and: [{$or: [{$and: [{"Player1Name": potentialUpcomingMatchSlot1SaturdayArray[i].Player1Name}, {"Player2Name": potentialUpcomingMatchSlot1SaturdayArray[i].Player2Name}]},
{$and: [{"Player1Name": potentialUpcomingMatchSlot1SaturdayArray[i].Player2Name}, {"Player2Name": potentialUpcomingMatchSlot1SaturdayArray[i].Player1Name}]}]},
{TournamentName: activeTournamentRecord.TournamentName}]
}, {MatchDate: saturdayDate, MatchTime: matchTime0, MatchCompleted: 'Scheduled Tournament Match'}).exec()
} catch (err) {
const error = new HttpError(
'Cant Find Tournament. ',
500
);
return next(error);
}
}
}
This is part of a long sequence of logic and further down stream I am doing some logic based on the values updated in mongo
let matchExistsInRecord
for(let i=potentialUpcomingMatchSlot2SaturdayArray.length - 1; i>=0; i--){
try {
matchExistsInRecord = await MatchRegister.findOne({
$and: [{$or: [{Player1Name: potentialUpcomingMatchSlot2SaturdayArray[i].Player1Name},{Player2Name: potentialUpcomingMatchSlot2SaturdayArray[i].Player2Name}]},
{TournamentName: activeTournamentRecord.TournamentName},
{MatchCompleted: {$eq: undefined}}
{MatchDate: {$ne: null}}
],
}).exec()
} catch (err) {
const error = new HttpError(
'Cant Find Tournament. ',
500
);
return next(error);
}
console.log("this is the check to see if previous record in db is found")
console.log(matchExistsInRecord)
if(matchExistsInRecord){
potentialUpcomingMatchSlot2SaturdayArray.splice(i, 1)
}
}
The key here is the MatchDate field. In my first code block I am updating the record and in the second code block I am querying the collection under the assumption that the MatchDate field will be updated.
Problem : - The query in the second code block fails because clearly the Match Date field is not udpated by the time it reaches the code block.
My understanding was that using AWAIT with findOneAndUpdate will ensure that the DB gets updated and then the rest of the code logic will flow.
Can someone please help me with this?
Schema Model as Requested :-
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const matchRegisterSchema = new Schema({
MatchDate: { type: String },
MatchTime: { type: String },
Player1Name: { type: String },
Player1SeedLevel: { type: Number },
Player1GroupNumber: { type: String },
Player1TeamName: { type: String },
ChallengingPlayer: { type: String },
Player2Name: { type: String },
Player2SeedLevel: { type: Number },
Player2GroupNumber: { type: String },
Player2TeamName: { type: String },
ChallengedPlayer: { type: String },
MatchFormat : { type: String },
Winning_Player: { type: String },
Losing_Player: { type: String },
ScoreLine: { type: String },
Game1Score: { type: String },
Game1Winner: { type: String },
Game2Score: { type: String },
Game2Winner: { type: String },
Game3Score: { type: String },
Game3Winner: { type: String },
Game4Score: { type: String },
Game4Winner: { type: String },
Game5Score: { type: String },
Game5Winner: { type: String },
Round_Number: { type: String },
TournamentName: { type: String },
TournametId: { type: String},
TotalTournamentPointsForWinningPlayer: { type: Number },
TotalTournamentPointsForLosingPlayer: { type: Number },
GSMPointsForWinningPlayer: { type: Number },
GSMPointsForLosingPlayer: { type: Number },
PointsByRankForWinningPlayer: { type: Number },
PointsByFDRForWinningPlayer: { type: Number },
PointsByRankForLosingPlayer: { type: Number },
PointsByFDRForLosingPlayer: { type: Number },
TotalGSMPointsForWinningPlayer: { type: Number },
TotalGSMPointsForLosingPlayer: { type: Number },
SpecialTournamentPoints1: { type: Number },
SpecialTournamentPoints2: { type: Number },
SpecialTournamentPoints3: { type: Number },
TotalTournamentPointsForWinningPlayerWithSpecialPoints: { type: Number },
TotalTournamentPointsForLosingPlayerWithSpecialPoints: { type: Number },
MatchCompleted: { type: String },
RejectionReason: { type: String },
PlaceholderField2: { type: String },
PlaceholderField3: { type: String },
PlaceholderField4: { type: String },
ChallengerMonth: { type: String },
ChallengerYear: { type: String },
ProposedChallengeDate: { type: String },
ProposedChallengeTime: { type: String },
ChallengeStatus: { type: String },
ChallengeMatchLocation: { type: String },
ChallengeMatchFlag: { type: String },
ScoreApprovalRequestRaisedBy: { type: String}
});
module.exports = mongoose.model('Match', matchRegisterSchema);