Skip to content

Embedded sharding keys are not correctly picked up from the shardKeySource Document #3590

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
christian-german opened this issue Mar 11, 2021 · 3 comments
Labels
type: bug A general bug

Comments

@christian-german
Copy link

The annotation @Sharded(shardKey = {...}) add some filters to the request performed upon MongoDB when updating a document (upsert).

If we use composite keys like "location.locationId", the value from the Document we store is not retrieved, due to the Document not being "flatted".
This is done in the applyShardKey method, in the UpdateContext inner class within the QueryOperations class:
getMappedShardKeyFields(domainType).forEach(key -> filterWithShardKey.putIfAbsent(key, shardKeySource.get(key)));

See this question on SO.

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Mar 11, 2021
@christian-german
Copy link
Author

I would be happy to help on this one ;)

@christophstrobl
Copy link
Member

@christian-german thanks! If you like please have a look at/comment/test #3591.

@christian-german
Copy link
Author

We have tested our application against the 3.2.0-GH-3590-SNAPSHOT version and everything went fine.

Meanwhile, we have implemented a workaround we will use while waiting for the official release.

From our repository implementation:

@Override
	public void saveWithShardKey(DataPayload dataPayload) {

		// If new datapayload
		if(dataPayload.getId() == null) {
			mongoTemplate.insert(dataPayload);

		} else { // If update existing datapayload

			// Criteria according to datapayload shardkey
			Criteria criteria = where("_id").is(new ObjectId(dataPayload.getId()))
					.and("location.locationId").is(dataPayload.getLocation().getLocationId())
					.and("ts").is(dataPayload.getTs());
			Query query = query(criteria);

			// Convert object to document
			Document document = new Document();
			mongoTemplate.getConverter().write(dataPayload, document);
			Update update = Update.fromDocument(document);

			mongoTemplate.upsert(query, update, "dataPayload");
		}
	}

@mp911de mp911de added type: bug A general bug and removed status: waiting-for-triage An issue we've not yet triaged labels Mar 15, 2021
@mp911de mp911de added this to the 3.0.8 (Neumann SR8) milestone Mar 15, 2021
mp911de pushed a commit that referenced this issue Mar 15, 2021
This commit fixes the lookup of shard key values for nested paths using the dot (.) notation.

Closes: #3590
Original pull request: #3591.
mp911de added a commit that referenced this issue Mar 15, 2021
Move hasValue(…) from DocumentAccessor to BsonUtils. Fix typo in tests.

See: #3590
Original pull request: #3591.
mp911de added a commit that referenced this issue Mar 15, 2021
Move hasValue(…) from DocumentAccessor to BsonUtils. Fix typo in tests.

See: #3590
Original pull request: #3591.
mp911de pushed a commit that referenced this issue Mar 15, 2021
This commit fixes the lookup of shard key values for nested paths using the dot (.) notation.

Closes: #3590
Original pull request: #3591.
mp911de added a commit that referenced this issue Mar 15, 2021
Move hasValue(…) from DocumentAccessor to BsonUtils. Fix typo in tests.

See: #3590
Original pull request: #3591.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: bug A general bug
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants