Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8433177
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T06:18:57+00:00 2026-06-10T06:18:57+00:00

First experiments with Spring Data and MongoDB were great. Now I’ve got the following

  • 0

First experiments with Spring Data and MongoDB were great. Now I’ve got the following structure (simplified):

public class Letter {
  @Id
  private String id;
  private List<Section> sections;
}

public class Section {
  private String id;
  private String content;
}

Loading and saving entire Letter objects/documents works like a charm. (I use ObjectId to generate unique IDs for the Section.id field.)

Letter letter1 = mongoTemplate.findById(id, Letter.class)
mongoTemplate.insert(letter2);
mongoTemplate.save(letter3);

As documents are big (200K) and sometimes only sub-parts are needed by the application: Is there a possibility to query for a sub-document (section), modify and save it?
I’d like to implement a method like

Section s = findLetterSection(letterId, sectionId);
s.setText("blubb");
replaceLetterSection(letterId, sectionId, s);

And of course methods like:

addLetterSection(letterId, s); // add after last section
insertLetterSection(letterId, sectionId, s); // insert before given section
deleteLetterSection(letterId, sectionId); // delete given section

I see that the last three methods are somewhat “strange”, i.e. loading the entire document, modifying the collection and saving it again may be the better approach from an object-oriented point of view; but the first use case (“navigating” to a sub-document/sub-object and working in the scope of this object) seems natural.

I think MongoDB can update sub-documents, but can SpringData be used for object mapping? Thanks for any pointers.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T06:18:58+00:00Added an answer on June 10, 2026 at 6:18 am

    I figured out the following approach for slicing and loading only one subobject. Does it seem ok? I am aware of problems with concurrent modifications.

    Query query1 = Query.query(Criteria.where("_id").is(instance));
    query1.fields().include("sections._id");
    LetterInstance letter1 = mongoTemplate.findOne(query1, LetterInstance.class); 
    LetterSection emptySection = letter1.findSectionById(sectionId);
    int index = letter1.getSections().indexOf(emptySection);
    
    Query query2 = Query.query(Criteria.where("_id").is(instance));
    query2.fields().include("sections").slice("sections", index, 1);
    LetterInstance letter2 = mongoTemplate.findOne(query2, LetterInstance.class);
    LetterSection section = letter2.getSections().get(0);
    

    This is an alternative solution loading all sections, but omitting the other (large) fields.

    Query query = Query.query(Criteria.where("_id").is(instance));
    query.fields().include("sections");
    LetterInstance letter = mongoTemplate.findOne(query, LetterInstance.class); 
    LetterSection section = letter.findSectionById(sectionId);
    

    This is the code I use for storing only a single collection element:

    MongoConverter converter = mongoTemplate.getConverter();
    DBObject newSectionRec = (DBObject)converter.convertToMongoType(newSection);
    
    Query query = Query.query(Criteria.where("_id").is(instance).and("sections._id").is(new ObjectId(newSection.getSectionId())));
    Update update = new Update().set("sections.$", newSectionRec);
    mongoTemplate.updateFirst(query, update, LetterInstance.class);
    

    It is nice to see how Spring Data can be used with “partial results” from MongoDB.

    Any comments highly appreciated!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a simple data structure (the properties are of course Dependency Properties): class
After some experiments I ended up with the following code to perform Search and
I'm doing my first experiments with Grails and am looking for a way to
I've been doing some experiments and here's something I found. Consider the following C
As part of my first experiments with C# (on Mono 2.6.7) , I am
First of all, this isn't for a keylogger, it's for an input in a
first take a look on this picture from localScope app : i have 2
first of all some details: I configured security as below in web.xml view plaincopy
first off I'm a noob to PHP but here is my problem. I am
First off, I'm coming (back) to Java from C#, so apologies if my terminology

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.