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

MongoDB कलेक्शन में कॉलम नाम कैसे बदलें?


MongoDB कलेक्शन में कॉलम नाम बदलना

कलेक्शन में कॉलम नाम बदलने के लिए, आप $rename ऑपरेटर का उपयोग कर सकते हैं। सिंटैक्स निम्नलिखित है:

db.yourCollectionName.update({}, {$rename: {'yourOldColumName': 'yourNewColumnName'}}, false, true);

उदाहरण: दस्तावेज़ों के साथ एक कलेक्शन बनाना

> db.renamingColumnNameDemo.insertOne({"StudentName":"Larry","Age":23}); 
{ "acknowledged" : true, "insertedId" : ObjectId("5c9ee2c6d628fa4220163b9a") } 
> db.renamingColumnNameDemo.insertOne({"StudentName":"Sam","Age":26}); 
{ "acknowledged" : true, "insertedId" : ObjectId("5c9ee2d0d628fa4220163b9b") } 
> db.renamingColumnNameDemo.insertOne({"StudentName":"Robert","Age":27}); 
{ "acknowledged" : true, "insertedId" : ObjectId("5c9ee2dbd628fa4220163b9c") }

find() मेथड से सभी दस्तावेज़ प्रदर्शित करना

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

यह निम्नलिखित आउटपुट देगा:


MongoDB कलेक्शन में कॉलम नाम बदलने का क्वेरी

> db.renamingColumnNameDemo.update({}, {$rename: {'Age': 'StudentAge'}}, false, true); 
WriteResult({ "nMatched" : 3, "nUpserted" : 0, "nModified" : 3 })

अब जांचें कि कॉलम Age का नाम "StudentAge" में बदल गया है

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

यह निम्नलिखित आउटपुट देगा:

            
  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: [ {