Exception in thread "main" com.mongodb.MongoCommandException: Command failed with error 2 (BadValue): '$where is not allowed in this context'

Hi,
After upgrading mongoDB from 3.* version to 7.0 and mongo java driver from 3.4.2 to 4.11.1, one of my query to get the counts of documents in collection is giving ‘$where is not allowed in this context’ as in my nested query $where is used and inside it forEach is also used.
But the same query i can directly execute on Mongo DB and get results but giving error while trying to get through mongo java driver.

Exception in thread "main" com.mongodb.MongoCommandException: Command failed with error 2 (BadValue): '$where is not allowed in this context' on server localhost:53011. The full response is {"ok": 0.0, "errmsg": "$where is not allowed in this context", "code": 2, "codeName": "BadValue"}
	at com.mongodb.internal.connection.ProtocolHelper.getCommandFailureException(ProtocolHelper.java:205)
	at com.mongodb.internal.connection.InternalStreamConnection.receiveCommandMessageResponse(InternalStreamConnection.java:448)
	at com.mongodb.internal.connection.InternalStreamConnection.sendAndReceive(InternalStreamConnection.java:370)
	at com.mongodb.internal.connection.UsageTrackingInternalConnection.sendAndReceive(UsageTrackingInternalConnection.java:114)
	at com.mongodb.internal.connection.DefaultConnectionPool$PooledConnection.sendAndReceive(DefaultConnectionPool.java:719)
	at com.mongodb.internal.connection.CommandProtocolImpl.execute(CommandProtocolImpl.java:76)
	at com.mongodb.internal.connection.DefaultServer$DefaultServerProtocolExecutor.execute(DefaultServer.java:203)
	at com.mongodb.internal.connection.DefaultServerConnection.executeProtocol(DefaultServerConnection.java:115)
	at com.mongodb.internal.connection.DefaultServerConnection.command(DefaultServerConnection.java:83)
	at com.mongodb.internal.connection.DefaultServerConnection.command(DefaultServerConnection.java:74)
	at com.mongodb.internal.connection.DefaultServer$OperationCountTrackingConnection.command(DefaultServer.java:287)
	at com.mongodb.internal.operation.CommandOperationHelper.createReadCommandAndExecute(CommandOperationHelper.java:245)
	at com.mongodb.internal.operation.CommandOperationHelper.lambda$executeRetryableRead$6(CommandOperationHelper.java:227)
	at com.mongodb.internal.operation.OperationHelper.lambda$withSourceAndConnection$0(OperationHelper.java:345)
	at com.mongodb.internal.operation.OperationHelper.withSuppliedResource(OperationHelper.java:370)
	at com.mongodb.internal.operation.OperationHelper.lambda$withSourceAndConnection$1(OperationHelper.java:344)
	at com.mongodb.internal.operation.OperationHelper.withSuppliedResource(OperationHelper.java:370)
	at com.mongodb.internal.operation.OperationHelper.withSourceAndConnection(OperationHelper.java:343)
	at com.mongodb.internal.operation.CommandOperationHelper.lambda$executeRetryableRead$7(CommandOperationHelper.java:225)
	at com.mongodb.internal.operation.CommandOperationHelper.lambda$decorateReadWithRetries$3(CommandOperationHelper.java:192)
	at com.mongodb.internal.async.function.RetryingSyncSupplier.get(RetryingSyncSupplier.java:67)
	at com.mongodb.internal.operation.CommandOperationHelper.executeRetryableRead(CommandOperationHelper.java:230)
	at com.mongodb.internal.operation.CommandOperationHelper.executeRetryableRead(CommandOperationHelper.java:212)
	at com.mongodb.internal.operation.AggregateOperationImpl.execute(AggregateOperationImpl.java:191)
	at com.mongodb.internal.operation.AggregateOperation.execute(AggregateOperation.java:152)
	at com.mongodb.internal.operation.CountDocumentsOperation.execute(CountDocumentsOperation.java:134)
	at com.mongodb.internal.operation.CountDocumentsOperation.execute(CountDocumentsOperation.java:41)
	at com.mongodb.client.internal.MongoClientDelegate$DelegateOperationExecutor.execute(MongoClientDelegate.java:153)
	at com.mongodb.client.internal.MongoCollectionImpl.executeCount(MongoCollectionImpl.java:224)
	at com.mongodb.client.internal.MongoCollectionImpl.countDocuments(MongoCollectionImpl.java:194)
	at com.mongodb.client.internal.MongoCollectionImpl.countDocuments(MongoCollectionImpl.java:189)

Any help here?

1 Like

Hi @Soumyashree_Jena and welcome in the MongoDB Community! :muscle:

$where is mostly deprecated now. You should consider using $expr instead.

Cheers,
Maxime.

Thanks @MaBeuLux88_xxx , i tried $expr and it worked. :slight_smile:

1 Like