简体   繁体   中英

Query multiple document types Spring Mongo

I am trying to extract multiple document types from Mongo database in a single query. If I use find method it requires me to specify class type, and therefore restricting me to get only one document type. For example if I have a collection that contains MongoDocumentA and MongoDocumentB, myList will get MongoDocumentA objects and any other documents will be nulls. Is there anyway around this?

Query query = new Query(Criteria.where("cir_group_status").ne(3).andOperator(Criteria.where("time").gte(startTime),Criteria.where("time").lt (stopTime)));
List myList = mongoOperation.find(query, MongoDocumentA.class);

Much appreciated!

I don't think this is possible in MongoDB, let alone in Spring's Mongo. If it did exist in Spring, I bet it would just perform two queries.

Take a look at this for some possible workarounds. It is not exactly the same problem as what you are trying to do, but I think the workarounds would be the same.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM