How to set unique constraint for field in document nested in array?
问题 I have a collection of documents in MongoDB that looks like: {"_id": 1, "array": [{"id": 1, "content": "..."}, {"id": 2, "content": "..."}]} {"_id": 2, "array": [{"id": 1, "content": "..."}, {"id": 2, "content": "..."}, {"a_id": 3, "content": "..."}]} and I want to ensure that there is no duplicate array.id within each document. So the provided example is ok, but the followign is not: {"_id": 1, "array": [{"id": 1, "content": "..."}, {"id": 1, "content": "..."}]} My question is how to do this