node.js - SailsJS 模型按条件计数

标签 node.js mongodb sails.js

我使用count方法按条件计算记录总数。这是我的代码:

    Users.count({ 
      $or: [
        { username  : { "$regex": search } },
        { email     : { "$regex": search } },
        { lastName  : { "$regex": search } },
        { firstName : { "$regex": search } }
      ]
    }).exec(function (err, total) {
      console.log(total);
      return res.json({
        "sEcho": 0, 
        "iTotalRecords": total, 
        "iTotalDisplayRecords": 7,
        "aaData": users
      });
    });

我总是收到“total”为 0 值以及“search”参数的任何值。 我的错误是什么以及如何解决?

非常感谢!

最佳答案

试试这个:

Users.count({ 
  or: [
    { username  : { contains: search } },
    { email     : { contains: search } },
    { lastName  : { contains: search } },
    { firstName : { contains: search } }
  ]
}).exec(function (err, total) {
  console.log(total);
  return res.json({
    "sEcho": 0, 
    "iTotalRecords": total, 
    "iTotalDisplayRecords": 7,
    "aaData": users
  });
});

关于node.js - SailsJS 模型按条件计数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32687169/

相关文章:

javascript - 回调后代码的最佳实践

mongodb - 有没有办法从 webstorm (mac) 运行 mongod

jquery - 风 sails :How can i read excel file data into json format

node.js - 在树莓派上的node.js上运行node_mdns时出现未知错误

node.js - Node js在ssh2中设置ssh超时

arrays - Mongodb聚合推送索引

mongodb - mgo/txn 断言集合中的唯一性

javascript - Jquery 删除所有选择选项 - 未按预期工作

session - 如何在 sails.js 中使用 sails-disk 作为 session 适配器?

javascript - 如何使用自定义查询方法延长水线?