Question

I am trying to return the a document using the string representation of its id (ObjectId) from a java class using Morphia. How can I achieve this? The following code snippett illustrates the problem...

    @Override
public List<SmsReply> getReplies() {
    List<SmsReply> replies = smsProvider.getSmsReplies();
    for(SmsReply reply: replies){
        System.out.println(reply);
        Sms sms = datastore.find(Sms.class, "_id", reply.getExternalMessageId()).get();
        sms.getSmsReplies().add(reply);
        datastore.save(sms);
    }

    return smsProvider.getSmsReplies();
}

The SmsReply class has a string representation of the ObjectId (externalMessageId) of the Sms document that it needs to find.

Thanks for your help.

Was it helpful?

Solution

If _id is an ObjectId in mongo, you can create an ObjectId instance java using the String contructor on ObjectId: new ObjectId(reply.getExternalMessageId())

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top