MongoDB डॉक्यूमेंट क्लास को JSON में बदलें!

MongoDB डॉक्यूमेंट क्लास डालें और उदाहरण JSON बनाएँ! Spring Data MongoDB @Document समर्थित। API डॉक्यूमेंटेशन और टेस्ट request body (sample JSON) के लिए उपयोगी।
Spring Data MongoDB
परिणाम
 

MongoDB डॉक्यूमेंट क्लास को JSON में बदलें!

इसे Spring Data MongoDB @Document class और उसकी embedded classes व records दें, और यह उस JSON का उदाहरण बनाता है जो controller वह document भेजते समय लौटाता है। आकार Spring Boot के default Jackson serialization का पालन करता है, इसलिए यह API response बताता है, MongoDB में stored BSON नहीं।

यह embedded lists, Map fields और Instant timestamps से भरे MongoDB documents पर समय बचाता है, जहां हाथ से response sample लिखना थकाऊ है, और storage format व response format के बीच का अंतर दिखाता है।

उपयोग कैसे करें

  1. @Document class और उसकी embedded classes या records को Spring Data MongoDB बॉक्स में पेस्ट करें।
  2. जिस document class को कोई दूसरी class reference नहीं करती वह आउटपुट का root बनती है।
  3. Docs या tests में JSON इस्तेमाल करने से पहले id और date placeholders को असली जैसी values से बदलें।

उदाहरण

इनपुट (Java · Spring Data MongoDB)
@Document(collection = "devices")
public class Device {
    @Id
    private String id;
    private String serial;
    @Field("fw")
    private String firmwareVersion;
    private Map<String, Double> lastReadings;
    private List<Alert> alerts;
    private Instant registeredAt;
}

record Alert(String level, String message, Instant raisedAt) {}
आउटपुट (JSON)
{
  "id": "",
  "serial": "",
  "firmwareVersion": "",
  "lastReadings": {
    "key": 0
  },
  "alerts": [
    {
      "level": "",
      "message": "",
      "raisedAt": "1970-01-01T00:00:00Z"
    }
  ],
  "registeredAt": "1970-01-01T00:00:00Z"
}

List<Alert> का Alert एक record है, इसलिए उसके components level, message और raisedAt fields बनते हैं। Map<String, Double> {"key": 0} बनता है, और Instant Z पर खत्म होने वाली UTC string है। @Field("fw") सिर्फ़ MongoDB में stored field rename करता है, इसलिए JSON key firmwareVersion ही रहती है।

समर्थित सुविधाएँ

  • record Alert(String level, ...) जैसे Java record components fields के रूप में पढ़े जाते हैं।
  • List और Set एक element वाले arrays बनते हैं; Map<String, T> {"key": value} बनता है।
  • Instant, ZonedDateTime और OffsetDateTime 1970-01-01T00:00:00Z देते हैं, जबकि LocalDateTime बिना offset वाली string देता है।
  • ObjectId type वाले fields को sample hex ObjectId string मिलती है।
  • @DBRef से जुड़ी classes इनपुट का हिस्सा हों तो nested objects बनती हैं।
  • @JsonIgnore, @JsonProperty, @JsonIgnoreProperties और inherit हुए fields JPA पेज वाले नियमों का ही पालन करते हैं।

उपयोग के मामले

कई embedded documents वाले responses

MongoDB जिन संरचनाओं को एक document में समेटता है, जैसे review lists, option maps और history arrays, उनसे लंबे response examples बनते हैं। Classes पेस्ट करें और हर nested स्तर आपके लिए लिख दिया जाता है।

Stored और लौटाए गए आकारों की तुलना

@Field से छोटे नाम या _id से map हुए fields database keys को API keys से अलग बना देते हैं। आउटपुट API वाली तरफ़ दिखाता है, इसलिए आप दोनों को साथ-साथ रख सकते हैं।

नोट्स और सीमाएँ

  • @Field("..."), @Indexed और @TextIndexed जैसे mapping annotations JSON keys नहीं बदलते और प्रतिबिंबित नहीं होते।
  • @Id String id _id के रूप में store होता है लेकिन Jackson से id के रूप में निकलता है, और आउटपुट यही दिखाता है।
  • Spring Boot का default Jackson org.bson.types.ObjectId को hex string नहीं, बल्कि timestamp और date वाले object के रूप में लिखता है। यहां दिखाया string रूप भेजने के लिए ToStringSerializer register करें या String id इस्तेमाल करें।
  • इनपुट में न मौजूद classes, GeoJsonPoint जैसे Spring Data types समेत, null बनती हैं।

अक्सर पूछे जाने वाले प्रश्न

मेरा @Id ObjectId हो या String?

String के साथ Spring Data save करते समय उसे ObjectId में बदलता है और API साफ़ hex string लौटाता है। ObjectId type रखने का मतलब है responses के लिए अतिरिक्त Jackson configuration चाहिए।

क्या @DBRef fields सच में nested objects के रूप में लौटते हैं?

@DBRef पढ़ते समय (lazy हो तो पहली access पर) reference किया document resolve करता है, इसलिए responses में nested object होता है। सिर्फ़ ObjectId या String id store करने वाले manual references सिर्फ़ id लौटाते हैं।

खुले रूप वाला Map<String, Object> field कैसा दिखता है?

उसकी value type अज्ञात है, इसलिए वह {"key": null} बनता है। Docs में डालने से पहले उसे उस तरह की वैल्यू से बदलें जो आप असल में store करते हैं।