【问题标题】:Where greater than integer - Waterline/Sails/Mongo大于整数的地方 - Waterline/Sails/Mongo
【发布时间】:2017-11-06 22:18:39
【问题描述】:

我正在尝试使用 Mongo 在 Sails 中执行多个 where 语句,但我没有这样的运气

    var start = moment().subtract(30, 'days').unix();
    var end = moment().unix();

    console.log(start, end)

    Measurement.find()
    .where({
        user: jwt.decoded.id,
        and: [
          { timestamp: { ">=": start }},
          { timestamp: { "<=": end }}
        ],
    })
    .sort('timestamp DESC')
    .then((results) => {
        return res.view('dashboard', {title: 'MMOL', data: results, tab: 'dashboard', code: 90})
    })

例如,我的开始时间是 1507410695,结束时间是 1510006295。

在我的收藏中,我有大约 40 条记录在这些日期之间,它们的结构如下:

{
    "_id": {
        "$oid": "5a0083aa25b9c9bb37b5fc5d"
    },
    "timestamp": 1509916500,
    "blood": 9,
    "carb": 20,
    "calorie": 230,
    "insulin": 2,
    "notes": null,
    "user": {
        "$oid": "59fcced3c40317c657878b5c"
    },
    "createdAt": {
        "$date": "2017-11-06T15:45:46.572Z"
    },
    "updatedAt": {
        "$date": "2017-11-06T22:05:35.270Z"
    }
}

我根本无法得到任何结果。据我所知,我这样做是正确的。如果我删除 and 语句,我会毫无问题地得到所有结果。有什么想法吗?

【问题讨论】:

  • 您确定这些日期内的记录也与相同的用户 ID 匹配吗?
  • 当然。如果我删除 and 语句,那么所有记录都会显示。如果我在 mongo 中运行一个只是 { timestamp: 1509916500} 的查询,它就会显示出来。
  • 我也刚刚通过sails在Node中运行了一个查询,将时间戳:1509916500放在where部分并返回结果
  • 试试下面的方法怎么样:user: jwt.decoded.id, timestamp: { '&gt;=' : start, '&lt;=' : end }
  • 很好,我刚刚更新了我的答案

标签: mongodb sails.js waterline


【解决方案1】:

不用and,你可以直接用

user: jwt.decoded.id, 
timestamp: { 
   '>=' : start, 
   '<=' : end 
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-06-21
    • 1970-01-01
    • 1970-01-01
    • 2014-05-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多