Finding a duplicate key in the collection

Hi,

I have the below data where there are 2 bookingLineId fields. I would like to get the number of such duplicate bookingLineId fields in my collection. Please let us know on this.

{
    "_id" : ObjectId("5f4e18f4f90762fe549a1eee"),
    "bookingNumber" : "NOO1034684",
    "bookingLines" : [ 
        {
            *"bookingLineId" : 11062129,*
            *"bookingLineId" : 99169522,*
            "parentBookingLineId" : null,
            "articleCode" : "E",
            "isExtension" : false,
        }, 
        {
            "bookingLineId" : 11062130,
            "parentBookingLineId" : 11062129,
            "articleCode" : "COL2",
            "isExtension" : false,
        }
    ],
    "bookingSchoolId" : null,
    "marketCode" : "NOO",
    "marketKey" : "Norway",
    "opportunityId" : "0061w000019Byu0AAC",
    "programCode" : "ILS",
}

If you inserted such a document into MongoDB only one of those fields would get created. Its not possible to store a document in MongoDB with two fields with the same key at the same level. The second key will overwrite the first.

I ran your example in the shell:

> use test
switched to db test
> db.community.insertOne({
...     "bookingNumber" : "NOO1034684",
...     "bookingLines" : [
...         {
...             "bookingLineId" : 11062129,
...             "bookingLineId" : 99169522,
...             "parentBookingLineId" : null,
...             "articleCode" : "E",
...             "isExtension" : false,
...         },
...         {
...             "bookingLineId" : 11062130,
...             "parentBookingLineId" : 11062129,
...             "articleCode" : "COL2",
...             "isExtension" : false,
...         }
...     ],
...     "bookingSchoolId" : null,
...     "marketCode" : "NOO",
...     "marketKey" : "Norway",
...     "opportunityId" : "0061w000019Byu0AAC",
...     "programCode" : "ILS",
... })
{
	"acknowledged" : true,
	"insertedId" : ObjectId("5f59e8ffc942c3417cad207b")
}
> db.community.findOne()
{
	"_id" : ObjectId("5f59e8ffc942c3417cad207b"),
	"bookingNumber" : "NOO1034684",
	"bookingLines" : [
		{
			"bookingLineId" : 99169522,
			"parentBookingLineId" : null,
			"articleCode" : "E",
			"isExtension" : false
		},
		{
			"bookingLineId" : 11062130,
			"parentBookingLineId" : 11062129,
			"articleCode" : "COL2",
			"isExtension" : false
		}
	],
	"bookingSchoolId" : null,
	"marketCode" : "NOO",
	"marketKey" : "Norway",
	"opportunityId" : "0061w000019Byu0AAC",
	"programCode" : "ILS"
}
>

The first ID is obliterated on insert.

1 Like

If I may add. Please see https://docs.mongodb.com/manual/core/document/#field-names

In particular the following 2 paragraphs:

BSON documents may have more than one field with the same name. Most MongoDB interfaces, however, represent MongoDB with a structure (e.g. a hash table) that does not support duplicate field names. If you need to manipulate documents that have more than one field with the same name, see the driver documentation for your driver.

Some documents created by internal MongoDB processes may have duplicate fields, but no MongoDB process will ever add duplicate fields to an existing user document.

4 Likes

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.