因此,我有一个嵌入式文档,跟踪组成员身份.每个嵌入文档都有一个指向另一个集合中的组的ID、一个开始日期和一个optional过期日期.

我想查询组中的当前成员."当前"表示开始时间小于当前时间,过期时间大于当前时间或为空.

This conditional query is totally blocking me up. I could do it by running two queries and merging the results, but that seems ugly and requires loading in all results at once. Or I could default the expire time to some arbitrary date in the far future, but that seems even uglier and potentially brittle. In SQL I'd just express it with "(expires >= Now()) OR (expires IS NULL)" -- but I don't know how to do that in Mongo.

有什么 idea 吗?非常感谢.

推荐答案

我想我会更新一下,以防将来有人无意中发现这个页面.从1.5.3开始,mongo现在支持一个真正的$or操作符:http://www.mongodb.org/display/DOCS/Advanced+Queries#AdvancedQueries-%24or

Your query of "(expires >= Now()) OR (expires IS NULL)" can now be rendered as:

{$or: [{expires: {$gte: new Date()}}, {expires: null}]}

Mongodb相关问答推荐

从两个相连的文件中获取电话和邮箱的渠道是什么?

在服务器上部署后端时判断??=默认判断

为什么 mongoose 在 mongodb 中找不到我的数据

如何在 MongoDB 中的集合下查找同一文档

来自嵌套对象数组的 MongoDB 聚合

DB.collection('comments').find() 不工作

Nestjs中不同语言数据的mongodb聚合代码

如何在 MongoDB 中存储时间?作为字符串?给出任意年/月/日?

为什么 local.oplog.rs 上每隔几分钟的活动就会锁定 mongo 客户端

Node.js 和 MongoDB,重用 DB 对象

如何使用 -number 后缀对字符串进行 MongoDB 查询排序?

Mongo DB中的Upserting和Id问题

将 MongoDB 数据库复制到本地计算机

如何在 Node.js 中格式化 Mongoose 的日期?

MongoDB 中的多个 $inc 更新

Mongoose Select 要从 findOneAndUpdate 返回的字段

如何判断 MongoDB 中是否存在字段?

将新值推送到 mongodb 内部数组 - mongodb/php

是否可以在 Mongodb 中的两个数据库之间进行 $lookup 聚合?

MissingSchemaError:Schema hasn't been registered for model