Opal
Opal

Reputation: 84864

Using querydsl with undetermined schema with mongo and spring boot

I've the following mapping:

@Document
class Event {
   Object carbonCopy; 
}

carbonCopy has several attributes and its schema is undetermined - various events can have different set of attributes. Assume that carbonCopy has status field. I need to find all Events that have carbonCopy.status equal to NEW. Is it possible with query DSL, how? I'm struggling with various mixes of Path and Expression instances to no avail.

Upvotes: 0

Views: 452

Answers (2)

Vipul Pandey
Vipul Pandey

Reputation: 1758

You can use similar like in terminal db.events.find({"carbonCopy.status":"NEW"}) in termilnal if you need existance as well

db.events.find({carbonCopy.status:{"$exists":true}},"carbonCopy.status":"NEW"})

NOW when it comes to SDM

Criteria criteria = Criteria.where("carbonCopy.status").is("NEW")

Query query = new Query(criteria);

return mongoTemplate.findOne(query, Event.class);

Criteria criteria = Criteria.where("carbonCopy.status").is("NEW").And("carbonCopy.status").exists(true) Query query = new Query(criteria); return mongoTemplate.findOne(query, Event.class);

Upvotes: 2

Opal
Opal

Reputation: 84864

It's possible with the following piece of code:

QEvent event = QEvent.event;

PathBuilder<Event> query = new PathBuilder<Event>(Event.class, "entity");
BooleanExpression expr = query.getMap("objectCarbonCopy", String.class, String.class).get("status").eq(Expressions.constant("NEW"));

Where QEvent is querydsl generated class.

Upvotes: 0

Related Questions