简体   繁体   English

使用ObjectId的字符串表示形式搜索mongo文档

[英]Searching for mongo documents using string representations of the ObjectId

I am trying to return the a document using the string representation of its id (ObjectId) from a java class using Morphia. 我正在尝试使用Morphia从Java类使用其id(ObjectId)的字符串表示形式返回文档。 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. SmsReply类具有需要查找的Sms文档的ObjectId(externalMessageId)的字符串表示形式。

Thanks for your help. 谢谢你的帮助。

如果_id是mongo中的ObjectId,则可以使用ObjectId上的String构造函数创建一个ObjectId实例java:new ObjectId(reply.getExternalMessageId())

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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