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

MongoDB में दो फ़ील्ड की तुलना पर क्वेरी शर्त?


दो फ़ील्ड की तुलना पर क्वेरी शर्त के लिए, निम्न सिंटैक्स का उपयोग करें:

db.yourCollectionName.find( { $where: function() { return this.yourFirstFieldName < this.yourSecondFieldName } } ).pretty();

सिंटैक्स समझने के लिए, एक दस्तावेज़ के साथ एक संग्रह बनाते हैं। दस्तावेज़ के साथ संग्रह बनाने की क्वेरी इस प्रकार है:

> db.comparingTwoFieldsDemo.insertOne({"StudentName":"John","StudentAge":21,"StudentMathMarks":99,"StudentPhysicsMarks":98}); { "acknowledged" : true, "insertedId" : ObjectId("5c8ac09e6cea1f28b7aa0807") } > db.comparingTwoFieldsDemo.insertOne({"StudentName":"Carol","StudentAge":22,"StudentMathMarks":79,"StudentPhysicsMarks":89}); { "acknowledged" : true, "insertedId" : ObjectId("5c8ac0b46cea1f28b7aa0808") } > db.comparingTwoFieldsDemo.insertOne({"StudentName":"David","StudentAge":24,"StudentMathMarks":39,"StudentPhysicsMarks":45}); { "acknowledged" : true, "insertedId" : ObjectId("5c8ac0c96cea1f28b7aa0809") } > db.comparingTwoFieldsDemo.insertOne({"StudentName":"Bob","StudentAge":23,"StudentMathMarks":87,"StudentPhysicsMarks":78}); { "acknowledged" : true, "insertedId" : ObjectId("5c8ac0e06cea1f28b7aa080a") }

find() विधि की मदद से संग्रह से सभी दस्तावेज़ प्रदर्शित करें। क्वेरी इस प्रकार है:

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

आउटपुट निम्न है:

{ "_id" : ObjectId("5c8ac09e6cea1f28b7aa0807"), "StudentName" : "John", "StudentAge" : 21, "StudentMathMarks" : 99, "StudentPhysicsMarks" : 98 } { "_id" : ObjectId("5c8ac0b46cea1f28b7aa0808"), "StudentName" : "Carol", "StudentAge" : 22, "StudentMathMarks" : 79, "StudentPhysicsMarks" : 89 } { "_id" : ObjectId("5c8ac0c96cea1f28b7aa0809"), "StudentName" : "David", "StudentAge" : 24, "StudentMathMarks" : 39, "StudentPhysicsMarks" : 45 } { "_id" : ObjectId("5c8ac0e06cea1f28b7aa080a"), "StudentName" : "Bob", "StudentAge" : 23, "StudentMathMarks" : 87, "StudentPhysicsMarks" : 78 }

दो फ़ील्ड की तुलना पर शर्त की क्वेरी यहाँ है:

> db.comparingTwoFieldsDemo.find( { $where: function() { return this.StudentMathMarks < this.StudentPhysicsMarks } } ).pretty();

आउटपुट निम्न है:

{ "_id" : ObjectId("5c8ac0b46cea1f28b7aa0808"), "StudentName" : "Carol", "StudentAge" : 22, "StudentMathMarks" : 79, "StudentPhysicsMarks" : 89 } { "_id" : ObjectId("5c8ac0c96cea1f28b7aa0809"), "StudentName" : "David", "StudentAge" : 24, "StudentMathMarks" : 39, "StudentPhysicsMarks" : 45 }

  1. - MongoDB में सबडॉक्यूमेंट पर शर्त के आधार पर दस्तावेज़ कैसे क्वेरी करें?

    MongoDB में सरणी के अंदर सबडॉक्यूमेंट पर शर्त के आधार पर दस्तावेज़ फ़िल्टर करने के लिए, $expr के साथ $let, $arrayElemAt (या $filter/$elemMatch) का उपयोग करें। उदाहरण: दिनांक सीमा वाले सबडॉक्यूमेंट्स की सरणी > db.demo394.insertOne({ details: [ { _id: 1, startDate: 2018-01-11T07:00:00.

  1. - MongoDB क्वेरी: एकाधिक फ़ील्ड के अस्तित्व की जांच करें

    एकाधिक फ़ील्ड के अस्तित्व की जांच के लिए $exists के साथ $and ऑपरेटर का उपयोग करें। आइए दस्तावेज़ों के साथ एक संग्रह बनाएं: > db.demo475.insertOne({"StudentFirstName":"Chris","StudentAge":23});{    "acknowledged" : true,    "inserte

  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