Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> ฐานข้อมูล

ดึงชื่อ Field ทั้งหมดจาก MongoDB Collection อย่างมีประสิทธิภาพ

เผยแพร่ครั้งแรกเมื่อวันที่ 18 มกราคม 2019 ที่ ObjectRocket.com/blog

เพื่อ Validate Schema, Debug Typos ใน Fields หรือหา Fields ที่คุณไม่ควรตั้งค่า คุณต้องการความเข้าใจเกี่ยวกับ Keys ทั้งหมดใน MongoDB® Collection ของคุณ

ดึงชื่อ Field ทั้งหมดจาก MongoDB Collection อย่างมีประสิทธิภาพ

บริษัท MongoDB-as-a-Service หลายแห่ง รวมถึง ObjectRocket ให้วิธีง่ายๆ ในการทำสิ่งนี้ได้ใน User Interface (UI) ผู้ใช้ MongoDB มืออาชีพมักเริ่มต้นด้วย Object-Document Mapper (ODM) เช่น Mongoose สำหรับ JavaScript® หรือ Mongoengine สำหรับ Python® เพื่อที่พวกเขาจะสร้าง Schema ที่สอดคล้องกันสำหรับแอปพลิเคชันและลด Typos ODMs ยังทำ Type Validation ด้วย ดังนั้นคุณจึงไม่ได้วาง String ใน Field ที่มี Integers และมีการคำนวณคณิตศาสตร์นำไปใช้

หากคุณไม่มี Service หรือ ODM คุณสามารถใช้วิธีอื่นๆ หลายวิธีเพื่อเข้าถึง Keys เราพบวิธีต่อไปนี้เพื่อรับ Keys ทั้งหมดใน MongoDB Collection สำหรับสถานการณ์ต่างๆ

ค้นหา Keys ของ MongoDB

สมมติว่าคุณมีตัวอย่างต่อไปนี้:

db.activities.insert( { type : ['indoor', 'outdoor' , 'mixed'] } ); db.activities.insert( { activity : 'cycling' } ); db.activities.insert( { activity : 'skiing', location: 'Alpes', } ); db.activities.insert( { equipment : ['paddles','sunglasses'] } ); 

คุณต้องการรับ Unique Keys: type, activity, equipment, และ location

MapReduce

คุณสามารถรับ Keys ด้วย MapReduce โดยใช้ Operation ต่อไปนี้:

mr = db.runCommand({ "mapreduce" : "activities", "map" : function() { for (var key in this) { emit(key, null); } }, "reduce" : function(key, stuff) { return null; }, "out": "activities" + "_keys" }) 

หลังจาก Operation เสร็จสิ้น ให้รันคำสั่ง distinct ต่อไปนี้บน Collection ที่ได้ เพื่อหา Keys ทั้งหมด:

 db.activities_keys.distinct("_id") 

หากคุณต้องการรับรายการ Keys ทั้งหมดที่ Unique ใน Subdocument ให้แก้ไขบรรทัดต่อไปนี้:

for (var key in this.first_level.second_level.nth_level) { emit(key, null); } 

Aggregation

คุณยังสามารถใช้ Aggregation ได้ วิธีนี้ทำงานกับ Drivers ทั้งหมดที่รองรับ Aggregate Framework ด้วย Aggregations ของคุณ ให้ใช้ $sample หรือ $limit เพื่อลด Overhead

หมายเหตุ: การรันด้วย Primary Read Preference อาจส่งผลต่อ Performance พิจารณาการรันด้วย Secondary Read Preference

ใช้ Aggregation กับ $objectToArrray (มีใน Version 3.4.4 ขึ้นไป) เพื่อแปลง Key-Value Pairs ทั้งหมดระดับบนสุดเป็น Document Arrays ตามด้วย $unwind และ $group กับ $addToSet เพื่อรับ Distinct Keys ทั่ว Collection ทั้งหมด

ในตัวอย่างต่อไปนี้ $$ROOT อ้างอิง Document ระดับบนสุด

db.activities.aggregate([ {"$project":{"arrayofkeyvalue":{"$objectToArray":"$$ROOT"}}}, {"$unwind":"$arrayofkeyvalue"}, {"$group":{"_id":null,"allkeys":{"$addToSet":"$arrayofkeyvalue.k"}}} ]) 

คุณยังสามารถใช้ Aggregation เพื่อรับ Keys ใน Document เดียวโดยใช้คำสั่งต่อไปนี้:

db.activities.aggregate([ {"$project":{"arrayofkeyvalue":{"$objectToArray":"$$ROOT"}}}, {"$project":{"keys":"$arrayofkeyvalue.k"}} ]) 

Mongo Shell

คุณยังสามารถรับ Keys ภายใต้ Mongo Shell Client โดยใช้คำสั่งต่อไปนี้:

var allKeys = {}; db.YOURCOLLECTION.find().forEach(function(doc){Object.keys(doc).forEach(function(key){allKeys[key]=1})}); allKeys; 

Output Fields สำหรับ Document เดียว

เนื่องจากแต่ละ Document ใน Collection อาจมี Keys ที่แตกต่างกันทั้งหมด คุณอาจต้องการ Output ของ Fields สำหรับ Document เดียวใน Collection ใช้คำสั่งต่อไปนี้เพื่อทำสิ่งนี้:

doc=db.thinks.findOne(); for (key in doc) print(key); 

Python

คุณสามารถใช้ Python เพื่อรับ MongoDB Keys ตัวอย่างต่อไปนี้คืนค่า Set ของ Top-level Keys ทั้งหมดใน Collection:

#Using pymongo and connection named 'db' reduce( lambda all_keys, rec_keys: all_keys | set(rec_keys), map(lambda d: d.keys(), db.things.find()), set() ) 

JavaScript

JavaScript เสนอวิธีอื่นในการรับ Keys ตามตัวอย่างต่อไปนี้:

db.collection('collectionName').mapReduce( function() { for (var key in this) { emit(key, null); } }, function(key, stuff) { return null; }, { "out": "allFieldNames" }, function(err, results) { var fields = db.collection('allFieldNames').distinct('_id'); fields .then(function(data) { var finalData = { "status": "success", "fields": data }; res.send(finalData); delteCollection(db, 'allFieldNames'); }) .catch(function(err) { res.send(err); delteCollection(db, 'allFieldNames'); }); }); 

หลังจากอ่าน Collection ที่สร้างใหม่ allFieldNames ให้ลบมัน

db.collection("allFieldNames").remove({}, function (err,result) { db.close(); return; }); 

ตัวเลือกเยอะมาก

คุณมีตัวเลือกมากมายในการรับ Mongo Keys นี้ หากคุณรู้สึกท่วมท้นกับการจัดการ MongoDB และต้องการโฟกัสที่โค้ดของคุณแทนการจัดการฐานข้อมูล เราอยู่ที่นี่เพื่อคุณ

เรียนรู้เพิ่มเติมเกี่ยวกับ Rackspace DBA Services

ใช้แท็บ Feedback เพื่อแสดงความคิดเห็นหรือถามคำถาม คุณยังสามารถเริ่มการสนทนากับเราได้