आंतरिक सरणी में डुप्लिकेट की जांच करने के लिए, MongoDB में समुच्चय () का उपयोग करें। आइए दस्तावेजों के साथ एक संग्रह बनाएं -
> db.demo347.insertOne(
... {
... "details": {
... "details1": [
... {
... Name: "Chris",
... Age: 21
... }
...
... ]
... }
... }
... );
{
"acknowledged" : true,
"insertedId" : ObjectId("5e5532eaf8647eb59e5620af")
}
> db.demo347.insertOne(
... {
... "details": {
... "details1": [
... {
... Name: "David",
... Age: 22
... }
...
... ]
... }
... }
... );
{
"acknowledged" : true,
"insertedId" : ObjectId("5e55330af8647eb59e5620b0")
}
> db.demo347.insertOne(
... {
... "details": {
... "details1": [
... {
... Name: "Chris",
... Age: 21
... }
...
... ]
... }
... }
... );
{
"acknowledged" : true,
"insertedId" : ObjectId("5e55331cf8647eb59e5620b1")
} संग्रह से सभी दस्तावेज़ों को खोजने () विधि की सहायता से प्रदर्शित करें -
> db.demo347.find();
यह निम्नलिखित आउटपुट देगा -
{ "_id" : ObjectId("5e5532eaf8647eb59e5620af"), "details" : { "details1" : [ { "Name" : "Chris", "Age" : 21 } ] } }
{ "_id" : ObjectId("5e55330af8647eb59e5620b0"), "details" : { "details1" : [ { "Name" : "David", "Age" : 22 } ] } }
{ "_id" : ObjectId("5e55331cf8647eb59e5620b1"), "details" : { "details1" : [ { "Name" : "Chris", "Age" : 21 } ] } } कुछ फ़ील्ड के डुप्लीकेट चेक करने के लिए क्वेरी निम्नलिखित है -
> db.demo347.aggregate([
... {"$unwind": "$details"},
... {"$unwind": "$details.details1"},
... {"$group" : { "_id": "$details.details1.Name", "count": { "$sum": 1 } } },
... {"$match": { "_id" :{ "$ne" : null } , "count" : { "$gt": 1} } }
... ]) यह निम्नलिखित आउटपुट देगा -
{ "_id" : "Chris", "count" : 2 }