$redact documents के contents को documents themselves में stored information के आधार पर restrict करता है। Aggregate में $cond के साथ $redact का उपयोग कर सकते हैं।
Collection बनाना
> db.demo546.insertOne({"Value1":10,"Value2":20});
{ "acknowledged" : true, "insertedId" : ObjectId("5e8e263f9e5f92834d7f05d7") }
> db.demo546.insertOne({"Value1":40,"Value2":30,Value3:50});
{ "acknowledged" : true, "insertedId" : ObjectId("5e8e26549e5f92834d7f05d8") }
> db.demo546.insertOne({"Value1":100,"Value2":200,Value3:null});
{ "acknowledged" : true, "insertedId" : ObjectId("5e8e26619e5f92834d7f05d9") }
> db.demo546.insertOne({"Value1":400,"Value2":1000,Value3:null});
{ "acknowledged" : true, "insertedId" : ObjectId("5e8e26e09e5f92834d7f05da") }
> db.demo546.insertOne({"Value1":400,"Value2":200,Value3:null});
{ "acknowledged" : true, "insertedId" : ObjectId("5e8e26f59e5f92834d7f05db") }
> db.demo546.insertOne({"Value1":400,"Value2":1000,Value3:60});
{ "acknowledged" : true, "insertedId" : ObjectId("5e8e27159e5f92834d7f05dc") }
सभी Documents Display करना
> db.demo546.find();
Output:
{ "_id" : ObjectId("5e8e263f9e5f92834d7f05d7"), "Value1" : 10, "Value2" : 20 }
{ "_id" : ObjectId("5e8e26549e5f92834d7f05d8"), "Value1" : 40, "Value2" : 30, "Value3" : 50 }
{ "_id" : ObjectId("5e8e26619e5f92834d7f05d9"), "Value1" : 100, "Value2" : 200, "Value3" : null }
{ "_id" : ObjectId("5e8e26e09e5f92834d7f05da"), "Value1" : 400, "Value2" : 1000, "Value3" : null }
{ "_id" : ObjectId("5e8e26f59e5f92834d7f05db"), "Value1" : 400, "Value2" : 200, "Value3" : null }
{ "_id" : ObjectId("5e8e27159e5f92834d7f05dc"), "Value1" : 400, "Value2" : 1000, "Value3" : 60 }
$exists के साथ $redact Query
> db.demo546.aggregate( { "$redact": { "$cond": { "if": { "$and": [ { "$lt": [ "$Value1", "$Value2" ] }, { "$ifNull": [ "$Value3", false ] } ] }, "then": "$$KEEP", "else": "$$PRUNE" } } } );
Output:
{ "_id" : ObjectId("5e8e27159e5f92834d7f05dc"), "Value1" : 400, "Value2" : 1000, "Value3" : 60 }
व्याख्या: $redact document को recursively process करता है। $$KEEP document को keep करता है, $$PRUNE remove करता है। यहाँ condition है: Value1 < Value2 AND Value3 null नहीं है। केवल last document इस condition को satisfy करता है।