3

When I do this:

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

Where partId and activityId are variables, defined elsewhere,

I get

{ name: 'MongoError',
  message: 'unknown top level operator: $set',
  index: 0,
  code: 2,
  errmsg: 'unknown top level operator: $set' }

This answer says

"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?

Community
  • 1
  • 1

1 Answers1

4

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

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

EDIT:

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({});

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

NOTE:

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

EmptyArsenal
  • 7,314
  • 4
  • 33
  • 56
  • Is an "instance of a Model" a `Model`, or is it a `document`? I can call `find()` on `scores` to query for documents, so that means it's a Model, right? –  Jul 25 '15 at 01:37
  • I updated to clarify language and add more depth. I don't know the ins and outs of how inheritance works in Mongoose, but it's possible, I don't know, that if you call `scores.find()`, it goes up the prototype until it finds `find()` at `Scores.find()`, so it works. However, there are conflicting update methods here, which is your problem. – EmptyArsenal Jul 25 '15 at 01:58