Unrecognized pipeline stage name: '$currentDate'

Hi there!

Using a standalone instance of MongoDB 4.4.1 Community and the Java driver (org.mongodb:mongodb-driver-sync:4.1.1), when I execute the following operation:

 final List<Bson> updates = new ArrayList<>();
        updates.add(Updates.set("projectId", "p1"));
        updates.add(Updates.set("userId", "u1"));
        updates.add(Updates.set("visitorId", "v1"));
        updates.add(Updates.currentTimestamp("lastSeenTime"));

        final Document updatedDocument =
                this.visitorsCollection.findOneAndUpdate(
                        and(eq("projectId", visitor.getProjectId()),
                                or(eq("userId", visitor.getUserId()), eq("visitorId", visitor.getVisitorId()))),
                        updates,
                        new FindOneAndUpdateOptions().returnDocument(ReturnDocument.AFTER).upsert(true));

I am getting the following error:

Exception in thread “main” com.mongodb.MongoCommandException: Command failed with error 40324 (Location40324): ‘Unrecognized pipeline stage name: ‘$currentDate’’ on server 35.238.203.251:53254. The full response is {“ok”: 0.0, “errmsg”: “Unrecognized pipeline stage name: ‘$currentDate’”, “code”: 40324, “codeName”: “Location40324”}
at com.mongodb.internal.connection.ProtocolHelper.getCommandFailureException(ProtocolHelper.java:175)
at com.mongodb.internal.connection.InternalStreamConnection.receiveCommandMessageResponse(InternalStreamConnection.java:359)
at com.mongodb.internal.connection.InternalStreamConnection.sendAndReceive(InternalStreamConnection.java:280)
at com.mongodb.internal.connection.UsageTrackingInternalConnection.sendAndReceive(UsageTrackingInternalConnection.java:100)
at com.mongodb.internal.connection.DefaultConnectionPool$PooledConnection.sendAndReceive(DefaultConnectionPool.java:490)
at com.mongodb.internal.connection.CommandProtocolImpl.execute(CommandProtocolImpl.java:71)
at com.mongodb.internal.connection.DefaultServer$DefaultServerProtocolExecutor.execute(DefaultServer.java:255)
at com.mongodb.internal.connection.DefaultServerConnection.executeProtocol(DefaultServerConnection.java:202)
at com.mongodb.internal.connection.DefaultServerConnection.command(DefaultServerConnection.java:118)
at com.mongodb.internal.connection.DefaultServerConnection.command(DefaultServerConnection.java:110)
at com.mongodb.internal.operation.CommandOperationHelper$13.call(CommandOperationHelper.java:712)
at com.mongodb.internal.operation.OperationHelper.withReleasableConnection(OperationHelper.java:620)
at com.mongodb.internal.operation.CommandOperationHelper.executeRetryableCommand(CommandOperationHelper.java:705)
at com.mongodb.internal.operation.CommandOperationHelper.executeRetryableCommand(CommandOperationHelper.java:697)
at com.mongodb.internal.operation.BaseFindAndModifyOperation.execute(BaseFindAndModifyOperation.java:69)
at com.mongodb.client.internal.MongoClientDelegate$DelegateOperationExecutor.execute(MongoClientDelegate.java:195)
at com.mongodb.client.internal.MongoCollectionImpl.executeFindOneAndUpdate(MongoCollectionImpl.java:785)
at com.mongodb.client.internal.MongoCollectionImpl.findOneAndUpdate(MongoCollectionImpl.java:765)

The error is caused by the use of Updates.currentTimestamp(...). The same happens if I use Updates.setOnInsert(...). Are these actions not allowed with findOneAndUpdate? My purpose is to update some fields or create a new document with specific fields depending on whether a document if found or not.

See JAVA-3872: Unrecognized pipeline stage name: ‘$setOnInsert’ for my answer to an almost identical question.

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.