MongoError未知的顶级运算符:$ set [英] MongoError unknown top level operator: $set

查看:68
本文介绍了MongoError未知的顶级运算符:$ set的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我这样做时:

return scores.updateQ({_id:score._id},
{
    $set:{"partId":partId,"activityId":activityId},
    $unset:{topicType:'',topicId:'',courseId:''}
},
{strict:false})

partIdactivityId是变量,在其他位置定义

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?

推荐答案

问题是您使用的语法错误的此方法的语法,并假定scores是文档.

The problem is that you're using the syntax for the wrong update method. You should be using this method's syntax, assuming that scores is a document.

return scores.updateQ({
    $set: { "partId": partId, "activityId": activityId},
    $unset: { topicType: '', topicId: '', courseId: ''}
},
{ strict: false });

此外,在猫鼬中,默认情况下它使用$set,因此它应该等效:

Also, in Mongoose, it uses $set by default, so this should be equivalent:

return scores.updateQ({
    partId: partId,
    activityId: activityId,
    $unset: { topicType: '', topicId: '', courseId: ''}
},
{ strict: false });

我的假设是scores是一个文档(模型的一个实例):

My assumption is that scores is a document (an instance of the Model):

var schema = new Schema({});
var Scores = mongoose.model('Scores', schema);
var scores = new Scores({});

Scores.updatescores.update都存在,但是语法不同,这可能是导致您出错的原因.区别在于:

Both 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);

注意:

如果这些假设不正确,请在答案中包括更多上下文,例如您的到达方式.

If these assumptions are not correct, include more context in your answer, like how you got there.

这篇关于MongoError未知的顶级运算符:$ set的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆