当我这样做时:
return scores.updateQ({_id:score._id},
{
$set:{"partId":partId,"activityId":activityId},
$unset:{topicType:'',topicId:'',courseId:''}
},
{strict:false})
其中partId
和activityId
是变量,定义在别处,
我明白了
{ name: 'MongoError',
message: 'unknown top level operator: $set',
index: 0,
code: 2,
errmsg: 'unknown top level operator: $set' }
这answer说
"The "unknown top level operator" message means that the operator has to appear after the field to which it applies."
但是the documentation说你可以按照我的方式去做
所以也许还有其他问题?
最佳答案
问题是您使用的语法错误 update method .你应该使用 this method's语法,假设 scores
是一个文档。
return scores.updateQ({
$set: { "partId": partId, "activityId": activityId},
$unset: { topicType: '', topicId: '', courseId: ''}
},
{ strict: false });
此外,在 Mongoose 中,它默认使用 $set
,因此这应该是等价的:
return scores.updateQ({
partId: partId,
activityId: activityId,
$unset: { topicType: '', topicId: '', courseId: ''}
},
{ strict: false });
编辑:
我的假设是 scores
是一个文档(模型的一个实例):
var schema = new Schema({});
var Scores = mongoose.model('Scores', schema);
var scores = new Scores({});
Scores.update
和 scores.update
都存在,但语法不同,这可能是导致错误的原因。这是区别:
// Generic update
Scores.update({ _id: id }, { prop: 'value' }, callback);
// Designed to update scores specifically
scores.update({ prop: 'value' }, callback);
注意:
如果这些假设不正确,请在您的回答中包含更多背景信息,例如您是如何到达那里的。
关于mongodb - MongoError 未知顶级运算符 : $set,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31620222/