如何在spring mongodb中获取最后插入的值?

时间:2017-03-05 15:06:42

标签: spring mongodb spring-data spring-data-mongodb spring-data-rest

我有一个名为设备的类

@Document
public class Devise 
{

@Id
public String id;
public String reference;
@DBRef
public List<Mesures> listMes;

public Devise() {
    // TODO Auto-generated constructor stub
}

public Devise(String reference, List<Mesures> listMes) {
    super();
    this.reference = reference;
    this.listMes = listMes;
}

public String getId() {
    return id;
}

public void setId(String id) {
    this.id = id;
}

public String getReference() {
    return reference;
}

public void setReference(String reference) {
    this.reference = reference;
}

public List<Mesures> getListMes() {
    return listMes;
}

public void setListMes(List<Mesures> listMes) {
    this.listMes = listMes;
}

}

另一个名为Mesures的类

@Document
public class Mesures {

@Id
private String id;
private Double value;
private Date date;
@DBRef
private Devise devise;

public Mesures() {
    // TODO Auto-generated constructor stub
}

public Mesures(Double value, Date date, Devise devise) {
    super();
    this.value = value;
    this.date = date;
    this.devise = devise;
}

public Mesures(Double value, Date date) {
    super();
    this.value = value;
    this.date = date;
}

public String getId() {
    return id;
}

public void setId(String id) {
    this.id = id;
}

public Double getValue() {
    return value;
}

public void setValue(Double value) {
    this.value = value;
}

public Date getDate() {
    return date;
}

public void setDate(Date date) {
    this.date = date;
}

public Devise getDevise() {
    return devise;
}

public void setDevise(Devise devise) {
    this.devise = devise;
}

这是从Mongo Repository扩展的MesuresRepository

public interface MesureRepository extends MongoRepository<Mesures, String>{
public Mesures findTopByDeviseOrderByDateDesc(String DeviseId);

}

这是主应用程序(Spring启动应用程序)

  ApplicationContext ctx = SpringApplication.run(UiApplication.class, args);
    MesureDAO MDAO = ctx.getBean(MesureDAO.class);
    DeviceDAO DDAO = ctx.getBean(DeviceDAO.class);


    DateFormat df = new SimpleDateFormat("yyyy-mm-dd-hh-mm-ss");
    Mesures m1 = new Mesures(10.0, df.parse("2016-11-12-12-51-42"));
    Mesures m2 = new Mesures(15.2,df.parse("2016-11-12-12-52-42"));
    //add Mesure
    MDAO.addMesure(m1);
    MDAO.addMesure(m2);
    List<Mesures>listMes = new ArrayList<>();
    listMes.add(m1);
    listMes.add(m2);
    //add Device
    Devise D = new Devise("G2fgG123", listMes);
    DDAO.addDevice(D);

,数据库看起来像:

 db.devise.find().pretty()
{
    "_id" : ObjectId("58bc8218d980530898a0b047"),
    "_class" : "demo.model.Devise",
    "reference" : "G2fgG123",
    "listMes" : [
            DBRef("mesures", ObjectId("58bc8218d980530898a0b045")),
            DBRef("mesures", ObjectId("58bc8218d980530898a0b046"))
    ]
}

和像这样的restController

@RestController
public class MesuressController {
@Autowired
MesureRepository mesureRepo;
@RequestMapping(value = "/mesure/{DeviseId}")
public Mesures findByDeviseIdOrderByDate(@PathVariable("DeviseId")String  DeviseId) {
    return mesureRepo.findTopByDeviseIdOrderByDateDesc(DeviseId);
}}

这是措施Doc

db.mesures.find().pretty()
{
    "_id" : ObjectId("58bc8218d980530898a0b045"),
    "_class" : "demo.model.Mesures",
    "value" : 10,
    "date" : ISODate("2016-01-11T23:51:42Z")
}
{
    "_id" : ObjectId("58bc8218d980530898a0b046"),
    "_class" : "demo.model.Mesures",
    "value" : 15.2,
    "date" : ISODate("2016-01-11T23:52:42Z")
}

这是更改后的设备文档

 db.devise.find().pretty()
{
    "_id" : ObjectId("58bd306500ff6d1670916a7d"),
    "_class" : "demo.model.Devise",
    "reference" : "G2fgG123",
    "listMes" : [
            {
                    "_id" : ObjectId("58bd306500ff6d1670916a7b"),
                    "value" : 10,
                    "date" : ISODate("2016-01-11T23:51:42Z")
            },
            {
                    "_id" : ObjectId("58bd306500ff6d1670916a7c"),
                    "value" : 15.2,
                    "date" : ISODate("2016-01-11T23:52:42Z")
            }
    ]

1 个答案:

答案 0 :(得分:0)

尝试使用mongodb的$ natural运算符:

Query query = new Query().with(new Sort(Direction.ASC, "$natural"));

BasicQuery basicQuery = new BasicQuery(new BasicDBObject());
basicQuery.setSortObject(new BasicDBObject("$natural", -1));