I'm trying to:
我试图:
- Find a document according to a search criteria,
- If found, update some attributes
- If not insert a document with some attributes.
根据搜索条件查找文档,
如果找到,请更新一些属性
如果没有插入带有某些属性的文档。
I'm using a Bulk.unOrderedOperation
as I'm also performing a single insert. And I want to do everything in one operation againast DB.
我正在使用Bulk.unOrderedOperation,因为我还在执行单个插入。而且我想在一次操作中再做一切DB。
However something it's causing nothing is being inserted for the update/upsert operation.
然而,正在为更新/ upsert操作插入一些不会导致任何问题。
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 the upsert document:
这是upsert文件:
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'm trying to upsert/update:
这就是我尝试upsert / 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 wouldn't it be upserting/updating?
为什么不进行上传/更新?
Note
That is JS code using waterline ORM which also uses mongodb native driver.
这是使用水线ORM的JS代码,它也使用mongodb本机驱动程序。
2 个解决方案
#1
7
Your syntax here is basically correct, but your general execution was wrong and you should have "seperated" the "upsert" action from the other modifications. These will otherwise "*" and produce an error when an "upsert" occurs:
这里的语法基本上是正确的,但是你的一般执行是错误的,你应该从其他修改中“分离”“upsert”动作。否则,当发生“upsert”时,这些将“冲突”并产生错误:
LineupPointsRecord.native(function (err,collection) {
var bulk = collection.initializeOrderedBulkOp();
// Match and update only. Do not attempt upsert
bulk.find({
"teamId": lineUpPointsGeneralRecord.teamId,
"round": 0
}).updateOne({
"$inc": { "lfPoints": roundPoints },
"$push": { "roundPoints": roundPoints }
});
// Attempt upsert with $setOnInsert only
bulk.find({
"teamId": lineUpPointsGeneralRecord.teamId,
"round": 0
}).upsert().updateOne({
"$setOnInsert": lineUpPointsGeneralRecord
});
bulk.execute(function (err,updateResult) {
sails.log.debug(err,updateResult);
});
});
Make sure your sails-mongo is a latest version supporting the Bulk operations properly be the inclusion of a recent node native driver. The most recent supports the v2 driver, which is fine for this.
确保您的sails-mongo是支持批量操作的最新版本,包括最近的节点本机驱动程序。最近支持v2驱动程序,这对此很好。
#2
1
Typically I have always set upsert as a property on update. Also update should be able to find the record itself so no need to find it individually.
通常我总是将upsert设置为更新时的属性。此外,更新应该能够找到记录本身,因此无需单独查找。
Depending on the environment the $ may or may not be necessary.
根据环境,$可能需要也可能不需要。
batch.update(
{team: lineUpPointsRoundRecord.teamId, round: 0},
{
$setOnInsert: lineUpPointsGeneralRecord,
$inc: {lfPoints: roundPoints},
$push: {roundPoints: roundPoints},
$upsert: true
});
#1
7
Your syntax here is basically correct, but your general execution was wrong and you should have "seperated" the "upsert" action from the other modifications. These will otherwise "*" and produce an error when an "upsert" occurs:
这里的语法基本上是正确的,但是你的一般执行是错误的,你应该从其他修改中“分离”“upsert”动作。否则,当发生“upsert”时,这些将“冲突”并产生错误:
LineupPointsRecord.native(function (err,collection) {
var bulk = collection.initializeOrderedBulkOp();
// Match and update only. Do not attempt upsert
bulk.find({
"teamId": lineUpPointsGeneralRecord.teamId,
"round": 0
}).updateOne({
"$inc": { "lfPoints": roundPoints },
"$push": { "roundPoints": roundPoints }
});
// Attempt upsert with $setOnInsert only
bulk.find({
"teamId": lineUpPointsGeneralRecord.teamId,
"round": 0
}).upsert().updateOne({
"$setOnInsert": lineUpPointsGeneralRecord
});
bulk.execute(function (err,updateResult) {
sails.log.debug(err,updateResult);
});
});
Make sure your sails-mongo is a latest version supporting the Bulk operations properly be the inclusion of a recent node native driver. The most recent supports the v2 driver, which is fine for this.
确保您的sails-mongo是支持批量操作的最新版本,包括最近的节点本机驱动程序。最近支持v2驱动程序,这对此很好。
#2
1
Typically I have always set upsert as a property on update. Also update should be able to find the record itself so no need to find it individually.
通常我总是将upsert设置为更新时的属性。此外,更新应该能够找到记录本身,因此无需单独查找。
Depending on the environment the $ may or may not be necessary.
根据环境,$可能需要也可能不需要。
batch.update(
{team: lineUpPointsRoundRecord.teamId, round: 0},
{
$setOnInsert: lineUpPointsGeneralRecord,
$inc: {lfPoints: roundPoints},
$push: {roundPoints: roundPoints},
$upsert: true
});