1

I have a NodeJS script that was working on a server for 6 months, this week I've setup a new server and have installed all updated versions of softwares, including mongojs, nodejs and mongodb, and somehow my code doesn't work anymore.

The part of code:

 collect.findOne({$or: [{from_id: enviou, to_id: viu}, {from_id: viu, to_id: enviou}]}, {sort: {$natural: -1}}, function (err, echoData) {
    if (err || !echoData) {
        console.log("No messages found -rv",err);
    } else {
       console.log(echoData);
    }
});

Gives me the error:

{ [MongoError: Can't canonicalize query: BadValue Projection cannot have a mix of inclusion and exclusion.]
  name: 'MongoError',
  message: 'Can\'t canonicalize query: BadValue Projection cannot have a mix of inclusion and exclusion.',
  '$err': 'Can\'t canonicalize query: BadValue Projection cannot have a mix of inclusion and exclusion.',
  code: 17287 }


And this part:

collect.find({$or: [{from_id: de, to_id: para}, {from_id: para, to_id: de}]}, {skip: skipCount, limit: useDbLimit, sort: {time: 1}}, function (err, echoData) {
    if (err || !echoData) {
        console.log("No messages found -sm",err);
    } else {
        echoData.forEach(function (returnData) {
            console.log(returnData);
        });
    }
});

Gives me the error:

{ [MongoError: Can't canonicalize query: BadValue Unsupported projection option: sort: { $natural: -1 }]
  name: 'MongoError',
  message: 'Can\'t canonicalize query: BadValue Unsupported projection option: sort: { $natural: -1 }',
  '$err': 'Can\'t canonicalize query: BadValue Unsupported projection option: sort: { $natural: -1 }',
  code: 17287 }



I don't have any ideia of how to fix it. It works on my old server, but not on the new with latest mongodb and mongojs. Anyone can help me ?

1 Answers1

3

I think it's related to a change in MongoJS that used to pass projection options to underlying cursos and that's not working anymore, but I can't pinpoint the exact version change.

Now, your two blocks are not valid in terms of the MongoDB API, and because of that, they're not valid on MongoJS API either, at least on the current version of each one.

In the first block, you should change your query to the following:

collect.findOne({$query: {$or: [{from_id: enviou, to_id: viu}, {from_id: viu, to_id: enviou}]}, $orderby: {$natural: -1}}, function (err, echoData) {
    ...
});

The second block is trickier because you can only pass those instructions to the cursor directly by chaining:

collect.find({$or: [{from_id: de, to_id: para}, {from_id: para, to_id: de}]}).skip(skipCount).limit(useDbLimit).sort({time: 1}, function (err, echoData) {
    ...
});

Those are MongoJS/MongoDB 3.0 API-compliant calls.

fixmycode
  • 8,220
  • 2
  • 28
  • 43