question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Mongo isNull check and null values

See original GitHub issue

Hello,

I am using Spring Boot with QueryDSL & MongoDB using querydsl-mongodb version 3.7.4. I have a MongoRepository and am using a QueryDSL Predicate to check if a field contains a null value like:

repository.findAll(QPerson.person.personId.isNull())

However, this isn’t retrieving results where the value of the field is null as expected.

Looking at the code for MongodbSerializer, it appears the isNull operation is translated to an $exists check in MongoDB which checks if the property exists. Instead, I need to check if the value of the field is null, which was what I was expecting isNull to do.

Is there a way to check if a field (that exists) has a null value using QueryDSL Predicates & MongoDB?

Thank you!

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:7
  • Comments:9 (2 by maintainers)

github_iconTop GitHub Comments

4reactions
butzy92commented, May 15, 2020

Found a workaround: `public class NullConstant implements Constant<Object> {

public static final NullConstant NULL_CONSTANT = new NullConstant();

private NullConstant() {
}

@Override
public Object getConstant() {
	return null;
}

@Nullable
@Override
public Object accept(Visitor v, @Nullable Object context) {
	return null;
}

@Override
public Class getType() {
	return Object.class;
}

}`

and use it: qEntity.field.eq(NullConstant.NULL_CONSTANT)

0reactions
stale[bot]commented, Jun 3, 2021

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Query for Null or Missing Fields — MongoDB Manual
The { item : null } query matches documents that either contain the item field whose value is null or that do not...
Read more >
MongoDB: How to query for records where field is null or not ...
Type Check · 1. If you want to pick records where sent_at exists and has the value null · 2. If you want...
Read more >
MongoDB query with null - w3resource
Description. In this page, we are going to discuss how null value in mongodb documents can be fetched using $type, $exists operators.
Read more >
How to Query For Is Not Null in MongoDB? - CodeForGeek
To query for is not null value, we can use the $ne operator as well as the $eq operator and specify the desired...
Read more >
How do I check whether a field contains null value in MongoDB?
How do I check whether a field contains null value in MongoDB? - You can use $type operator to check whether a filed...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found