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

MongoDB में संग्रह का नाम कैसे बदलें?


renameCollection() मेथड का उपयोग करना

किसी मौजूदा संग्रह का नाम बदलने के लिए संग्रह ऑब्जेक्ट पर renameCollection() मेथड का उपयोग करें।

सिंटैक्स

db.yourOldCollectionName.renameCollection("yourNewCollectionName");

उदाहरण: संग्रह बनाना और नाम बदलना

> db.savingInformationDemo.insertOne({"StudentName":"Larry"}); 
{ "acknowledged" : true, "insertedId" : ObjectId("5c9cb44da629b87623db1b07") }
> db.savingInformationDemo.insertOne({"StudentName":"John"}); 
{ "acknowledged" : true, "insertedId" : ObjectId("5c9cb45da629b87623db1b08") }
> db.savingInformationDemo.insertOne({"StudentName":"Mike"}); 
{ "acknowledged" : true, "insertedId" : ObjectId("5c9cb461a629b87623db1b09") }
> db.savingInformationDemo.insertOne({"StudentName":"Sam"}); 
{ "acknowledged" : true, "insertedId" : ObjectId("5c9cb465a629b87623db1b0a") }

नाम बदलने से पहले देखना

> db.savingInformationDemo.find().pretty();

नाम बदलना

> db.savingInformationDemo.renameCollection("saveStudentInformation");

आउटपुट: { "ok" : 1 }

नए नाम से सत्यापन

> db.saveStudentInformation.find();

आउटपुट: वही दस्तावेज़, नए संग्रह नाम saveStudentInformation के तहत।

महत्वपूर्ण नोट्स:

  • नाम बदलना **परमाणु** ऑपरेशन है (MongoDB 4.0+ में)।
  • <लक्ष्य संग्रह नाम पहले से मौजूद नहीं होना चाहिए (अन्यथा त्रुटि)।
  • संग्रह के इंडेक्स नए नाम के साथ बने रहते हैं।
  • शार्डेड क्लस्टर में, नाम बदलने के प्रतिबंध होते हैं (उदाहरण के लिए, शार्ड कुंजी नहीं बदल सकते)।
  • वैकल्पिक रूप से, db.runCommand({ renameCollection: "oldName", to: "newName" }) का उपयोग करें।
  1. - MongoDB Collection Query - Find() में Some Fields Exclude करना

    जिन fields को include नहीं करना उन्हें 0 set करें। यहाँ हमने fields yourFieldName1 और yourFieldName2 को 0 set किया: db.yourCollectionName.find(yourQuery, {yourFieldName1:0,yourFieldName2:0}); Collection बनाना > db.demo567.insertOne({Name:Chris,Age:21}); { acknowledged : true, insertedId : Object

  1. - MongoDB Query - Collection Rename करना

    MongoDB में collection rename करने के लिए renameCollection() का उपयोग करें। Collection बनाना > db.demo690.insertOne({_id:101,Name:Sam}); { acknowledged : true, insertedId : 101 } > db.demo690.insertOne({_id:102,Name:Mike}); { acknowledged : true, insertedId : 102 } > db.demo690.insertOne({

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

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