$addToSet 使用通常的mongodb equality rules:它会进行逐个值的深度比较,因此以下两个文档是相同的:
{ name: "John", hobbies: ["coding", "drinking", "chess"] }
{ hobbies: ["coding", "drinking", "chess"], name: "John" }
(文档is not guaranteed内的顺序,所以它们是相同的)
而那些不是(成对的):
// 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" }
请注意这里没有特殊字段。您可以添加 _id 字段,但它没有特殊语义,将与任何其他字段一样处理。