【问题标题】:Find method not working in GMongo using $gt greater and less than operands使用 $gt 大于和小于操作数查找在 GMongo 中不起作用的方法
【发布时间】:2012-01-19 19:42:58
【问题描述】:

我正在使用 Groovy 中的 GMongo 库从 MongoDB 中读取项目。 CacheItem 类是一个保存缓存项的简单对象,每个项都有一个过期时间,该过期时间是在添加到 mongo 时设置的。当我从mongo读取项目时,我只想根据expirationMillis字段检索没有过期的项目。

class CacheItem {
  def _id
  def cacheKey
  long expirationMillis
  def value
}

这适用于使用 cacheKey 检索项目

item = mongoDb.cache.findOne( cacheKey: "600")

但是当我尝试使用大于/小于约定的条件来对抗 expireMillis 时,我似乎无法检索任何文档..

long nowMillis = (new Date()).getTime()
item = mongoDb.cache.findOne( cacheKey: "600", expirationMillis: { $gt: nowMillis})

我是否使用了错误的约定?

【问题讨论】:

    标签: grails mongodb groovy gorm-mongodb


    【解决方案1】:

    您必须转义 $gt 使其看起来像这样:

    long nowMillis = (new Date()).getTime()
    item = mongoDb.cache.findOne( [ cacheKey: "600", expirationMillis: [ "\\\$gt" : nowMillis]]  as BasicDBObject)
    

    long nowMillis = (new Date()).getTime()
    item = mongoDb.cache.findOne( [ cacheKey: "600", expirationMillis: [ '$gt' : nowMillis]]  as BasicDBObject )
    

    【讨论】:

    • 我试过了,但 Eclipse 抱怨语法;不喜欢用分号分隔 '\\\$gt' 和 'nowMillis'
    • 对不起...错字。您应该使用方括号而不是大括号
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多