0

我有一个下面的集合示例。该数据在 cron 作业中每 20 分钟从端点提取一次。

{"id":AFFD6,"empty":8,"capacity":15,"ready":6,"t":1474370406,"_id":"kROabyTIQ5eNoIf1"}
    {"id":AFFD6,"empty":9,"capacity":15,"ready":5,"t":1474116005,"_id":"kX0DpoZ5fkMr2ezg"}
    {"id":AFFD6,"empty":9,"capacity":15,"ready":5,"t":1474684808,"_id":"ken1WRN47PTW159H"}
    {"id":AFFD6,"empty":9,"capacity":15,"ready":5,"t":1474117205,"_id":"kes1gDlG1sBjgV1R"}
    {"id":AFFD6,"empty":10,"capacity":15,"ready":4,"t":1474264806,"_id":"khILUjzGEPOn0c2P"}
    {"id":AFFD6,"empty":9,"capacity":15,"ready":5,"t":1474275606,"_id":"ko9r8u860es7E2hI"}
    {"id":AFFD6,"empty":9,"capacity":15,"ready":5,"t":1474591207,"_id":"kpLS6mCtkIiffTrN"}

我想丢弃任何不显示empty(以及因此)变化的文档(行ready)。我的目标是在这个集合中找到这些值发生变化的最新时间戳。

更好地说明,我想将它减少到值改变的地方:

{"id":AFFD6,"empty":8,"capacity":15,"ready":6,"t":1474370406,"_id":"kROabyTIQ5eNoIf1"}
{"id":AFFD6,"empty":9,"capacity":15,"ready":5,"t":1474117205,"_id":"kes1gDlG1sBjgV1R"}
{"id":AFFD6,"empty":10,"capacity":15,"ready":4,"t":1474264806,"_id":"khILUjzGEPOn0c2P"}
{"id":AFFD6,"empty":9,"capacity":15,"ready":5,"t":1474591207,"_id":"kpLS6mCtkIiffTrN"}

我可以在 MongoDB 查询中执行此操作吗?还是我最好使用 JavaScript 过滤器功能?

4

1 回答 1

1

MongoDB 允许您在索引上指定唯一约束。这些约束阻止应用程序插入对插入的字段具有重复值的文档。

使用以下代码使唯一

db.collection.createIndex( { "id": 1 }, { unique: true } )

另请参阅MongoDB 文档以获得更多说明。

于 2016-09-24T15:58:48.017 回答