First of all, I've read this thread already and it didn't really help me on this particular problem. I'm also new to MongoDB.
I have a document in my db.songs
collection:
{
"title" : "Ignorance"
"artist" : "Paramore"
"listeners" : ["John", "Bill", "Amber"]
}
I want enforce no duplicates on the users
key, such that whenever I push "John" or an existing user, I get an error. Can I do this in mongo shell, and if so how can I configure my collection to employ this behavior?
Some example code that should give me a duplicate error (or some similar error):
db.songs.update({title:"Ignorance"}, {'$push':{listeners:"John"}})
Thank you in advance.