Using date expressions in Java driver sync?

If I’m doing something like the query below as part of an aggregate:

    { $group: { 
      "_id": { $dateTrunc: { date: "$timestamp", unit: "minute", binSize: 1 } }, 
      "value": { $avg: "$value" }, 
      "id": { $first: "$metadata.id" } 
    } }

How am I able to use expression like dateTrunc or some other examples like dayOfMonth, year… In MongoDB Java Driver sync? I cannot find a function that relates to those, but can I specify it in some other way?

Hello @Bob_Doe, you can build the same Aggregation query in the MongoDB Compass. There is an Aggregation TAB next to the Documents TAB in the GUI, to create the same query and it can be exported to your favorite programming language (Java, is one of the options). The export converts the pipeline to Java code which you can copy and paste in your application.

Thanks for this! Was not aware of this functionality, and it did get it working.
Ideally I would hope that it used e.g. Accumulators and Projections, but just plain Document tree worked like a charm.

Hello, I’m getting error “Command failed with error 168 (InvalidPipelineOperator): 'Unrecognized expression ‘$dateTrunc’” with the code exported from MongoDB compass in my quarkus application. Any pointers? Already tried upgrading drivers to the latest stable version (4.5.1).

Full stack trace:

com.mongodb.MongoCommandException: Command failed with error 168 (InvalidPipelineOperator): 'Unrecognized expression '$dateTrunc'' on server localhost:53973. The full response is {"operationTime": {"$timestamp": {"t": 1650273747, "i": 3}}, "ok": 0.0, "errmsg": "Unrecognized expression '$dateTrunc'", "code": 168, "codeName": "InvalidPipelineOperator", "$clusterTime": {"clusterTime": {"$timestamp": {"t": 1650273747, "i": 3}}, "signature": {"hash": {"$binary": {"base64": "AAAAAAAAAAAAAAAAAAAAAAAAAAA=", "subType": "00"}}, "keyId": 0}}}

	at com.mongodb.internal.connection.ProtocolHelper.getCommandFailureException(ProtocolHelper.java:198)
	at com.mongodb.internal.connection.InternalStreamConnection.receiveCommandMessageResponse(InternalStreamConnection.java:418)
	at com.mongodb.internal.connection.InternalStreamConnection.sendAndReceive(InternalStreamConnection.java:342)
	at com.mongodb.internal.connection.UsageTrackingInternalConnection.sendAndReceive(UsageTrackingInternalConnection.java:116)
	at com.mongodb.internal.connection.DefaultConnectionPool$PooledConnection.sendAndReceive(DefaultConnectionPool.java:643)
	at com.mongodb.internal.connection.CommandProtocolImpl.execute(CommandProtocolImpl.java:71)
	at com.mongodb.internal.connection.DefaultServer$DefaultServerProtocolExecutor.execute(DefaultServer.java:240)
	at com.mongodb.internal.connection.DefaultServerConnection.executeProtocol(DefaultServerConnection.java:226)
	at com.mongodb.internal.connection.DefaultServerConnection.command(DefaultServerConnection.java:126)
	at com.mongodb.internal.connection.DefaultServerConnection.command(DefaultServerConnection.java:116)
	at com.mongodb.internal.connection.DefaultServer$OperationCountTrackingConnection.command(DefaultServer.java:345)
	at com.mongodb.internal.operation.CommandOperationHelper.createReadCommandAndExecute(CommandOperationHelper.java:230)
	at com.mongodb.internal.operation.CommandOperationHelper.lambda$executeRetryableRead$4(CommandOperationHelper.java:212)
	at com.mongodb.internal.operation.OperationHelper.lambda$withSourceAndConnection$2(OperationHelper.java:575)
	at com.mongodb.internal.operation.OperationHelper.withSuppliedResource(OperationHelper.java:600)
	at com.mongodb.internal.operation.OperationHelper.lambda$withSourceAndConnection$3(OperationHelper.java:574)
	at com.mongodb.internal.operation.OperationHelper.withSuppliedResource(OperationHelper.java:600)
	at com.mongodb.internal.operation.OperationHelper.withSourceAndConnection(OperationHelper.java:573)
	at com.mongodb.internal.operation.CommandOperationHelper.lambda$executeRetryableRead$5(CommandOperationHelper.java:209)
	at com.mongodb.internal.async.function.RetryingSyncSupplier.get(RetryingSyncSupplier.java:65)
	at com.mongodb.internal.operation.CommandOperationHelper.executeRetryableRead(CommandOperationHelper.java:215)
	at com.mongodb.internal.operation.CommandOperationHelper.executeRetryableRead(CommandOperationHelper.java:195)
	at com.mongodb.internal.operation.AggregateOperationImpl.execute(AggregateOperationImpl.java:195)
	at com.mongodb.internal.operation.AggregateOperation.execute(AggregateOperation.java:306)
	at com.mongodb.internal.operation.AggregateOperation.execute(AggregateOperation.java:46)
	at com.mongodb.client.internal.MongoClientDelegate$DelegateOperationExecutor.execute(MongoClientDelegate.java:191)
	at com.mongodb.client.internal.MongoIterableImpl.execute(MongoIterableImpl.java:135)
	at com.mongodb.client.internal.MongoIterableImpl.iterator(MongoIterableImpl.java:92)
	at com.techroots.qmongo.InvoiceService.totalRevenuePerYear(InvoiceService.java:54)
	at com.techroots.qmongo.InvoiceService_ClientProxy.totalRevenuePerYear(Unknown Source)
	at com.techroots.qmongo.InvoiceServiceTest.test(InvoiceServiceTest.java:28)

Here is my source code mongodb-quickstart/InvoiceService.java at main · faskan/mongodb-quickstart · GitHub

Hello @Faisal_Khan_Thayub_Khan, Welcome to the MongoDB Community forum!

Your aggregation code using Java driver is okay. It worked fine when I ran using Java SE 8, Java Driver 4.2.0 and MongoDB server v 5.0.7.

Note that $dateTrunc aggregate operator is introduced with MongoDB version 5.0. If your database version is prior to v5.0, you will get this error: com.mongodb.MongoCommandException: Command failed with error 168 (InvalidPipelineOperator): 'Unrecognized expression '$dateTrunc'' on server ....

Hope this helps!

Thank you @Prasad_Saya, it was really quick response. I just realised that I was running the aggregate on testcontainers for mongodb. It works perfectly fine with Mongodb. Thanks again!