nodejs mongodbfind修改并返回新的。 无法规范化查询

我的问题与此有关

mongodb中的findAndModify错误 – nodejs – 错误代码17287

但解决scheme没有奏效(我试着指定顺序,但我得到了同样的错误)我认为这可能是沿索引的行我使用的是example而不是_id_id是这个集合中的字段我只是在这种情况下,不想通过_id进行search)不确定…

错误:

 { [MongoError: exception: nextSafe(): { $err: "Can't canonicalize query: BadValue bad sort specification", code: 17287 }] name: 'MongoError', message: 'exception: nextSafe(): { $err: "Can\'t canonicalize query: BadValue bad sort specification", code: 17287 }', errmsg: 'exception: nextSafe(): { $err: "Can\'t canonicalize query: BadValue bad sort specification", code: 17287 }', code: 13106, ok: 0 } 

这是我的代码,它应该find并修改(并使用new:true返回)文档的第一个实例的字段example等于减1。

 db.collection('documents').findAndModify({example:-1},{$set:{example:0}},{new:true},function(err,result){ console.dir(err||result); }); 

但它所做的只是错误,就像它恨我的脸!

这是一个文件:

 {'_id':0,'example':-1} 

我的ID字段是自定义数字在哪里我确保_id总是唯一的(对于我的目的,我不能改变_id为标准的mongodb方式)。

当节点文档突然结束,然后你就猜测代码结构应该是在什么样的结构上,而不是在标准结构上的时候,这是很痛苦的。 由于开发者没有logging的方法,我浪费了一天的时间。

这是工作代码

 db.collection('documents').findAndModify({'example':{$eq:-1}},[['example',1]],{$set:{'example':-1}},{'new':true},function(err,result){ console.dir(err||result); }); 

我不确定我真的很喜欢写这样的代码。 我可能会寻找另一个节点模块,因为上面的查询看起来很恶心!

如果我能find像以下这样简单的东西,那将是很大的挑战:

 db.collection('documents').findModifyReturn('find:example==-1;order:asc;set:example=0;',function(e,r){}); 

尝试使用$eq运算符。

 db.collection('documents').findAndModify({example: {$eq: -1}}, {'example', 'ascending'},{$set:{example:0}},{new:true},function(err,result){ console.dir(err||result); });