[Mongo] 解决mongoose不支持条件操作符 $gt$gte:$lte$ne $in $all $not

时间:2022-09-16 15:13:49

reference : http://blog.sina.com.cn/s/blog_4df23d840100u25x.html

 

找到mongoose的安装目录
/usr/local/lib/node/mongoose/lib/mongoose/schema
下的文件:string.js
修改SchemaString.prototype.$conditionalHandlers = {
 

   '$lt':handleSingle,
    '$lte':handleSingle,
    '$gt':handleSingle,
    '$gte':handleSingle,
   '$all':handleArray,
    '$ne':handleSingle,
    '$in':handleArray,
    '$nin':handleArray
};
模糊查询:
在mongodb中:
db.admins.find({loginName:{$all:[/^a.*/]}});
在mongoose中
var q = new RegExp("^" + key +".*");//所有以传入参数开始的
         userM.admins.find({loginName:{'$all':[q]}},function(err,results){        
             if (err){
                  console.log(err);
              }
              else {
               res.send(results);
              }
          });     
$findCondition = array(
 'Channel' => "$ch",
'Name' => array('$ne' => ""),
'Type' => array('$ne' => ""),
'Stype' => array('$in' => array("d", "dr")),
'CTime' => array('$gt' => $minDate, '$lt' => $maxDate),
);