简体   繁体   中英

Mongo DB / No duplicates

I have have a mongo collection that keeps state records for devices. Thus, there could be multiple records per device. What I would like to do is create a query through the mongoTemplate that gets the latest record for each device.

Here's the constraints:

  • Pass in a Set<'String'> name_ids, regular field within mongo collection not the _id or found within the _id
  • get only the latest record for each device with matching name_id
  • return List<'DeviceStateData'> (No duplicates should be found with the same name_id)

example of collection object:

{
  _id: "241324123412",
  name_id: "flyingMan",
  powerState:"ON",
  timeStamp: ISODate('')
}

Thanks

You should look on Distinct function.

Here you can find details with Spring.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM