I'm trying to:
- Find your document according to your search criteria,
- If found, update some attributes
- If you do not insert a document with some attributes.
I am using Bulk.unOrderedOperation as I am also doing one insertion. And I want to do everything in one operation again. Db.
However, something that does not cause anything is inserted for the update / restore operation.
This is the insert document:
var lineUpPointsRoundRecord = { lineupId: lineup.id, // String totalPoints: roundPoints, // Number teamId: lineup.team, // String teamName: home.team.name, // String userId: home.iduser, // String userName: home.user.name, // String round: lineup.matchDate.round, // Number date: new Date() }
This is an extended document:
var lineUpPointsGeneralRecord = { teamId: lineup.team, // String teamName: home.team.name, // String userId: home.iduser, // String userName: home.user.name, // String round: 0, signupPoints: home.signupPoints, // String lfPoints: roundPoints+home.signupPoints, // Number roundPoints: [roundPoints] // Number };
This is how I try to update / update:
var batch = collection.initializeUnorderedBulkOp(); batch.insert(lineUpPointsRoundRecord); batch.find({team: lineUpPointsRoundRecord.teamId, round: 0}). upsert(). update({ $setOnInsert: lineUpPointsGeneralRecord, $inc: {lfPoints: roundPoints}, $push: {roundPoints: roundPoints} }); batch.execute(function (err, result) { return cb(err,result); });
Why not upgrade / upgrade?
Note
This is JS code using the ORM waterline, which also uses the mongodb native driver.