Computer >> कंप्यूटर ट्यूटोरियल >  >> प्रोग्रामिंग >> MongoDB

MongoDB में यूनिक इंडेक्स सेट करें

MongoDB में यूनिक इंडेक्स बनाना

यूनिक इंडेक्स यह सुनिश्चित करता है कि इंडेक्स किए गए फ़ील्ड के लिए कोई दो दस्तावेज़ समान मान न रखें। unique: true विकल्प के साथ इंडेक्स बनाएं।

यूनिक इंडेक्स बनाना

> db.demo257.ensureIndex({Name:1}, {unique:true});
{ "createdCollectionAutomatically" : true, "numIndexesBefore" : 1, "numIndexesAfter" : 2, "ok" : 1 }

नोट: ensureIndex deprecated है

आधुनिक MongoDB (3.0+) में createIndex() उपयोग करें:

> db.demo257.createIndex({ Name: 1 }, { unique: true });

दस्तावेज़ डालना (सफल)

> db.demo257.insertOne({Name:"Chris"});
{ "acknowledged" : true, "insertedId" : ObjectId("5e47a5e51627c0c63e7dba91") }
> db.demo257.insertOne({Name:"Bob"});
{ "acknowledged" : true, "insertedId" : ObjectId("5e47a5e81627c0c63e7dba92") }

डुप्लिकेट डालने का प्रयास (त्रुटि)

> db.demo257.insertOne({Name:"Chris"});
2020-02-15T13:33:54.064+0530 E QUERY [js] WriteError: E11000 duplicate key error collection: test.demo257 index: Name_1 dup key: { : "Chris" }
WriteError({ "index" : 0, "code" : 11000, "errmsg" : "E11000 duplicate key error collection: test.demo257 index: Name_1 dup key: { : \"Chris\" }", "op" : { "_id" : ObjectId("5e47a5ea1627c0c63e7dba93"), "Name" : "Chris" } })

दस्तावेज़ देखना (डुप्लिकेट अस्वीकृत)

> db.demo257.find();

आउटपुट (केवल दो दस्तावेज़):


यूनिक इंडेक्स के महत्वपूर्ण पहलू

  • डुप्लिकेट रोकथाम: इंसर्ट/अपडेट पर डुप्लिकेट मान अस्वीकृत करता है
  • Null मान: डिफ़ॉल्ट रूप से, यूनिक इंडेक्स एक null मान की अनुमति देता है (यदि फ़ील्ड अनुपस्थित है)
  • Partial Filter Expression (MongoDB 3.2+): केवल विशिष्ट शर्तों को पूरा करने वाले दस्तावेज़ों के लिए यूनिकनेस लागू करें
  • Sparse + Unique: { unique: true, sparse: true } - null/अनुपस्थित फ़ील्ड्स को अनुमति देता है लेकिन मौजूद मानों के लिए यूनिकनेस लागू करता है

स्पार्स यूनिक इंडेक्स उदाहरण

> db.demo.createIndex({ "firstName": 1, "lastName": 1 }, { unique: true });
// firstName + lastName का संयोजन यूनिक होना चाहिए
  1. - MongoDB $in में रेगुलर एक्सप्रेशन कैसे सेट करें?

    आइए दस्तावेज़ों के साथ एक संग्रह बनाएं: > db.demo675.insertOne({ ListOfNames:[John,Chris,David]}); { acknowledged : true, insertedId : ObjectId(5ea3f5b404263e90dac943ef) } > db.demo675.insertOne({ ListOfNames:[Bob,Johnson,Sam]}); { acknowledged : true, insertedId : ObjectId(5ea3f5c8042

  1. - MongoDB में Nested Document में Condition Set करना

    मान लें हमें specific value से greater value वाले document find करना है। इसके लिए nested document में dot notation का उपयोग करें और $gt से condition set करें। Collection बनाना > db.demo688.insert( ... { ... information:{id:1,details:[ ... {otherDetails:{ ... values:75 ... }} ...

  1. - MongoDB कलेक्शन में इंडेक्स बनाएं

    MongoDB में इंडेक्स बनाने के लिए createIndex() का उपयोग करें। आइए दस्तावेजों के साथ एक कलेक्शन बनाते हैं: > db.demo702.createIndex({details.id:1}); { createdCollectionAutomatically : true, numIndexesBefore : 1, numIndexesAfter : 2, ok : 1 } > db.demo702.insertOne({ details: [ {