Skip to content

Using .in("dbName") with $out causes invalid output stage — works fine without .in() #4969

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

Open
ricardohsmello opened this issue May 13, 2025 · 0 comments
Labels
status: waiting-for-triage An issue we've not yet triaged

Comments

@ricardohsmello
Copy link

Hi team,

I'm using Spring Boot 3.4.4 with Spring Data MongoDB 4.4.4, and I noticed an issue when using the $out aggregation stage together with .in("new_db"). The aggregation works perfectly if I don't include .in(), meaning it defaults to writing in the current database.

Reproduction

void exportBooksWithPublishedYear() {
    Aggregation aggregation = Aggregation.newAggregation(
        Aggregation.project("title", "publishedAt")
                   .andExpression("year(publishedAt)").as("year"),

        Aggregation.out("books_year_export")
                   .in("new_db")  // <--  Only breaks when this is included
                   .mode(OutOperation.OutMode.INSERT)
    );

    AggregationOptions options = AggregationOptions.builder()
        .skipOutput()  // <-- I also tried skipOutput but didn't work
        .build();

    mongoTemplate.aggregate(
        aggregation.withOptions(options),
        Book.BOOK_COLLECTION_NAME,
        Document.class
    );
}

Running on MongoDB Atlas 8.0.9 (Cloud) gives:

com.mongodb.MongoCommandException: Command failed with error 8000 (AtlasError): '$out must contain both db and coll'

{
  "ok": 0,
  "errmsg": "Failed to retrieve database or collection name from $out, err=$out must contain both db and coll",
  "code": 8000,
  "codeName": "AtlasError"
}

Running on MongoDB AtlasLocalDev 8.0.5 (Docker)

Using the same code on the [MongoDB Atlas Local Docker image] gives:

com.mongodb.MongoCommandException: Command failed with error 40415 (IDLUnknownField): 'BSON field '$out.to' is an unknown field.'

{
  "ok": 0.0,
  "errmsg": "BSON field '$out.to' is an unknown field.",
  "code": 40415,
  "codeName": "IDLUnknownField"
}

Environment

  • Spring Boot: 3.4.4
  • Spring Data MongoDB: 4.4.4
  • MongoDB Java Driver: 5.2.1
  • Java: 21

I’m not sure if the $out stage is being constructed correctly under the hood when using .in("dbName"), but it seems to be missing expected fields for the newer MongoDB 8.x versions.

Please let me know if I can help with a sample project or further info. Thanks a lot!

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label May 13, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: waiting-for-triage An issue we've not yet triaged
Projects
None yet
Development

No branches or pull requests

2 participants