There is some documentation and examples here
<https://developers.google.com/protocol-buffers/docs/javatutorial> showing
the basics of how to parse and serialize protos in Java. Ilia is right that
doing this and using the official protobuf binary format would be the most
reliable way to go.

However, it sounds like you want to be able to make MongoDB queries
referencing fields in your proto. In that case, storing serialized protos
will not work, because from what I understand MongoDB will just see them as
opaque binary blobs. If you need to query based on proto fields then I
believe you would need to convert the proto into BSON before storing it.
Probably the easiest way to do that would be to first convert to JSON using
JsonFormat
<https://github.com/protocolbuffers/protobuf/blob/master/java/util/src/main/java/com/google/protobuf/util/JsonFormat.java>
and
then presumably it's straightforward to go from JSON to BSON. I don't have
any experience doing this, though, so I can't say for sure how well it will
work. It might also be feasible to use the reflection API to serialize and
parse directly to and from BSON. Making protos queryable in MongoDB is not
a use case we have really thought about much, though.

On Thu, Sep 6, 2018 at 10:56 AM Chris Zhang <zrzll...@gmail.com> wrote:

> Sure, but do you know which util is best for convert the stored protobuf
> data back to Java Object?
>
> On Thursday, September 6, 2018 at 1:39:12 PM UTC-4, Ilia Mirkin wrote:
>>
>> Why not just store the serialized data of the protobuf instead? That's
>> kind of the whole point of protobuf...
>>
>> On Thu, Sep 6, 2018 at 1:27 PM, Chris Zhang <zrzl...@gmail.com> wrote:
>> > Hi Adam,
>> >
>> > Thanks for the response.
>> >
>> > We are trying to persist the protobuf generated java object into
>> mongoDB
>> > using Spring framework.
>> > However, when doing the querying from database, the spring framework
>> does
>> > not support any field name with underscore.
>> >
>> > Is there anyway we can work around?
>> >
>> > Thanks.
>> >
>> >
>> > On Thursday, September 6, 2018 at 1:05:38 PM UTC-4, Adam Cozzette
>> wrote:
>> >>
>> >> There is no way to remove the underscores without changing protoc. But
>> why
>> >> do you want to get rid of the underscores anyway? Those variables are
>> just a
>> >> private implementation detail and make no difference to the public
>> API.
>> >>
>> >> On Wed, Sep 5, 2018 at 1:07 PM Chris Zhang <zrzl...@gmail.com> wrote:
>> >>>
>> >>> Hi,
>> >>>
>> >>> I am new to Protobuf, and recently I found out that the java
>> generated
>> >>> code by protobuf has underscore by the end of each field names.
>> >>>
>> >>> For example,
>> >>>
>> >>> protobuf message file look like this:
>> >>>
>> >>> message DummyMessage [
>> >>>
>> >>>     string some_id = 1;
>> >>>     bool is_active = 2;
>> >>> }
>> >>>
>> >>> The generated java code is like this:
>> >>>
>> >>> Class DummyMessage {
>> >>>
>> >>>     String someId_;
>> >>>     boolean isActive_;
>> >>>
>> >>> }
>> >>>
>> >>> Is there any way to get rid of the underscore of each field?
>> >>>
>> >>> Thanks,
>> >>>
>> >>>
>> >>> --
>> >>> You received this message because you are subscribed to the Google
>> Groups
>> >>> "Protocol Buffers" group.
>> >>> To unsubscribe from this group and stop receiving emails from it,
>> send an
>> >>> email to protobuf+u...@googlegroups.com.
>> >>> To post to this group, send email to prot...@googlegroups.com.
>> >>> Visit this group at https://groups.google.com/group/protobuf.
>> >>> For more options, visit https://groups.google.com/d/optout.
>> >
>> > --
>> > You received this message because you are subscribed to the Google
>> Groups
>> > "Protocol Buffers" group.
>> > To unsubscribe from this group and stop receiving emails from it, send
>> an
>> > email to protobuf+u...@googlegroups.com.
>> > To post to this group, send email to prot...@googlegroups.com.
>> > Visit this group at https://groups.google.com/group/protobuf.
>> > For more options, visit https://groups.google.com/d/optout.
>>
> --
> You received this message because you are subscribed to the Google Groups
> "Protocol Buffers" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to protobuf+unsubscr...@googlegroups.com.
> To post to this group, send email to protobuf@googlegroups.com.
> Visit this group at https://groups.google.com/group/protobuf.
> For more options, visit https://groups.google.com/d/optout.
>

-- 
You received this message because you are subscribed to the Google Groups 
"Protocol Buffers" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to protobuf+unsubscr...@googlegroups.com.
To post to this group, send email to protobuf@googlegroups.com.
Visit this group at https://groups.google.com/group/protobuf.
For more options, visit https://groups.google.com/d/optout.

Reply via email to