【发布时间】:2016-09-22 08:10:42
【问题描述】:
我正在尝试根据条件以及 upsert 进行推送或拉取
myCollection.update(
{'id': location},
{
$set: { count },
$setOnInsert: {
id: location,
users: []
},
},
{
$cond: {
if: (increment==1),
then: {$push: { users: userToken }},
else: {$pull: { users: userToken }}
}
},
{'upsert':true},
(err, data) => {
...
我正在尝试把它弄干(这很有效):
mongo.connect(dbUrl, (err, db) => {
if (err) throw err
let myCollection = db.collection('myCollection')
if(increment==1){
myCollection.update(
{'id': location},
{
$set: { count },
$push: { users: userToken },
$setOnInsert: {
id: location
}
},
{'upsert':true},
(err, data) => {
if (err) throw err
console.log(data);
callback()
db.close()
}
)
}
else{
myCollection.update(
...
$pull: { users: userToken },
...
)
}
})
当我有 $cond 时,它不会向数据库添加任何内容。 $cond 应该在哪里?
【问题讨论】:
标签: javascript node.js mongodb conditional