英文:
how to query mongodb using java
问题
DBCollection collection = db.getCollection("FNTeams");
DBObject unwind1 = new BasicDBObject("$unwind", "$mailIDs");
DBObject lookup = new BasicDBObject("$lookup", new BasicDBObject("from", "FNContacts")
.append("localField", "mailIDs").append("foreignField", "_id").append("as", "productObjects"));
BasicDBObject pushField = new BasicDBObject();
pushField.append("_id", "$_id");
pushField.append("UserID", new BasicDBObject("$push", "$UserID"));
pushField.append("TeamName", new BasicDBObject("$push", "$TeamName"));
pushField.append("productObjects", new BasicDBObject("$push", "$productObjects"));
DBObject group = new BasicDBObject("$group", pushField);
AggregationOutput output = collection.aggregate(Arrays.asList(group, lookup, unwind1));
注意:这里我只翻译了你提供的代码部分,不包含问题描述和其他内容。如果你有更多需要翻译的内容或其他问题,请随时提问。
英文:
db.FNTeams.aggregate([ { "$unwind": "$mailIDs" },{ "$lookup": {"from":
"FNContacts", "localField": "mailIDs", "foreignField": "_id", "as":
"productObjects" }}, { "$group": { "_id": "$_id", "mailIDs": { "$push":
"$mailIDs" }, "UserID":{"$push":"$UserID"},"TeamName":
{"$push":"$TeamName"},"productObjects": { "$push": "$productObjects" }}}])
How to query the above query in java, I have tried using below code
DBCollection collection = db.getCollection("FNActivity");
DBObject unwind1 = new BasicDBObject("$unwind", "$mailIDs");
DBObject lookup = new BasicDBObject("$lookup", new BasicDBObject("from",
"FNContacts")
.append("localField", "mailIDs").append("foreignField",
"_id").append("as", "mailWithID"));
BasicDBObject pushField = new BasicDBObject();
pushField.append("_id", "$_id");
pushField.append("UserID", new BasicDBObject("$push", "$UserID"));
pushField.append("TeamName", new BasicDBObject("$push", "$TeamName"));
pushField.append("TeamDesc", new BasicDBObject("$push", "$TeamDesc"));
pushField.append("Status", new BasicDBObject("$push", "$Status"));
pushField.append("MailWithID", new BasicDBObject("$push",
"$mailWithID"));
DBObject group = new BasicDBObject("$group", pushField);
AggregationOutput output = collection.aggregate(Arrays.asList(group,
lookup, unwind1));
but I'm getting empty in output. The above query gives the exact output what I want while run in cmd.
答案1
得分: 1
你可以使用 AggregationOperation 类,如 此链接 中所示:
public static void checkMongoOperations(){
ApplicationContext ctx = new AnnotationConfigApplicationContext(MongoConfig.class);
MongoOperations mongoOperation = (MongoOperations) ctx.getBean("mongoTemplate");
AggregationOperation match = Aggregation.match(Criteria.where("country").is("tiro"));
AggregationOperation unwind = Aggregation.unwind("myDetails");
AggregationOperation match2 = Aggregation.match(Criteria.where("myDetails.type").is("health"));
AggregationOperation sort = Aggregation.sort(Sort.Direction.ASC, "myDetails.datetime");
AggregationOperation limit = Aggregation.limit(1);
Aggregation aggregation = Aggregation.newAggregation(match, unwind, match2, sort, limit);
System.out.println("Aggregation = " + aggregation);
AggregationResults<AggregateFactoryResult> output = mongoOperation.aggregate(aggregation, "gui_data", AggregateFactoryResult.class);
System.out.println("output = " + output.getMappedResults().get(0).getCountry());
}
英文:
You can use AggregationOperation class, as specified in this link
Something like this:
public static void checkMongoOperations(){
ApplicationContext ctx = new AnnotationConfigApplicationContext(MongoConfig.class);
MongoOperations mongoOperation = (MongoOperations) ctx.getBean("mongoTemplate");
AggregationOperation match = Aggregation.match(Criteria.where("country").is("tiro"));
AggregationOperation unwind = Aggregation.unwind("myDetails");
AggregationOperation match2 = Aggregation.match(Criteria.where("myDetails.type").is("health"));
AggregationOperation sort = Aggregation.sort(Sort.Direction.ASC, "myDetails.datetime");
AggregationOperation limit = Aggregation.limit(1);
Aggregation aggregation = Aggregation.newAggregation(match, unwind, match2, sort, limit);
System.out.println("Aggregation = "+aggregation);
AggregationResults<AggregateFactoryResult> output = mongoOperation.aggregate(aggregation, "gui_data", AggregateFactoryResult.class);
System.out.println("output = "+output.getMappedResults().get(0).getCountry());
}
答案2
得分: 0
你可能会使用Spring Boot应用程序和Spring Data来编写代码部分。具体内容可以参考以下链接:https://spring.io/guides/gs/accessing-data-mongodb/。
英文:
you might write a spring boot application with spring-data
https://spring.io/guides/gs/accessing-data-mongodb/
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论