$and with $all and $nin not working
See original GitHub issueFor the known reasons I have tried to switch from fongo
to mongo-java-server
.
Except for the following case, all tests ran correctly. Here the demo test class to reproduce the problem (find all entities that have a tag “A”, but not a tag “B” or “C” -> expect no entities at all):
@RunWith(SpringRunner.class)
@SpringBootTest(classes={Issue.TestConfiguration.class})
public class Issue {
@Autowired private TestRepository repository;
@Test
public void testTags() throws Exception {
repository.deleteAll();
repository.insert(new TestEntity("ID_1", "A", "B"));
repository.insert(new TestEntity("ID_2", "A", "C"));
List<TestEntity> entitiesByTags = repository.findByTags(Arrays.asList("A"), Arrays.asList("B", "C"));
assertEquals(0, entitiesByTags.size());
}
@Configuration
@EnableMongoRepositories(basePackageClasses={TestRepository.class})
protected static class TestConfiguration {
@Bean
public MongoTemplate mongoTemplate(MongoClient mongoClient) {
return new MongoTemplate(mongoDbFactory(mongoClient));
}
@Bean
public MongoDbFactory mongoDbFactory(MongoClient mongoClient) {
return new SimpleMongoDbFactory(mongoClient, "test");
}
@Bean(destroyMethod="shutdown")
public MongoServer mongoServer() {
MongoServer mongoServer = new MongoServer(new MemoryBackend());
mongoServer.bind();
return mongoServer;
}
@Bean(destroyMethod="close")
public MongoClient mongoClient(MongoServer mongoServer) {
return new MongoClient(new ServerAddress(mongoServer.getLocalAddress()));
}
}
}
@Document(collection="test")
public class TestEntity {
@Id private String id;
@Indexed private Set<String> tags = new HashSet<>();
public TestEntity() {
}
public TestEntity(String id, String... tags) {
this.id = id;
this.tags = new HashSet<>(Arrays.asList(tags));
}
public String getId() {
return id;
}
public Set<String> getTags() {
return tags;
}
}
public interface TestRepository extends MongoRepository<TestEntity, String> {
/**
* @param all https://docs.mongodb.com/manual/reference/operator/query/all/
* @param nin https://docs.mongodb.com/manual/reference/operator/query/nin/
* @return a list of {@link TestEntity} that contains {@code $all} the specified tags but not the tags specified in {@code $nin}.
*/
@Query("{$and:[{'tags':{$all:?0}},{'tags':{$nin:?1}}]}")
public List<TestEntity> findByTags(Collection<String> all, Collection<String> nin);
}
Issue Analytics
- State:
- Created 5 years ago
- Comments:6 (6 by maintainers)
Top Results From Across the Web
mongodb $nin on ObjectId not working - Stack Overflow
'products' is the collection I'm trying to update. Instead of updating fields that are not in 'items', it updates all documents. What am...
Read more >$nin is not working as expected, - Working with Data - MongoDB
Hi I'm new to MongoDB and I'm facing an issue with the deleteMany method by using $nin. Irrespective of using $nin, the method...
Read more >MongoDB $nin: How to Use the Not In Operator Effectively
This tutorial explains to you the MongoDB Not In operator ($nin) and shows you how to apply it effectively.
Read more >MongoDB: $nin and $in not working together in $elemMatch ...
MongoDB: $nin and $in not working together in $elemMatch to fetch documents having subjects “MongoDB”, but not “Java” - For such kind of ......
Read more >How to use $in and $nin operators in MongoDB - Linux Hint
The $in (pronounced as “in”) and $nin (Not IN) operators in MongoDB belong to ... After that, you can get the display of...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Fixes are released in version
1.10.0
You are absolutely correct,
$all:[]
never matches an empty list. Sorry about that.All tests are green now. Thanks alot! 🎁