如何通过仅保留更改点来减少集合?
How can I reduce a collection by keeping only change points?
我有一个集合示例如下。这些数据在 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 过滤功能?
MongoDB 允许您在索引上指定唯一约束。这些约束可防止应用程序插入对插入字段具有重复值的文档。
使用以下代码使唯一
db.collection.createIndex( { "id": 1 }, { unique: true } )
另请参阅 MongoDB documentation 以获得更多说明。
我有一个集合示例如下。这些数据在 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 过滤功能?
MongoDB 允许您在索引上指定唯一约束。这些约束可防止应用程序插入对插入字段具有重复值的文档。
使用以下代码使唯一
db.collection.createIndex( { "id": 1 }, { unique: true } )
另请参阅 MongoDB documentation 以获得更多说明。