Mongodb में फ़ील्ड का नाम कैसे बदलें (3 उदाहरण)
MongoDB में फ़ील्ड का नाम बदलने के लिए आप निम्न विधियों का उपयोग कर सकते हैं:
विधि 1: फ़ील्ड का नाम बदलें
db.collection.updateMany({}, { $rename :{" oldField ":" newField "}}, false, true)
विधि 2: एकाधिक फ़ील्ड का नाम बदलें
db.collection.updateMany({}, { $rename :{" old1 ":" new1 ", " old2 ":" new2 "}}, false, true)
विधि 3: उपफ़ील्ड का नाम बदलें
db.collection.updateMany({}, { $rename :{" field.oldSub ":" field.newSub "}}, false, true)
ध्यान दें कि $rename फ़ंक्शन में गलत, सत्य का अर्थ है {upsert:false, multiple:true} ।
आपको अपने सभी दस्तावेज़ों में फ़ील्ड नाम अपडेट करने के लिए मल्टी:ट्रू की आवश्यकता है।
निम्नलिखित उदाहरण दिखाते हैं कि निम्नलिखित दस्तावेजों के साथ संग्रह टीम के साथ प्रत्येक विधि का उपयोग कैसे करें:
db.teams.insertOne({team: "Mavs", class: {conf: "Western", div: "A"}, points: 31 }) db.teams.insertOne({team: "Spurs", class: {conf: "Western", div: "A"}, points: 22 }) db.teams.insertOne({team: "Jazz", class: {conf: "Western", div: "B"}, points: 19 }) db.teams.insertOne({team: "Celtics", class: {conf: "Eastern", div: "C"}, points: 26 }) db.teams.insertOne({team: "Cavs", class: {conf: "Eastern", div: "D"}, points: 33 }) db.teams.insertOne({team: "Nets", class: {conf: "Eastern", div: "D"}, points: 38 })
उदाहरण 1: किसी फ़ील्ड का नाम बदलें
हम टीम फ़ील्ड का नाम बदलकर new_team करने के लिए निम्नलिखित कोड का उपयोग कर सकते हैं:
db.teams.updateMany({}, { $rename :{" team ":" new_team "}}, false, true)
दस्तावेज़ अब इस तरह दिखते हैं:
{ _id: ObjectId("62017ce6fd435937399d6b58"), class: { conf: 'Western', div: 'A' }, points: 31, new_team: 'Mavs' } { _id: ObjectId("62017ce6fd435937399d6b59"), class: { conf: 'Western', div: 'A' }, points: 22, new_team: 'Spurs' } { _id: ObjectId("62017ce6fd435937399d6b5a"), class: { conf: 'Western', div: 'B' }, points: 19, new_team: 'Jazz' } { _id: ObjectId("62017ce6fd435937399d6b5b"), class: { conf: 'Eastern', div: 'C' }, points: 26, new_team: 'Celtics' } { _id: ObjectId("62017ce6fd435937399d6b5c"), class: { conf: 'Eastern', div: 'D' }, points: 33, new_team: 'Cavs' } { _id: ObjectId("62017ce6fd435937399d6b5d"), class: { conf: 'Eastern', div: 'D' }, points: 38, new_team: 'Nets' }
ध्यान दें कि प्रत्येक दस्तावेज़ के लिए टीम फ़ील्ड का नाम बदलकर new_team कर दिया गया है।
उदाहरण 2: एकाधिक फ़ील्ड का नाम बदलें
हम टीम फ़ील्ड का नाम बदलकर new_team और पॉइंट फ़ील्ड का नाम new_points करने के लिए निम्नलिखित कोड का उपयोग कर सकते हैं:
db.teams.updateMany({}, { $rename :{" team ":" new_team ", " points ":" new_points "}}, false, true)
दस्तावेज़ अब इस तरह दिखते हैं:
{ _id: ObjectId("62017ce6fd435937399d6b58"), class: { conf: 'Western', div: 'A' }, new_team: 'Mavs', new_points: 31 } { _id: ObjectId("62017ce6fd435937399d6b59"), class: { conf: 'Western', div: 'A' }, new_team: 'Spurs', new_points: 22 } { _id: ObjectId("62017ce6fd435937399d6b5a"), class: { conf: 'Western', div: 'B' }, new_team: 'Jazz', new_points: 19 } { _id: ObjectId("62017ce6fd435937399d6b5b"), class: { conf: 'Eastern', div: 'C' }, new_team: 'Celtics', new_points: 26 } { _id: ObjectId("62017ce6fd435937399d6b5c"), class: { conf: 'Eastern', div: 'D' }, new_team: 'Cavs', new_points: 33 } { _id: ObjectId("62017ce6fd435937399d6b5d"), class: { conf: 'Eastern', div: 'D' }, new_team: 'Nets', new_points: 38 }
ध्यान दें कि प्रत्येक दस्तावेज़ में टीम फ़ील्ड और पॉइंट फ़ील्ड दोनों का नाम बदल दिया गया है।
उदाहरण 3: उपक्षेत्र का नाम बदलें
हम div क्लास फ़ील्ड में div सबफ़ील्ड का नाम बदलने के लिए निम्नलिखित कोड का उपयोग कर सकते हैं:
db.teams.updateMany({}, { $rename :{" class.div ":" class.division "}}, false, true)
दस्तावेज़ अब इस तरह दिखते हैं:
{ _id: ObjectId("62017e21fd435937399d6b5e"), team: 'Mavs', class: { conf: 'Western', division: 'A' }, points: 31 } { _id: ObjectId("62017e21fd435937399d6b5f"), team: 'Spurs', class: { conf: 'Western', division: 'A' }, points: 22 } { _id: ObjectId("62017e21fd435937399d6b60"), team: 'Jazz', class: { conf: 'Western', division: 'B' }, points: 19 } { _id: ObjectId("62017e21fd435937399d6b61"), team: 'Celtics', class: { conf: 'Eastern', division: 'C' }, points: 26 } { _id: ObjectId("62017e21fd435937399d6b62"), team: 'Cavs', class: { conf: 'Eastern', division: 'D' }, points: 33 } { _id: ObjectId("62017e21fd435937399d6b63"), team: 'Nets', class: { conf: 'Eastern', division: 'D' }, points: 38 }
ध्यान दें कि प्रत्येक दस्तावेज़ में क्लास फ़ील्ड में डिव सबफ़ील्ड का नाम बदलकर डिवीज़न कर दिया गया है।
नोट : आप $rename फ़ंक्शन के लिए संपूर्ण दस्तावेज़ यहां पा सकते हैं।
अतिरिक्त संसाधन
निम्नलिखित ट्यूटोरियल बताते हैं कि MongoDB में अन्य सामान्य ऑपरेशन कैसे करें:
MongoDB: एक नया फ़ील्ड कैसे जोड़ें
MongoDB: किसी फ़ील्ड को कैसे हटाएं
MongoDB: किसी फ़ील्ड में विशिष्ट मानों की गणना कैसे करें