在 MongoDB 外壳中
使用$type 运算符匹配undefined 值,请参阅this。
// match `undefined`
db.TEST.findOne({ updatedAt: {'$type':6} });
// match `null`
db.TEST.findOne({ updatedAt: null });
在 LB4 中
// match `undefined`
return await this.myRepository.find(
{
where: {
// !!!! "$" will be added automatically here. (explained below)
updatedAt: { 'type': 6 }
},
}
);
// match `null`
return await this.myRepository.find(
{
where: {
updatedAt: null
},
}
);
为什么{ 'type': 6 }会被转换成{ '$type': 6 }?
./node_modules/loopback-connector-mongodb/lib/mongodb.js 918 行:
您的where 将在此函数中重构
MongoDB.prototype.buildWhere = function(modelName, where, options) {
...
在第 1011 行,{ 'type': 6 } 将转换为 { '$type': 6 }
...
} else {
query[k] = {};
query[k]['$' + spec] = cond;
}
...
顺便说一下,在第 1016 行,你可以看到 null 将转换为 {$type: 10}
...
if (cond === null) {
// http://docs.mongodb.org/manual/reference/operator/query/type/
// Null: 10
query[k] = {$type: 10};
} else {
...