Skip to content
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

Map collection and fields for $lookup/$graphLookup aggregation stage against domain type #4379

Open
evilmilo opened this issue Apr 28, 2023 · 3 comments · May be fixed by #4406
Open

Map collection and fields for $lookup/$graphLookup aggregation stage against domain type #4379

evilmilo opened this issue Apr 28, 2023 · 3 comments · May be fixed by #4406
Assignees
Labels
type: enhancement A general enhancement

Comments

@evilmilo
Copy link

Working on a legacy code base, we've got lots of code that builds up aggregations including lookups, so we want to lookup the collection name for a class.

We've got an implementation that I'm guessing was at some point copied from mongo, but doesn't seem up to date and digging through code can assume it was copied from
BasicMongoPersistentEntity

	public BasicMongoPersistentEntity(TypeInformation<T> typeInformation) {

		super(typeInformation, MongoPersistentPropertyComparator.INSTANCE);

		Class<?> rawType = typeInformation.getType();
		String fallback = MongoCollectionUtils.getPreferredCollectionName(rawType);

		if (this.isAnnotationPresent(Document.class)) {
			Document document = this.getRequiredAnnotation(Document.class);

			this.collection = StringUtils.hasText(document.collection()) ? document.collection() : fallback;
			this.language = StringUtils.hasText(document.language()) ? document.language() : "";
			this.expression = detectExpression(document.collection());
			this.collation = document.collation();
			this.collationExpression = detectExpression(document.collation());
		} else {

			this.collection = fallback;
			this.language = "";
			this.expression = null;
			this.collation = null;
			this.collationExpression = null;
		}

Is there any reason this isn't exposed as a static method call so anyone can resolve class name to collection name consistently with how mongo does it?

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Apr 28, 2023
@odrotbohm
Copy link
Member

Can you elaborate at which points you need the collection name explicitly? Unless I overlook something (perfectly possible) all our APIs (aggregations included) usually – at least also – take a type parameter where they expose an explicit collection name.

@odrotbohm odrotbohm added status: waiting-for-feedback We need additional information before we can continue and removed status: waiting-for-triage An issue we've not yet triaged labels Apr 28, 2023
@evilmilo
Copy link
Author

We want to know what the name that is actually going to be used is, even if it didn't have a type parameter. We are required by our company to use Java API, and no literals, so have to build up things like:

Aggregation.lookup(
	getCollectionName(Project.class),
	Dashboard.FIELD_NAME_PROJECT_ID, 
	FIELD_NAME_ID_WITH_UNDERSCORE,
	FIELD_NAME_PROJECT
);

@spring-projects-issues spring-projects-issues added status: feedback-provided Feedback has been provided and removed status: waiting-for-feedback We need additional information before we can continue labels Apr 28, 2023
@christophstrobl
Copy link
Member

MongoOperations already exposes getCollectionName.
However, extending the Aggregation API to accept a type parameter for the foreign collection would in my opinion be a nice enhancement to enable something like:

lookup()
  .from(User.class)
  .localField("id")
  .foreignField("firstname")
  .pipeline(...

Having the type present would also allow to map the foreignField against it to capture any potential @Field annotations that customize the target field name.

@christophstrobl christophstrobl added type: enhancement A general enhancement and removed status: feedback-provided Feedback has been provided labels Jun 9, 2023
@mp911de mp911de changed the title Expose method that calculates collection name from class Map collection and fields for $lookup/$graphLookup aggregation stage against domain type Jun 14, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: enhancement A general enhancement
Projects
None yet
4 participants