MongoDB - $addToSet on a list of Embedded Document

我怕爱的太早我们不能终老 提交于 2019-11-30 19:06:18
mnemosyn

$addToSet uses the usual mongodb equality rules: it will do a deep value-by-value comparison, so the following two documents are identical:

{ name: "John", hobbies: ["coding", "drinking", "chess"] }
{ hobbies: ["coding", "drinking", "chess"], name: "John" }

(order within documents is not guaranteed, so they are identical)

while those aren't (pairwise):

// compare to:
{ name: "John", hobbies: ["chess", "coding", "drinking"] } 

// in arrays, the order matters:
{ name: "John", hobbies: ["coding", "drinking", "chess"] } 

// field names and values are case sensitive
{ Name: "John", hobbies: ["chess", "coding", "drinking"] } 
{ name: "john", hobbies: ["chess", "coding", "drinking"] } 

// additional field:
{ name: "John", lastName: "Doe", hobbies: ["chess", "coding", "drinking"] }

// missing field:
{ name: "John" }

Please note that there is no special field here. You can add an _id field, but it has no special semantics and will be treated just like any other field.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!