node.js - Create unique autoincrement field with mongoose -


स्कीमा को देखते हुए:

  var EventSchema = new schema ({id: {// ...}, नाम: {प्रकार: स्ट्रिंग},});   

मैं id अद्वितीय और स्वत: निर्माण करना चाहता हूं मैं समझने की कोशिश करता हूं लेकिन मुंह में सही ढंग से यह कैसे समझने की समस्या है

मेरा प्रश्न है : किसी भी प्लगइन्स का उपयोग किए बिना मोनोग्राफ में autoincrement फ़ील्ड को लागू करने का सही तरीका क्या है?

यहां मोनोजोज का उपयोग करके स्वत: वृद्धिशील फ़ील्ड कार्यान्वयन का एक अच्छा उदाहरण है:

  var CounterSchema = Schema ({_id: {type : स्ट्रिंग, आवश्यक: सत्य}, सीईसी: {प्रकार: नंबर, डिफ़ॉल्ट: 0}}); Var काउंटर = मोंगोइज़.मॉडेल ('काउंटर', काउंटरस्केमा); var entitySchema = mongoose.Schema ({testvalue: {प्रकार: स्ट्रिंग}}); इकाई एसचेमा.पीआर ('सहेजें', फ़ंक्शन (अगले) {var doc = this; counter.findByIdAndUpdate ({_ आईडी: 'इकाईआईडी'}, {$ inc: {seq: 1}}, फ़ंक्शन (त्रुटि, काउंटर) {if ( त्रुटि) वापस अगले (त्रुटि); doc.testvalue = counter.seq; अगला ();});});   

आपको सबसे पहले चरण 1 करना चाहिए।

Comments

Popular posts from this blog

Verilog Error: output or inout port "Q" must be connected to a structural net expression -

jasper reports - How to center align barcode using jasperreports and barcode4j -

c# - ASP.NET MVC - Attaching an entity of type 'MODELNAME' failed because another entity of the same type already has the same primary key value -