2012-08-17 12 views
6

स्प्रिंग डेटा और मोंगोडीबी के साथ पहले प्रयोग बहुत अच्छे थे। अब मुझे निम्नलिखित संरचना मिली है (सरलीकृत):स्प्रिंग डेटा मोंगोडीबी: उप दस्तावेजों तक पहुंच और अद्यतन

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

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

संपूर्ण पत्र वस्तुओं/दस्तावेज़ों को लोड और सहेजना एक आकर्षण की तरह काम करता है। (मैं ObjectId का उपयोग Section.id क्षेत्र के लिए अद्वितीय आईडी जेनरेट करने।)

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

दस्तावेजों के रूप में बड़ा (200K) और कभी कभी केवल उप-भागों आवेदन के द्वारा की आवश्यकता नहीं है: वहाँ एक के लिए क्वेरी करने के लिए एक संभावना है सब-दस्तावेज़ (सेक्शन), संशोधित करें और इसे सेव करें? मैं

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

की तरह और जैसे पाठ्यक्रम के तरीकों की एक विधि को लागू करना चाहते हैं:

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

मुझे लगता है कि पिछले तीन तरीकों को कुछ हद तक "अजीब" कर रहे हैं, यानी पूरे दस्तावेज लोड हो रहा है, को संशोधित संग्रह और इसे फिर से सहेजना किसी वस्तु-उन्मुख दृष्टिकोण से बेहतर दृष्टिकोण हो सकता है; लेकिन पहला उपयोग केस (उप-दस्तावेज़/उप-ऑब्जेक्ट में "नेविगेट करना" और इस ऑब्जेक्ट के दायरे में काम करना) प्राकृतिक लगता है।

मुझे लगता है कि मोंगोडीबी उप-दस्तावेज अपडेट कर सकता है, लेकिन क्या ऑब्जेक्ट मैपिंग के लिए स्प्रिंगडाटा का उपयोग किया जा सकता है? किसी भी पॉइंटर्स के लिए धन्यवाद।

उत्तर

12

मैंने स्लाइसिंग और केवल एक सबोबजेक्ट लोड करने के लिए निम्न दृष्टिकोण का पता लगाया। क्या यह ठीक लगता है? मैं समवर्ती संशोधन के साथ समस्याओं से अवगत हूं।

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); 

यह सभी वर्गों को लोड करने का एक वैकल्पिक समाधान है, लेकिन अन्य (बड़े) फ़ील्ड को छोड़ रहा है।

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); 

यह कैसे स्प्रिंग डाटा MongoDB से "आंशिक परिणाम" के साथ इस्तेमाल किया जा सकता है देखने के लिए अच्छा है:

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); 

इस कोड मैं केवल एक ही संग्रह तत्व संग्रहीत करने के लिए है।

कोई भी टिप्पणी अत्यधिक सराहना की!

+0

क्या आप अपना findSectionById() विधि कार्यान्वयन साझा कर सकते हैं? –

+0

मुझे इसे साझा करने में खुशी है, यह सीधा है, हालांकि - यह सिर्फ पत्र के अनुभागों पर लूप करता है और आईडी फ़ील्ड की तुलना करता है। इस समय यहां सही कोड नहीं है। –

संबंधित मुद्दे