Re: Camel Mongo findById // empty results

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

Re: Camel Mongo findById // empty results

Pasquale Congiusti
Hi Reji,
actually you're providing a String type to your body, and Mongo does not
treat the "_id" field as a String but as an ObjectId type.

Please, change your setBody method to:

.setBody(constant(new ObjectId("5ea1c4ae0911be2db0008909")))

and everything should work as expected.

Cheers,
Pasquale.

On Mon, Apr 27, 2020 at 4:07 PM Reji Mathews <[hidden email]>
wrote:

> Hello Camel Community
>
> I am trying to use camel to query records based on mongo id with operation
> "findById". Following is the route snippet
>
> public void configure() throws Exception {
>     from("jetty:http://0.0.0.0:8081/mongo?httpMethodRestrict=GET")
>             .setBody(constant("5ea1c4ae0911be2db0008909"))
>
> .to("mongodb:mongoClient?database=connectortesting&operation=findById&collection=requests")
>             .log("Mongo response ${body}")
>             .end();
> }
>
> I have following records in my test db
>
> > db.requests.find({});
> { "_id" : ObjectId("5ea1c4ae0911be2db0008909"), "name" : "mark", "age" :
> 40, "address" : "toronto, canada", "work" : { "company" : "mycompany",
> "company_phone" : "+1437447xxxx" } }
> { "_id" : ObjectId("5ea1f4df5e8c086e869250e3"), "name" : "mark", "age" :
> 43, "address" : "toronto, canada", "work" : { "company" : " mycompany ",
> "company_phone" : "+143744xxxxx" } }
> { "_id" : ObjectId("5ea1f4f05e8c086e869250e4"), "name" : "hima", "age" :
> 65, "address" : "toronto, canada", "work" : { "company" : " mycompany ",
> "company_phone" : "+143744xxxxx" } }
> { "_id" : ObjectId("5ea1f4f75e8c086e869250e5"), "name" : "paul", "age" :
> 45, "address" : "toronto, canada", "work" : { "company" : " mycompany ",
> "company_phone" : "+1437xxxxxxxx" } }
> { "_id" : ObjectId("5ea1f4fd5e8c086e869250e6"), "name" : "tim", "age" :
> 34, "address" : "toronto, canada", "work" : { "company" : " mycompany ",
> "company_phone" : "+143744xxxxxx" } }
>
> I was expecting mongodb component to return 1st record where mongoId
> matches "_id" but I get an empty result set. Am I doing something different
> than expected here?
>
> MongoDB documentation says "This operation retrieves only one element from
> the collection whose _id field matches the content of the IN message body"
> . Hence ideally, this should return first record
>
> PS:  I am using camel version 2.24.0
>
> Cheers
>
>
Reply | Threaded
Open this post in threaded view
|

Re: Camel Mongo findById // empty results

Claus Ibsen-2
Hi

Nice one Pasquale. I wonder if the docs are highlighting this? Maybe
we can add this as an example.
PR is welcome, we love contributions.

You can do this in the adoc file in src/main/docs of the component.
You can find that on github

On Wed, Apr 29, 2020 at 9:35 AM Pasquale Congiusti
<[hidden email]> wrote:

>
> Hi Reji,
> actually you're providing a String type to your body, and Mongo does not
> treat the "_id" field as a String but as an ObjectId type.
>
> Please, change your setBody method to:
>
> .setBody(constant(new ObjectId("5ea1c4ae0911be2db0008909")))
>
> and everything should work as expected.
>
> Cheers,
> Pasquale.
>
> On Mon, Apr 27, 2020 at 4:07 PM Reji Mathews <[hidden email]>
> wrote:
>
> > Hello Camel Community
> >
> > I am trying to use camel to query records based on mongo id with operation
> > "findById". Following is the route snippet
> >
> > public void configure() throws Exception {
> >     from("jetty:http://0.0.0.0:8081/mongo?httpMethodRestrict=GET")
> >             .setBody(constant("5ea1c4ae0911be2db0008909"))
> >
> > .to("mongodb:mongoClient?database=connectortesting&operation=findById&collection=requests")
> >             .log("Mongo response ${body}")
> >             .end();
> > }
> >
> > I have following records in my test db
> >
> > > db.requests.find({});
> > { "_id" : ObjectId("5ea1c4ae0911be2db0008909"), "name" : "mark", "age" :
> > 40, "address" : "toronto, canada", "work" : { "company" : "mycompany",
> > "company_phone" : "+1437447xxxx" } }
> > { "_id" : ObjectId("5ea1f4df5e8c086e869250e3"), "name" : "mark", "age" :
> > 43, "address" : "toronto, canada", "work" : { "company" : " mycompany ",
> > "company_phone" : "+143744xxxxx" } }
> > { "_id" : ObjectId("5ea1f4f05e8c086e869250e4"), "name" : "hima", "age" :
> > 65, "address" : "toronto, canada", "work" : { "company" : " mycompany ",
> > "company_phone" : "+143744xxxxx" } }
> > { "_id" : ObjectId("5ea1f4f75e8c086e869250e5"), "name" : "paul", "age" :
> > 45, "address" : "toronto, canada", "work" : { "company" : " mycompany ",
> > "company_phone" : "+1437xxxxxxxx" } }
> > { "_id" : ObjectId("5ea1f4fd5e8c086e869250e6"), "name" : "tim", "age" :
> > 34, "address" : "toronto, canada", "work" : { "company" : " mycompany ",
> > "company_phone" : "+143744xxxxxx" } }
> >
> > I was expecting mongodb component to return 1st record where mongoId
> > matches "_id" but I get an empty result set. Am I doing something different
> > than expected here?
> >
> > MongoDB documentation says "This operation retrieves only one element from
> > the collection whose _id field matches the content of the IN message body"
> > . Hence ideally, this should return first record
> >
> > PS:  I am using camel version 2.24.0
> >
> > Cheers
> >
> >



--
Claus Ibsen
-----------------
http://davsclaus.com @davsclaus
Camel in Action 2: https://www.manning.com/ibsen2
Reply | Threaded
Open this post in threaded view
|

Re: Camel Mongo findById // empty results

Pasquale Congiusti
Yeah, probably we can add a note to warn the users that default _id is
treated as an ObjectId type by Mongo, I'll take care of that shortly.

I've also planned to write a camel-example based on Mongo, as soon as it's
ready I'll raise a PR as well.

Cheers,
Pasquale.

On Wed, Apr 29, 2020 at 3:05 PM Claus Ibsen <[hidden email]> wrote:

> Hi
>
> Nice one Pasquale. I wonder if the docs are highlighting this? Maybe
> we can add this as an example.
> PR is welcome, we love contributions.
>
> You can do this in the adoc file in src/main/docs of the component.
> You can find that on github
>
> On Wed, Apr 29, 2020 at 9:35 AM Pasquale Congiusti
> <[hidden email]> wrote:
> >
> > Hi Reji,
> > actually you're providing a String type to your body, and Mongo does not
> > treat the "_id" field as a String but as an ObjectId type.
> >
> > Please, change your setBody method to:
> >
> > .setBody(constant(new ObjectId("5ea1c4ae0911be2db0008909")))
> >
> > and everything should work as expected.
> >
> > Cheers,
> > Pasquale.
> >
> > On Mon, Apr 27, 2020 at 4:07 PM Reji Mathews <[hidden email]>
> > wrote:
> >
> > > Hello Camel Community
> > >
> > > I am trying to use camel to query records based on mongo id with
> operation
> > > "findById". Following is the route snippet
> > >
> > > public void configure() throws Exception {
> > >     from("jetty:http://0.0.0.0:8081/mongo?httpMethodRestrict=GET")
> > >             .setBody(constant("5ea1c4ae0911be2db0008909"))
> > >
> > >
> .to("mongodb:mongoClient?database=connectortesting&operation=findById&collection=requests")
> > >             .log("Mongo response ${body}")
> > >             .end();
> > > }
> > >
> > > I have following records in my test db
> > >
> > > > db.requests.find({});
> > > { "_id" : ObjectId("5ea1c4ae0911be2db0008909"), "name" : "mark", "age"
> :
> > > 40, "address" : "toronto, canada", "work" : { "company" : "mycompany",
> > > "company_phone" : "+1437447xxxx" } }
> > > { "_id" : ObjectId("5ea1f4df5e8c086e869250e3"), "name" : "mark", "age"
> :
> > > 43, "address" : "toronto, canada", "work" : { "company" : " mycompany
> ",
> > > "company_phone" : "+143744xxxxx" } }
> > > { "_id" : ObjectId("5ea1f4f05e8c086e869250e4"), "name" : "hima", "age"
> :
> > > 65, "address" : "toronto, canada", "work" : { "company" : " mycompany
> ",
> > > "company_phone" : "+143744xxxxx" } }
> > > { "_id" : ObjectId("5ea1f4f75e8c086e869250e5"), "name" : "paul", "age"
> :
> > > 45, "address" : "toronto, canada", "work" : { "company" : " mycompany
> ",
> > > "company_phone" : "+1437xxxxxxxx" } }
> > > { "_id" : ObjectId("5ea1f4fd5e8c086e869250e6"), "name" : "tim", "age" :
> > > 34, "address" : "toronto, canada", "work" : { "company" : " mycompany
> ",
> > > "company_phone" : "+143744xxxxxx" } }
> > >
> > > I was expecting mongodb component to return 1st record where mongoId
> > > matches "_id" but I get an empty result set. Am I doing something
> different
> > > than expected here?
> > >
> > > MongoDB documentation says "This operation retrieves only one element
> from
> > > the collection whose _id field matches the content of the IN message
> body"
> > > . Hence ideally, this should return first record
> > >
> > > PS:  I am using camel version 2.24.0
> > >
> > > Cheers
> > >
> > >
>
>
>
> --
> Claus Ibsen
> -----------------
> http://davsclaus.com @davsclaus
> Camel in Action 2: https://www.manning.com/ibsen2
>
Reply | Threaded
Open this post in threaded view
|

Re: Camel Mongo findById // empty results

Pasquale Congiusti
A note for Reji,
as you probably will use the body coming from some source as an input you
may wish to use the convert method instead of setting the body with the
proper type:

.convertBodyTo(ObjectId.class)

I've just updated the documentation highlighting that.

Cheers,
Pasquale.

On Thu, Apr 30, 2020 at 9:13 AM Pasquale Congiusti <
[hidden email]> wrote:

> Yeah, probably we can add a note to warn the users that default _id is
> treated as an ObjectId type by Mongo, I'll take care of that shortly.
>
> I've also planned to write a camel-example based on Mongo, as soon as it's
> ready I'll raise a PR as well.
>
> Cheers,
> Pasquale.
>
> On Wed, Apr 29, 2020 at 3:05 PM Claus Ibsen <[hidden email]> wrote:
>
>> Hi
>>
>> Nice one Pasquale. I wonder if the docs are highlighting this? Maybe
>> we can add this as an example.
>> PR is welcome, we love contributions.
>>
>> You can do this in the adoc file in src/main/docs of the component.
>> You can find that on github
>>
>> On Wed, Apr 29, 2020 at 9:35 AM Pasquale Congiusti
>> <[hidden email]> wrote:
>> >
>> > Hi Reji,
>> > actually you're providing a String type to your body, and Mongo does not
>> > treat the "_id" field as a String but as an ObjectId type.
>> >
>> > Please, change your setBody method to:
>> >
>> > .setBody(constant(new ObjectId("5ea1c4ae0911be2db0008909")))
>> >
>> > and everything should work as expected.
>> >
>> > Cheers,
>> > Pasquale.
>> >
>> > On Mon, Apr 27, 2020 at 4:07 PM Reji Mathews <[hidden email]>
>> > wrote:
>> >
>> > > Hello Camel Community
>> > >
>> > > I am trying to use camel to query records based on mongo id with
>> operation
>> > > "findById". Following is the route snippet
>> > >
>> > > public void configure() throws Exception {
>> > >     from("jetty:http://0.0.0.0:8081/mongo?httpMethodRestrict=GET")
>> > >             .setBody(constant("5ea1c4ae0911be2db0008909"))
>> > >
>> > >
>> .to("mongodb:mongoClient?database=connectortesting&operation=findById&collection=requests")
>> > >             .log("Mongo response ${body}")
>> > >             .end();
>> > > }
>> > >
>> > > I have following records in my test db
>> > >
>> > > > db.requests.find({});
>> > > { "_id" : ObjectId("5ea1c4ae0911be2db0008909"), "name" : "mark",
>> "age" :
>> > > 40, "address" : "toronto, canada", "work" : { "company" : "mycompany",
>> > > "company_phone" : "+1437447xxxx" } }
>> > > { "_id" : ObjectId("5ea1f4df5e8c086e869250e3"), "name" : "mark",
>> "age" :
>> > > 43, "address" : "toronto, canada", "work" : { "company" : " mycompany
>> ",
>> > > "company_phone" : "+143744xxxxx" } }
>> > > { "_id" : ObjectId("5ea1f4f05e8c086e869250e4"), "name" : "hima",
>> "age" :
>> > > 65, "address" : "toronto, canada", "work" : { "company" : " mycompany
>> ",
>> > > "company_phone" : "+143744xxxxx" } }
>> > > { "_id" : ObjectId("5ea1f4f75e8c086e869250e5"), "name" : "paul",
>> "age" :
>> > > 45, "address" : "toronto, canada", "work" : { "company" : " mycompany
>> ",
>> > > "company_phone" : "+1437xxxxxxxx" } }
>> > > { "_id" : ObjectId("5ea1f4fd5e8c086e869250e6"), "name" : "tim", "age"
>> :
>> > > 34, "address" : "toronto, canada", "work" : { "company" : " mycompany
>> ",
>> > > "company_phone" : "+143744xxxxxx" } }
>> > >
>> > > I was expecting mongodb component to return 1st record where mongoId
>> > > matches "_id" but I get an empty result set. Am I doing something
>> different
>> > > than expected here?
>> > >
>> > > MongoDB documentation says "This operation retrieves only one element
>> from
>> > > the collection whose _id field matches the content of the IN message
>> body"
>> > > . Hence ideally, this should return first record
>> > >
>> > > PS:  I am using camel version 2.24.0
>> > >
>> > > Cheers
>> > >
>> > >
>>
>>
>>
>> --
>> Claus Ibsen
>> -----------------
>> http://davsclaus.com @davsclaus
>> Camel in Action 2: https://www.manning.com/ibsen2
>>
>
Reply | Threaded
Open this post in threaded view
|

Re: Camel Mongo findById // empty results

contactreji
Thanks Pasquale for this one. I will definitely try this one

On Thu, Apr 30, 2020 at 3:43 AM Pasquale Congiusti <
[hidden email]> wrote:

> A note for Reji,
> as you probably will use the body coming from some source as an input you
> may wish to use the convert method instead of setting the body with the
> proper type:
>
> .convertBodyTo(ObjectId.class)
>
> I've just updated the documentation highlighting that.
>
> Cheers,
> Pasquale.
>
> On Thu, Apr 30, 2020 at 9:13 AM Pasquale Congiusti <
> [hidden email]> wrote:
>
> > Yeah, probably we can add a note to warn the users that default _id is
> > treated as an ObjectId type by Mongo, I'll take care of that shortly.
> >
> > I've also planned to write a camel-example based on Mongo, as soon as
> it's
> > ready I'll raise a PR as well.
> >
> > Cheers,
> > Pasquale.
> >
> > On Wed, Apr 29, 2020 at 3:05 PM Claus Ibsen <[hidden email]>
> wrote:
> >
> >> Hi
> >>
> >> Nice one Pasquale. I wonder if the docs are highlighting this? Maybe
> >> we can add this as an example.
> >> PR is welcome, we love contributions.
> >>
> >> You can do this in the adoc file in src/main/docs of the component.
> >> You can find that on github
> >>
> >> On Wed, Apr 29, 2020 at 9:35 AM Pasquale Congiusti
> >> <[hidden email]> wrote:
> >> >
> >> > Hi Reji,
> >> > actually you're providing a String type to your body, and Mongo does
> not
> >> > treat the "_id" field as a String but as an ObjectId type.
> >> >
> >> > Please, change your setBody method to:
> >> >
> >> > .setBody(constant(new ObjectId("5ea1c4ae0911be2db0008909")))
> >> >
> >> > and everything should work as expected.
> >> >
> >> > Cheers,
> >> > Pasquale.
> >> >
> >> > On Mon, Apr 27, 2020 at 4:07 PM Reji Mathews <[hidden email]
> >
> >> > wrote:
> >> >
> >> > > Hello Camel Community
> >> > >
> >> > > I am trying to use camel to query records based on mongo id with
> >> operation
> >> > > "findById". Following is the route snippet
> >> > >
> >> > > public void configure() throws Exception {
> >> > >     from("jetty:http://0.0.0.0:8081/mongo?httpMethodRestrict=GET")
> >> > >             .setBody(constant("5ea1c4ae0911be2db0008909"))
> >> > >
> >> > >
> >>
> .to("mongodb:mongoClient?database=connectortesting&operation=findById&collection=requests")
> >> > >             .log("Mongo response ${body}")
> >> > >             .end();
> >> > > }
> >> > >
> >> > > I have following records in my test db
> >> > >
> >> > > > db.requests.find({});
> >> > > { "_id" : ObjectId("5ea1c4ae0911be2db0008909"), "name" : "mark",
> >> "age" :
> >> > > 40, "address" : "toronto, canada", "work" : { "company" :
> "mycompany",
> >> > > "company_phone" : "+1437447xxxx" } }
> >> > > { "_id" : ObjectId("5ea1f4df5e8c086e869250e3"), "name" : "mark",
> >> "age" :
> >> > > 43, "address" : "toronto, canada", "work" : { "company" : "
> mycompany
> >> ",
> >> > > "company_phone" : "+143744xxxxx" } }
> >> > > { "_id" : ObjectId("5ea1f4f05e8c086e869250e4"), "name" : "hima",
> >> "age" :
> >> > > 65, "address" : "toronto, canada", "work" : { "company" : "
> mycompany
> >> ",
> >> > > "company_phone" : "+143744xxxxx" } }
> >> > > { "_id" : ObjectId("5ea1f4f75e8c086e869250e5"), "name" : "paul",
> >> "age" :
> >> > > 45, "address" : "toronto, canada", "work" : { "company" : "
> mycompany
> >> ",
> >> > > "company_phone" : "+1437xxxxxxxx" } }
> >> > > { "_id" : ObjectId("5ea1f4fd5e8c086e869250e6"), "name" : "tim",
> "age"
> >> :
> >> > > 34, "address" : "toronto, canada", "work" : { "company" : "
> mycompany
> >> ",
> >> > > "company_phone" : "+143744xxxxxx" } }
> >> > >
> >> > > I was expecting mongodb component to return 1st record where mongoId
> >> > > matches "_id" but I get an empty result set. Am I doing something
> >> different
> >> > > than expected here?
> >> > >
> >> > > MongoDB documentation says "This operation retrieves only one
> element
> >> from
> >> > > the collection whose _id field matches the content of the IN message
> >> body"
> >> > > . Hence ideally, this should return first record
> >> > >
> >> > > PS:  I am using camel version 2.24.0
> >> > >
> >> > > Cheers
> >> > >
> >> > >
> >>
> >>
> >>
> >> --
> >> Claus Ibsen
> >> -----------------
> >> http://davsclaus.com @davsclaus
> >> Camel in Action 2: https://www.manning.com/ibsen2
> >>
> >
>
Reji Mathews Sr. Engineer - Middleware Integrations / SOA ( Open Source - Apache Camel & Jboss Fuse ESB | Mule ESB ) LinkedIn - http://in.linkedin.com/pub/reji-mathews/31/9a2/40a Twitter - reji_mathews