정의
버전 5.0에서 변경되었습니다.
$addToSet returns an array of all unique values that results from applying an expression to each document in a group.
반환되는 배열의 요소 순서는 지정되지 않습니다.
$addToSet 이 단계에서 사용할 수 있습니다:
$setWindowFields(MongoDB 5.0부터 사용 가능)
구문
$addToSet 구문:
{ $addToSet: <expression> }
표현식에 대한 자세한 내용은 표현식을 참조하세요 .
행동
If the value of the expression is an array, $addToSet appends the whole array as a single element.
표현식의 값이 문서인 경우, 배열의 다른 문서가 추가할 문서와 정확히 일치하면 MongoDB는 해당 문서가 중복된 것으로 판단합니다. 특히 기존 문서에는 정확히 동일한 필드와 값이 정확히 동일한 순서로 있는 경우입니다.
예시
$group 단계에서 사용
다음 문서가 포함된 sales collection을 생각해 보세요.
db.sales.insertMany( [ { _id : 1, "item" : "abc", "price" : 10, "quantity" : 2, "date" : ISODate("2014-01-01T08:00:00Z") }, { _id : 2, "item" : "jkl", "price" : 20, "quantity" : 1, "date" : ISODate("2014-02-03T09:00:00Z") }, { _id : 3, "item" : "xyz", "price" : 5, "quantity" : 5, "date" : ISODate("2014-02-03T09:05:00Z") }, { _id : 4, "item" : "abc", "price" : 10, "quantity" : 10, "date" : ISODate("2014-02-15T08:00:00Z") }, { _id : 5, "item" : "xyz", "price" : 5, "quantity" : 10, "date" : ISODate("2014-02-15T09:12:00Z") } ] )
Grouping the documents by the day and the year of the date field, the following operation uses the $addToSet accumulator to compute the list of unique items sold for each group:
db.sales.aggregate( [ { $group: { _id: { day: { $dayOfYear: "$date"}, year: { $year: "$date" } }, itemsSold: { $addToSet: "$item" } } } ] )
이 연산은 다음과 같은 결과를 반환합니다.
{ "_id" : { "day" : 46, "year" : 2014 }, "itemsSold" : [ "xyz", "abc" ] } { "_id" : { "day" : 34, "year" : 2014 }, "itemsSold" : [ "xyz", "jkl" ] } { "_id" : { "day" : 1, "year" : 2014 }, "itemsSold" : [ "abc" ] }
$setWindowFields 단계에서 사용
버전 5.0의 신규 항목입니다.
캘리포니아주(CA)와 워싱턴주(WA)의 케이크 판매량이 포함된 cakeSales collection을 생성합니다.
db.cakeSales.insertMany( [ { _id: 0, type: "chocolate", orderDate: new Date("2020-05-18T14:10:30Z"), state: "CA", price: 13, quantity: 120 }, { _id: 1, type: "chocolate", orderDate: new Date("2021-03-20T11:30:05Z"), state: "WA", price: 14, quantity: 140 }, { _id: 2, type: "vanilla", orderDate: new Date("2021-01-11T06:31:15Z"), state: "CA", price: 12, quantity: 145 }, { _id: 3, type: "vanilla", orderDate: new Date("2020-02-08T13:13:23Z"), state: "WA", price: 13, quantity: 104 }, { _id: 4, type: "strawberry", orderDate: new Date("2019-05-18T16:09:01Z"), state: "CA", price: 41, quantity: 162 }, { _id: 5, type: "strawberry", orderDate: new Date("2019-01-08T06:12:03Z"), state: "WA", price: 43, quantity: 134 } ] )
This example uses $addToSet in the $setWindowFields stage to output the unique cake type sales for each state:
db.cakeSales.aggregate( [ { $setWindowFields: { partitionBy: "$state", sortBy: { orderDate: 1 }, output: { cakeTypesForState: { $addToSet: "$type", window: { documents: [ "unbounded", "current" ] } } } } } ] )
예시:
partitionBy: "$state"는 collection의 문서를state로 파티셔닝합니다.CA및WA에 대한 파티션이 있습니다.sortBy: { orderDate: 1 }각 파티션의 문서를orderDate을 기준으로 오름차순(1)으로 정렬하므로, 가장 이른orderDate이 첫 번째가 됩니다.
outputadds each unique caketypeto thecakeTypesForStatearray field using$addToSetthat is run in a documents window.The window contains documents between an
unboundedlower limit and thecurrentdocument. This means$addToSetreturns an array containing the unique caketypefields for the documents between the beginning of the partition and the current document.
이 예시 출력에서는 CA 및 WA의 케이크 type 배열이 cakeTypesForState 필드에 표시됩니다.
{ "_id" : 4, "type" : "strawberry", "orderDate" : ISODate("2019-05-18T16:09:01Z"), "state" : "CA", "price" : 41, "quantity" : 162, "cakeTypesForState" : [ "strawberry" ] } { "_id" : 0, "type" : "chocolate", "orderDate" : ISODate("2020-05-18T14:10:30Z"), "state" : "CA", "price" : 13, "quantity" : 120, "cakeTypesForState" : [ "strawberry", "chocolate" ] } { "_id" : 2, "type" : "vanilla", "orderDate" : ISODate("2021-01-11T06:31:15Z"), "state" : "CA", "price" : 12, "quantity" : 145, "cakeTypesForState" : [ "strawberry", "vanilla", "chocolate" ] } { "_id" : 5, "type" : "strawberry", "orderDate" : ISODate("2019-01-08T06:12:03Z"), "state" : "WA", "price" : 43, "quantity" : 134, "cakeTypesForState" : [ "strawberry" ] } { "_id" : 3, "type" : "vanilla", "orderDate" : ISODate("2020-02-08T13:13:23Z"), "state" : "WA", "price" : 13, "quantity" : 104, "cakeTypesForState" : [ "vanilla", "strawberry" ] } { "_id" : 1, "type" : "chocolate", "orderDate" : ISODate("2021-03-20T11:30:05Z"), "state" : "WA", "price" : 14, "quantity" : 140, "cakeTypesForState" : [ "vanilla", "chocolate", "strawberry" ] }