踩坑记录:
match筛选条件中必不可少根据_id来筛选,_id是ObjectId类型,如果代码中直接使用字符串类型给match,然后就神奇的发现返回结果永远是空的:


这里谷歌到的一句话:

Turns out the casting of the ObjectId seemed to be the issue. It was being cast using the Schema type Object Id mongoose.Schema.Types.ObjectId when it needed to be just a pure ObjectId mongoose.Types.ObjectId.

意思大致是ObjectId 需要使用mongoose.Types.ObjectId.不能使用mongoose.Schema.Types.ObjectId

如上代码中的ObjectId 属于 mongoose.Schema.Types.ObjectId, 想当然的使用直接用 ObjectId(_id) 还是空的结果

因此正确写法:
mongoose.Types.ObjectId(_id);
注释:这里的mongoose 是
var mongoose = require('mongoose');
所有评论
加载评论 ...
发表评论