db.getCollection("tbdailydate").aggregate([ {$match:{'activity_no':'3VSWM91OVOZS6LP0Z9YWE11O',"create_time":{$gte:new Date("2023-05-12T00:00:00.000Z"),$lte:new Date("2023-05-21T23:59:59.000Z")}}}, {$group :{_id:"$create_time", usenum : {$sum : { $cond: [ {$and: [ {$gte: ["$create_time", new Date("2023-05-12T00:00:00.000Z")]}, {$lte: ["$create_time", new Date("2023-05-12T23:59:59.000Z")]}, ]}, "$use_num", 0 ] } }, weekend : { $sum : { $cond: [ {$and: [ {$gte: ["$create_time", new Date("2023-05-19T00:00:00.000Z")]}, {$lte: ["$create_time", new Date("2023-05-21T23:59:59.000Z")]}, ]}, "$use_num", 0 ] } } }} ])转成java格式的Aggregation方式
时间: 2024-04-15 15:29:49 浏览: 241
Sure! Here's the equivalent Java code for the given MongoDB aggregation pipeline:
```java
import org.bson.Document;
import com.mongodb.client.AggregateIterable;
import com.mongodb.client.MongoCollection;
import static com.mongodb.client.model.Aggregates.*;
import static com.mongodb.client.model.Filters.*;
import static com.mongodb.client.model.Accumulators.*;
// Assuming you have a MongoClient instance and have connected to the database
// Get the collection
MongoCollection<Document> collection = database.getCollection("tbdailydate");
// Create the pipeline
List<Document> pipeline = Arrays.asList(
match(and(eq("activity_no", "3VSWM91OVOZS6LP0Z9YWE11O"),
gte("create_time", new Date("2023-05-12T00:00:00.000Z")),
lte("create_time", new Date("2023-05-21T23:59:59.000Z")))),
group("$create_time",
sum("usenum",
cond(and(gte("create_time", new Date("2023-05-12T00:00:00.000Z")),
lte("create_time", new Date("2023-05-12T23:59:59.000Z")))),
"$use_num", 0)),
sum("weekend",
cond(and(gte("create_time", new Date("2023-05-19T00:00:00.000Z")),
lte("create_time", new Date("2023-05-21T23:59:59.000Z")))),
"$use_num", 0))
);
// Execute the aggregation
AggregateIterable<Document> result = collection.aggregate(pipeline);
```
Please make sure to import the necessary MongoDB Java driver classes and adjust the code according to your specific setup.
阅读全文