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.

$and with $all and $nin not working

See original GitHub issue

For 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):

public class Issue {

	@Autowired private TestRepository repository;

	public void testTags() throws Exception {
		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());

	protected static class TestConfiguration {
		public MongoTemplate mongoTemplate(MongoClient mongoClient) {
			return new MongoTemplate(mongoDbFactory(mongoClient));

		public MongoDbFactory mongoDbFactory(MongoClient mongoClient) {
			return new SimpleMongoDbFactory(mongoClient, "test");

		public MongoServer mongoServer() {
			MongoServer mongoServer = new MongoServer(new MemoryBackend());
			return mongoServer;

		public MongoClient mongoClient(MongoServer mongoServer) {
			return new MongoClient(new ServerAddress(mongoServer.getLocalAddress()));
public class TestEntity {
	@Id private String id;
	@Indexed private Set<String> tags = new HashSet<>();
	public TestEntity() {
	public TestEntity(String id, String... tags) { = 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
	 * @param nin
	 * @return a list of {@link TestEntity} that contains {@code $all} the specified tags but not the tags specified in {@code $nin}.
	public List<TestEntity> findByTags(Collection<String> all, Collection<String> nin);

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:6 (6 by maintainers)

github_iconTop GitHub Comments

bwaldvogelcommented, Dec 12, 2018

Fixes are released in version 1.10.0

markbiglercommented, Dec 9, 2018

However, I observe a slightly different behavior on a real MongoDB. It never seems to match for empty lists in $all expressions.

You are absolutely correct, $all:[] never matches an empty list. Sorry about that.

Can you retest?

All tests are green now. Thanks alot! 🎁

Read more comments on GitHub >

github_iconTop 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 >

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 Post

No results found

github_iconTop Related Hashnode Post

No results found