Mongodb allows multiple keys with the same name

I was under the impression that this should never happen:

Playername is an indexed field, so having two instances of an indexed field is even weirder than an unindexed field.

The value “ahmer#44164” already exists in another document, so mongodb throws an error if i try to set the first playername to “ahmer#44164", but not if I do it on the second field

Querying for the playername “ahmer#44164” yields only one document

Steps to reproduce:

  • make a blank doc
  • right click the doc in robo3t and click edit
  • add the duplicate keys
  • save

I found this question on stackoverflow but it has no explanation.

The underlying BSON storage format in MongoDB uses an ordered object representation rather than a dictionary, so technically duplicate fields are allowed (but not commonly used).

This is noted in the documentation on field names:

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.

I would consider creation of duplicate fields when creating/editing a document via an Admin UI an unexpected outcome. The typical mental model is that MongoDB documents are JSON-like with additional field types.

Unfortunately Robo3T is not actively maintained and embeds a specific version of the mongo shell rather than using the driver API. It has a growing backlog of bugs and compatibility issues that are not being triaged.

I strongly recommend using an actively developed MongoDB admin UI such as MongoDB Compass.

For more comparison on BSON vs JSON you might be interested in: Why does MongoDB check for order of keys when matching embedded documents? (Software Engineering StackExchange).

Regards,
Stennie

4 Likes

Did you verify if one of the field name is “playername” while the other is "playername "? The trailing blank at the end does not show in the GUI but it makes the field name differents. That possibility was mentionned in the link you provided. You should connect with the shell and print the document from there.

It’s exactly the same. I manually typed the two fields so there aren’t any unicode hijinks going on either.

As Stennie pointed out above, Robo3T is not actively maintained, and this bug has been open since Dec 2014

2 posts were merged into an existing topic: Issues with attempting to Create a Compound Index

2 posts were split to a new topic: Issues with attempting to Create a Compound Index