-1

i have a class Named Device

@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;
}

}

another class named 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;
}

and this is the MesuresRepository who extend from Mongo Repository

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

}

and this is the main app (Spring boot app)

  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);

and the database look like :

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

and the restController like this

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

and this is the measure 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")
}

and this the device document after changing

 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")
            }
    ]
o.O
  • 155
  • 5
  • 16
  • may be duplicate of this http://stackoverflow.com/questions/26986183/spring-mongodb-get-id-of-inserted-item-after-save – Gaurav Kumar Singh Mar 05 '17 at 15:17
  • How is `device` in `Mesures` associated ? You may want to create a `DBRef`. – s7vr Mar 05 '17 at 16:57
  • the same thing it returns a white page without any error – o.O Mar 05 '17 at 18:19
  • So let me understand this correctly you are trying to get the `Mesures` record by `deviseId` correct ? If yes you have to save the reference of `device` in `Mesures` doc. Please correct me if I am wrong or better update the question with input and expected output. – s7vr Mar 05 '17 at 21:39
  • i'm trinng to get the last record by deviseId . and why i should save the reference of device in mesures ? in the next time i will receive this measures from cloud because i'm working with gauge and evrey period of time it must update itself with the last value . so evrey time the sript request this URL :"dataStreamUrl": "http://localhost:8080/mesures/id" it shoud give me the lastest value – o.O Mar 05 '17 at 21:57
  • The only reason I said that is because you are using `MesureRepository` which is repository over `Mesures` collection and it doesn't have `devise` reference for you to query on `deviseId`. How will you know to pull `Mesures` for a given `deviseId` ? So other option is have to change to use `DeviseRepository` which can only be queried by `mesureId` for `mesures`(meaning you cant use `date`) – s7vr Mar 05 '17 at 22:06
  • evrey period of time the Device will sent the id of the device(reference) and the value x(variable during the time) wihcich is Mesure – o.O Mar 05 '17 at 22:19
  • I think the the solution would be to not use `DBRef` when you have to access the `Mesure` collection by `value`. You can add the `Mesure`s as embedded documents in the `Devise` collection and access them. – s7vr Mar 05 '17 at 22:24
  • in other word would say to get List of Mesures by deviceId ? but how i can get the last value in this case ? – o.O Mar 05 '17 at 22:25
  • yes that is an option too with current structure and than you do the sorting on the java side and pick the latest one by date. – s7vr Mar 05 '17 at 22:26
  • what do you mean by as embedded documents in the Devise – o.O Mar 05 '17 at 22:41
  • https://docs.mongodb.com/manual/tutorial/query-array-of-documents/#array-match-embedded-documents. Look at field `instock` – s7vr Mar 05 '17 at 22:42
  • do you mean to retreive @Dbref from Mesures ? after this change the same thing :/ – o.O Mar 06 '17 at 10:02
  • I thought little bit more about this. Can you just keep one collection where you store `mesure` information by `deviseId`? Something like `"_id" : ObjectId("58bd306500ff6d1670916a7d"), "_class" : "demo.model.Mesures", "reference" : "G2fgG123", "value" : 10, "date" : ISODate("2016-01-11T23:51:42Z")}` and after that you can use `public Mesures findTopByReferenceOrderByDateDesc(String reference);`. You can remove all the `DBRef`. Let me know what do you think. – s7vr Mar 07 '17 at 01:57

1 Answers1

0

Try to use mongodb's $natural operator:

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

BasicQuery basicQuery = new BasicQuery(new BasicDBObject());
basicQuery.setSortObject(new BasicDBObject("$natural", -1));
Atish
  • 4,277
  • 2
  • 24
  • 32