如何使用Java查询MongoDB。

huangapple go评论81阅读模式
英文:

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(&quot;mongoTemplate&quot;); 

    AggregationOperation match = Aggregation.match(Criteria.where(&quot;country&quot;).is(&quot;tiro&quot;));
    AggregationOperation unwind = Aggregation.unwind(&quot;myDetails&quot;);
    AggregationOperation match2 = Aggregation.match(Criteria.where(&quot;myDetails.type&quot;).is(&quot;health&quot;));
    AggregationOperation sort = Aggregation.sort(Sort.Direction.ASC, &quot;myDetails.datetime&quot;);
    AggregationOperation limit = Aggregation.limit(1);

    Aggregation aggregation = Aggregation.newAggregation(match, unwind, match2, sort, limit);
    System.out.println(&quot;Aggregation = &quot;+aggregation);
    AggregationResults&lt;AggregateFactoryResult&gt; output = mongoOperation.aggregate(aggregation, &quot;gui_data&quot;, AggregateFactoryResult.class);
    System.out.println(&quot;output = &quot;+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/

huangapple
  • 本文由 发表于 2020年9月4日 15:09:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/63736447.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定