mongoDB Async Java driver aggregation $lookup examples

1,444 views
Skip to first unread message

Krishna Kumar C

unread,
Dec 18, 2016, 12:37:12 PM12/18/16
to mongodb-user
Hi,
I am looking for mongoDB Async Java driver aggregation $lookup examples.
I have two collections A and B both are linked using one to one mapping. I am using two collections because we need to have getAllDocuments from both the collection and there will be write operations in both these collection. So i don't want to use the embed option. So seems like aggregation is the best option for such implementation. Unfortunately i couldn't find any Java driver Async examples.It would be useful if any of you can give some pointers.

Thanks!

regards
Krishna 

Wan Bachtiar

unread,
Dec 20, 2016, 1:21:29 AM12/20/16
to mongodb-user

Unfortunately i couldn’t find any Java driver Async examples.It would be useful if any of you can give some pointers.

Hi Krishna,

You can find async tutorials at MongoDB Java Async Driver, especially for aggregation see async aggregation framework .

For $lookup operator example you can execute code as below:

collection.aggregate(Arrays.asList(
                                    Aggregates.lookup("fromCollection", "localField", "foreignField", "asOutputArrayField")
                   )).forEach(printBlock, callbackWhenFinished);

See more information on Aggregation Lookup Builders.

Another great async code example are the tour example files. Also, as the nature of async is non-blocking, you may find the use of CountDownLatch useful for ‘await’ operations.

I am using two collections because we need to have getAllDocuments from both the collection and there will be write operations in both these collection. So i don’t want to use the embed option

Document modelling depends on your use case, as long as the model benefits your application usage of the data and the inherent structure of the data. You may find Data Model Examples and Patterns a good resource, it covers different types of relationships.

Regards,

Wan.

Reply all
Reply to author
Forward
0 new messages