티스토리 뷰

반응형
db.collection.distinct(field, query, options)
{ "_id": 1, "dept": "A", "item": { "sku": "111", "color": "red" }, "sizes": [ "S", "M" ] }
{ "_id": 2, "dept": "A", "item": { "sku": "111", "color": "blue" }, "sizes": [ "M", "L" ] }
{ "_id": 3, "dept": "B", "item": { "sku": "222", "color": "blue" }, "sizes": "S" }
{ "_id": 4, "dept": "A", "item": { "sku": "333", "color": "black" }, "sizes": [ "S" ] }

 

 

[ MongoDB ]

> db.inventory.distinct( "dept" )
[ "A", "B" ]

> db.inventory.distinct( "item.sku" )
[ "111", "222", "333" ]

> db.inventory.distinct( "item.sku", { dept: "A" } )
[ "111", "333" ]

> db.inventory.distinct( "sizes" )
[ "M", "S", "L" ]

 

[ JAVA ]

mongoTemplate.getCollection("inventory").distinct("dept", Document.class);

mongoTemplate.getCollection("inventory").distinct("item.sku");

mongoTemplate.getCollection("inventory").distinct("item.sku", query);

mongoTemplate.getCollection("inventory").distinct("sizes");

 

반응형
반응형