Specify raw aggregate request with Spring Data Mongodb - mongodb

I write a big aggregate query for mongodb, now I try to translate it in Spring Data Mongodb API, and it's very complicated, and spring data api did not help me a lot.
So like with #Query annotation, is it possible to just specify my raw aggregate query in text and map my field with Spring Data (or just Mongodb Java driver) ?
I won't c/p my aggregate request because, it's not the purpose of my question.

I found a solution by using MongoDB java driver, which is available through Spring Data :
DBCollection collection = mongoTemplate.getCollection("myCollection");
and I used BasicDBObject from this solution : MongoDB aggregation with Java driver

Related

Query DBRef with Spring Data MongoDB using QueryDSL

I'm using Spring Data MongoDB and QueryDSL to perform some simple queries, but I'm having issues trying to use a predicate with a DBRef object's field.
It seems the DBRef are not resolved, so the query always returns empty results. There are some questions about this topic from 2014 mostly, and although there seem to have been some work done about it on both QueryDSL and Spring Data's side I still can't make it work and didn't find any working example.
I'm looking for a simple solution, as in the following simplified test case:
#Document
class Foo {
#Id Integer id;
#DBref Bar bar;
}
#Document
class Bar {
#Id Integer id;
String name;
}
interface FooRepository extends MongoRepository<Foo, Integer>, QueryDslPredicateExecutor<Foo> { ... }
and the query I'm trying to use :
fooRepository.findAll(QFoo.foo.bar.name.eq("test"))
I'm using QueryDSL 4.1.4, Spring Boot 1.5.3 with Spring Data MongoDB 1.10.3
Is this supported? Am I missing anything?
TL;DR;
It's not supported.
Explanation
You're expressing a predicate that follows the reference into a different document.
MongoDB is a document-oriented database that knows no joins. Each document may contain subdocuments or references (which are sort of artificial). Also, each document has to be either looked up individually or fetched as a collection of documents based on a query.
Because there are no joins built-in to MongoDB you can't query across DBRef references. You can express a query that scans the subdocument as the subdocument is embedded.
Spring Data MongoDB exposes functionality provided by MongoDB in a more user-friendly way and does not pretend to provide functionality that isn's supported by MongoDB.
Since MongoDB 3.4, the aggregation framework supports $graphLookup to let MongoDB lookup during the aggregation stage to look up individual documents. Lookups are expensive and that's something you'd rather want to avoid.

Do a MongoDB update with Spring XD & Spring Integration

I'm trying to read a file with Spring XD (source), modify data (processor) and save it in Mongo (sink). I've used the default MongoDB sink which do a save and expect a Spring Data Mongo entity.
But I'd like to do an update with upsert. How can I do ?
Thanks.
The mongo outbound adapter uses MongoTemplate.save() which is already an upsert according to the javadoc.
If you provide your own ObjectId for the same object it will update. You can for example do this during your processor.

MongoDB document to POJO

I'm using allanbank async driver for operations on mongodb. Is there any api available through which I can convert returned Document to a POJO, like we can do in spring driver available for mongodb
You could use morphia (which would mean using mongodb's java driver, or you could use jackson to map back to your POJOs.
You can use Spring Data , it includes integrated object mapping between documents and POJOs.
I wanted something for async driver as well. As MongoDB came with Codec Feature in 3.0 version (both sync and async version of driver), it's not neccessary to use libraries such as Morphia anymore so I wrote simple library for mapping POJO into MongoDB myself.
in Spring Data MongoDB you can use
#Autowired
MongoConverter mongoConverter;
Then
TARGET_OBJECT = mongoConverter.read(YOUR_TARGET.class, YOUR_DOCUMENT);

How to use DBCursor provided in mongodb java driver in spring-data-mongodb?

I want to read around 1 million documents from my mongodb database and I am using spring data mongodb. I do not want to read all of 1 million data at once for performance reasons. Is there any way in spring-data-mongodb to do this. In raw java driver we have DBCursor.
One way i know is using pagination through repositories. Is there any other way in latest versions of spring data mongodb?
Yes. You can use pagination with spring data mongodb. MongoRepository extends from PagingAndSortingRepository which means you can call findAll(Pagable) and provide page information.
Alternatively, you can use mongoOperations/mongoTemplate to get a DBCollection reference, and then call find() on the collection and that will return you the DBCursor you want.

Using Java can one store BSON documents in MongoDB directly bypassing the DBObject API?

I think Jongo does it by using the Jackson pipeline to efficiently convert a POJO into the respective BSON, thus bypassing any intermediaries, like DBObject. However, it depends on Jackson 2.x (now FasterXML) and I am not in a position to upgrade from Jackson 1.9.x, so it seems I cannot use Jongo.
So, I am using the standard Java mongo driver, which requires me to map a POJO into the respective DBObject (see Efficient POJO mapping to/from Java Mongo DBObject using Jackson), which I suppose is marshalled to a BSON buffer by the driver at some point before being sent to the Mongo server process.
My question is this - can I prepare the BSON buffer myself and just hand it over to the driver?