MongoError未知的顶级运算符:$ set [英] MongoError unknown top level operator: $set
问题描述
当我这样做时:
return scores.updateQ({_id:score._id},
{
$set:{"partId":partId,"activityId":activityId},
$unset:{topicType:'',topicId:'',courseId:''}
},
{strict:false})
partId
和activityId
是变量,在其他位置定义
Where partId
and activityId
are variables, defined elsewhere,
我知道
{ name: 'MongoError',
message: 'unknown top level operator: $set',
index: 0,
code: 2,
errmsg: 'unknown top level operator: $set' }
此
未知的顶级操作员"消息表示该操作员具有
出现在它所适用的字段之后."
"The "unknown top level operator" message means that the operator has
to appear after the field to which it applies." 但是文档说,您可以按照我的方式进行操作做到 But the documentation says you can do it the way I'm doing it 那么也许还有其他问题吗? So maybe there's something else wrong? 问题是您使用的语法错误的此方法的语法,并假定 The problem is that you're using the syntax for the wrong update method. You should be using this method's syntax, assuming that 此外,在猫鼬中,默认情况下它使用 Also, in Mongoose, it uses 我的假设是 My assumption is that Both 注意: 如果这些假设不正确,请在答案中包括更多上下文,例如您的到达方式. If these assumptions are not correct, include more context in your answer, like how you got there. 这篇关于MongoError未知的顶级运算符:$ set的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
推荐答案
scores
是文档. scores
is a document.return scores.updateQ({
$set: { "partId": partId, "activityId": activityId},
$unset: { topicType: '', topicId: '', courseId: ''}
},
{ strict: false });
$set
,因此它应该等效:$set
by default, so this should be equivalent:return scores.updateQ({
partId: partId,
activityId: activityId,
$unset: { topicType: '', topicId: '', courseId: ''}
},
{ strict: false });
scores
是一个文档(模型的一个实例):scores
is a document (an instance of the Model):var schema = new Schema({});
var Scores = mongoose.model('Scores', schema);
var scores = new Scores({});
Scores.update
和scores.update
都存在,但是语法不同,这可能是导致您出错的原因.区别在于:Scores.update
and scores.update
exist, but the syntax is different, which may be what's causing your error. Here's the difference:// Generic update
Scores.update({ _id: id }, { prop: 'value' }, callback);
// Designed to update scores specifically
scores.update({ prop: 'value' }, callback);