我正在第一次使用java为mongo db编写聚合查询。 我无法将之前编写的shell查询转换为java格式。 我正面临一些问题。下面是我已经编写的shell查询,它工作正常。
为房间设定日期。
{
"_id": ObjectId("571c5724db62826826d28d08"),
"conversationId": "6puebew70kke29",
"userId": "600",
"firstName": "Test",
"profileImagePath": "",
"created": ISODate("2016-04-24T05:18:28.753Z"),
"__v": 0
}
{
"_id": ObjectId("571c5724db62826826d28d09"),
"conversationId": "6puebew70kke29",
"userId": "900",
"firstName": "User",
"profileImagePath": "",
"created": ISODate("2016-04-24T05:18:28.754Z"),
"__v": 0
}
{
"_id": ObjectId("571c574edb62826826d28d0b"),
"conversationId": "fsny11z742kpgb9",
"userId": "600",
"firstName": "FitTest",
"profileImagePath": "",
"created": ISODate("2016-04-24T05:19:10.192Z"),
"__v": 0
}
{
"_id": ObjectId("571c574edb62826826d28d0c"),
"conversationId": "fsny11z742kpgb9",
"userId": "800",
"firstName": "Dev",
"profileImagePath": "",
"created": ISODate("2016-04-24T05:19:10.193Z"),
"__v": 0
}
rooms.aggregate([{
$match: {
type: 'PRIVATE'
}
}, {
$group: {
_id: '$conversationId',
users: {
$push: '$userId'
}
}
}, {
$match: {
users: {
$all: [friendProfileData.id, userprofileData.id]
}
}
}, ]
上述查询的Java代码。
Aggregation agg = newAggregation(
match(Criteria.where("type").is("PRIVATE")),
group("_id", "conversationId"),
group("users").push("userId").as("users")
);
AggregationResults<Rooms> groupResults = mongoOps.aggregate(agg, "rooms", Rooms.class);
List<Rooms> result = groupResults.getMappedResults();
无法完全完成它我不知道如何写几个表达式。 非常感谢您的帮助。
答案 0 :(得分:1)
我做了一个假设,因为我不确定&#34; friendProfileData&#34;和&#34; userprofileData&#34;最后一个属性#34;匹配&#34;。
您可以更改&#34; Filters.all&#34;根据您的要求相应声明。否则,此代码应符合您的要求,并且与#34; MongoDB 3.2.0&#34;和#34; Mongo Java Driver 3.2.2 Jar&#34;。
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.bson.Document;
import org.bson.conversions.Bson;
import com.mongodb.MongoClient;
import com.mongodb.client.AggregateIterable;
import com.mongodb.client.MongoCursor;
import com.mongodb.client.MongoDatabase;
import com.mongodb.client.model.Accumulators;
import com.mongodb.client.model.Aggregates;
import com.mongodb.client.model.Filters;
public static void main(String[] args) {
MongoClient client = new MongoClient();
MongoDatabase database = client.getDatabase("localhost");
List<Bson> aggregateList = new ArrayList<>();
aggregateList.add(Aggregates.match(Filters.eq("type", "PRIVATE")));
aggregateList.add(Aggregates.group("$conversationId", Accumulators.push("users", "$userId")));
aggregateList.add(Aggregates.match(Filters.all("users", Arrays.asList("1", "800"))));
AggregateIterable<Document> mongoCollectionList = database.getCollection("rooms")
.aggregate(aggregateList);
MongoCursor<Document> mongoCursor = mongoCollectionList.iterator();
while (mongoCursor.hasNext()) {
System.out.println(mongoCursor.next());
}
}
Maven依赖: -
以上正常工作,具有以下Maven依赖关系。
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongo-java-driver</artifactId>
<version>3.2.2</version>
</dependency>
请注意,如果您使用以下jar,上述可能无效:
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongodb-driver</artifactId>
<version>3.2.2</version>
</dependency>