MongoDB 查询时屏蔽 _id 产生的问题

今天更新生产环境代码,然后页面就打不开了。

数据访问出错,后台日志报错:

OperationFailure: database error: You cannot currently mix including and excluding fields. Contact us if this is an issue.

由于本地测试是没问题,所以只能把问题定位在数据库上。

首先想到的是服务器和本地的数据库版本不一样。

但是也不至于这样啊。


再仔细看日志内容,不得其所。

那就只好求助于万能的 google 了。

还真有人也碰到过这样的问题,传送门:

https://github.com/LearnBoost/mongoose/issues/1276


相关说明内容:

Theres really no need to specify _id in your schema that way since its defined by default as the same type.

Based on the message, either the field selection argument is incorrect (but not posted here) or the schema possibly excludes a field by default (again not in the above example). Otherwise, I cannot reproduce.


_id cannot currently be set to a document in mongoose. If that's what you need then yes, either using another project or submitting a pull request are your options.

那就是由于代码中查询数据时,强制设置了 { _id: 0 } 引起的了。

把代码中的这个限制删除,重新上传到服务器覆盖后,问题解决。