I was working with MongoKitten today and found a weird and unexpected behavior i.e.., the existence of duplicate keys.
Please refer the image below for better clearity.
What I had tried to do and noticed is as follows -
let gotFromDb = try collection.find(["email": eMail_])var user = try gotFromDb.find().first
Till here 👆 is the same code(or common code), and after that I am getting different results based on different and those are the things I want to share here.
If I do the following -
user.append("Mitesh", forKey: "something")user["something"] = ["ABCD", "eFgH"]try collection.update(["email": _eMail], to: user, upserting: false, multiple: false, writeConcern: nil, stoppingOnError: true)
I face the issue of getting the duplicate keys.
Next case, If I do this -
let myName: Document = ["myName": "\(arc4random_uniform(UInt32(999)))"]user.append(contentsOf: myName) -> This will not create an entry with duplicate key instead it will update to the new value.
user.append(myName) -> This will create an entry with duplicate key
try collection.update(["email": _eMail], to: user, upserting: false, multiple: false, writeConcern: nil, stoppingOnError: true)
Next case, If I do this -
let tokenDocument: Document = ["lalla": ["\(arc4random_uniform(UInt32(999)))", "XKCD", "somethign", "Ron"]]user.append(contentsOf: tokenDocument)try collection.update(["email": _eMail], to: user, upserting: false, multiple: false, writeConcern: nil, stoppingOnError: true)
It works properly I mean the new value overrites the previous one and no value with duplicate key is added.
I was working with MongoKitten today and found a weird and unexpected behavior i.e.., the existence of duplicate keys. Please refer the image below for better clearity.
What I had tried to do and noticed is as follows -
let gotFromDb = try collection.find(["email": eMail_])
var user = try gotFromDb.find().first
Till here 👆 is the same code(or common code), and after that I am getting different results based on different and those are the things I want to share here.If I do the following -
user.append("Mitesh", forKey: "something")
user["something"] = ["ABCD", "eFgH"]
try collection.update(["email": _eMail], to: user, upserting: false, multiple: false, writeConcern: nil, stoppingOnError: true)
I face the issue of getting the duplicate keys.Next case, If I do this -
let myName: Document = ["myName": "\(arc4random_uniform(UInt32(999)))"]
user.append(contentsOf: myName)
-> This will not create an entry with duplicate key instead it will update to the new value.user.append(myName)
-> This will create an entry with duplicate keytry collection.update(["email": _eMail], to: user, upserting: false, multiple: false, writeConcern: nil, stoppingOnError: true)
Next case, If I do this -
let tokenDocument: Document = ["lalla": ["\(arc4random_uniform(UInt32(999)))", "XKCD", "somethign", "Ron"]]
user.append(contentsOf: tokenDocument)
try collection.update(["email": _eMail], to: user, upserting: false, multiple: false, writeConcern: nil, stoppingOnError: true)
It works properly I mean the new value overrites the previous one and no value with duplicate key is added.