Skip to content

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

Open
@ricardohsmello

Description

@ricardohsmello

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!

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions