Comments (4)
@piotrmucha thanks for reporting - I need to check if what happens in the case of an untyped aggregation. It's likely the error stems from there.
from spring-data-mongodb.
Thank you. I can confirm that replacing untyped aggregation with typed aggregation resolves the problem.
So changing from:
var aggregation = Aggregation.newAggregation(match);
to:
var aggregation = Aggregation.newAggregation(classInstance, match);
Makes everyting works.
from spring-data-mongodb.
thanks for checking @piotrmucha - we'll have a look to get it working with untyped ones as well.
from spring-data-mongodb.
I've encountered an issue similar to what @piotrmucha described, where the MatchOperation in an aggregation fails with certain _id formats. The test only passes when I modify the _id by adding a prefix.
Here's a concise summary of the problem:
I'm experienced an issue where a aggregation involving the MatchOperation fails when using certain _id formats. The test passes when I modify the _id by adding a prefix to it.
When attempting to match documents by _id using the MatchOperation, the operation fails if the _id is "66014bb53e3e9474cc0f39d2". However, modifying this ID by prefixing it with a character or a number (e.g., "A66014bb53e3e9474cc0f39d2") allows the test to pass. Both IDs are stored as strings in MongoDB.
@DataMongoTest
@Testcontainers
class TestIdBugRepositoryImplIntTest {
private static final String ENTITY_ID_1 = "66014bb53e3e9474cc0f39d2";
private static final String ENTITY_ID_2 = "166014bb53e3e9474cc0f39d2";
@Autowired
private MongoTemplate mongoTemplate;
@Container
@ServiceConnection
private static final MongoDBContainer mongoDbContainer = new MongoDBContainer("mongo:6.0");
@BeforeEach
void setUp() {
List<Entity> entities = new ArrayList<>();
entities.addAll(List.of(
new Entity(ENTITY_ID_1),
new Entity(ENTITY_ID_2)
));
mongoTemplate.insertAll(entities);
}
// Test if the find method works
@Test
void matchById_entity1() {
final Criteria byEntityId = new Criteria("_id").is(ENTITY_ID_1);
final MatchOperation matchStage = Aggregation.match(byEntityId);
Aggregation aggregation = Aggregation.newAggregation(matchStage);
List<Entity> entities = mongoTemplate.aggregate(aggregation, "testEntity", Entity.class).getMappedResults();
assertThat(entities, is(notNullValue()));
assertThat(entities, is(hasSize(1)));
assertThat(entities.get(0).get_id(), is(ENTITY_ID_1));
}
@Test
void matchById_entity2() {
final Criteria byEntityId = new Criteria("_id").is(ENTITY_ID_2);
final MatchOperation matchStage = Aggregation.match(byEntityId);
Aggregation aggregation = Aggregation.newAggregation(matchStage);
List<Entity> entities = mongoTemplate.aggregate(aggregation, "testEntity", Entity.class).getMappedResults();
assertThat(entities, is(notNullValue()));
assertThat(entities, is(hasSize(1)));
assertThat(entities.get(0).get_id(), is(ENTITY_ID_2));
}
@AfterEach
void tearDown() {
mongoTemplate.dropCollection(Entity.class);
}
@Document(collection = "testEntity")
public class Entity {
@MongoId
private String _id;
public Entity(String _id) {
this._id = _id;
}
public String get_id() {
return _id;
}
}
}
Using typed aggregations also fixed this case:
Aggregation aggregation = Aggregation.newAggregation(Entity.class, matchStage);
While this resolved the issue for me, I thought it was still worth mentioning here as it may highlight a deeper problem or help creating better test coverage.
from spring-data-mongodb.
Related Issues (20)
- Adding `SECONDARY_READS` meta flag to the query does not affect the `readPreference` HOT 2
- Add support for value expressions in repository query methods
- Upgrade to MongoDB 5.0.1 driver
- Upgrade to MongoDB 4.11.2 driver HOT 1
- Disable auditable fields on query basis HOT 3
- Allow creating time series collections with custom name and CollectionOptions derived from annotation HOT 2
- Criteria.getCriteriaObject invalid when using Criteria.expr HOT 4
- Add a debug information for sort fields when using findByQuery
- Release 4.1.12 (2023.0.12)
- Release 4.2.6 (2023.1.6)
- Release 4.3 GA (2024.0.0)
- Regex criteria is not mapped to mongo query
- Aggregation/Aggregation Operations - equals() function is missing
- Using aggregation parameters with @Aggregation HOT 3
- `MongoTemplate.indexOps(collectionName, type)` ignores collectionName when type is not `null` HOT 1
- Release 4.2.7 (2023.1.7)
- Release 4.3.1 (2024.0.1)
- Release 4.4 M1 (2024.1.0)
- Aggregations should be able to handle `Sort.unsorted()` HOT 5
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from spring-data-mongodb.