Why Does Mongodb Require `unique:true` To Create A Collection?
Solution 1:
I just tried replicating the scenario you described above on my end with near the same version of mongoose and MongoDB, it worked fine, documents were created(and saved) without the unique property on the firstName
field in the schema. I don't think the reason your objects were not saved in the DB is because of the absence of the unique property.
Also, the reason the collection was not created without the unique property is that mongoose(or MongoDB itself) won't create a collection until you insert data into the collection. By adding the unique property, MongoDB would have to create an index document for that property in the collection consequently adding data to the collection and this is why it seems that the collection was created only after adding the unique property. So while the db.<collectionName>.find({}
) might return nothing, db.<collectionName>.getIndexes()
would give you something.
Post a Comment for "Why Does Mongodb Require `unique:true` To Create A Collection?"