03-29 08:50
Notice
Recent Posts
Recent Comments
관리 메뉴

Scientific Computing & Data Science

[MongoDB] Query / $size 본문

Data Science/MongoDB

[MongoDB] Query / $size

cinema4dr12 2014. 2. 1. 15:39

by Geol Choi | 

"$size"는 array의 크기를 이용하여 검색할 수 있는 오퍼레이터이다.

우선 다음과 같이 아이템을 준비하자.

> db.food.drop()

> db.food.insert({"_id" : 1, "fruit" : ["apple", "banana", "peach"]})
> db.food.insert({"_id" : 2, "fruit" : ["apple", "kumquat", "orange"]})
> db.food.insert({"_id" : 3, "fruit" : ["cherry", "banana", "apple"]})
> db.food.insert({"_id" : 4, "fruit" : ["pineapple", "plum"]})


ID 1, 2, 3에 대해서는 "fruit"의 array 사이즈가 모두 3이다. (즉 array의 아이템이 3개이다) 이에 반해서 ID 4는 "fruit"에 대한 array의 사이즈가 2이다. 다음과 같이 입력해 보자.

> db.food.find({"fruit" : {"$size" : 2}})
{ "_id" : 4, "fruit" : [  "pineapple",  "plum" ] }

"fruit"의 array 크기가 2인 것을 검색했으므로 ID 4만이 검색되었다.

만약 array 크기가 3 이상의 도큐먼트를 검색하려면 다음을 이용하자.

> db.food.find({"size" : {"$gt" : 3}})


'Data Science > MongoDB' 카테고리의 다른 글

[MongoDB] Query / Querying Embedded Documents  (0) 2014.02.02
[MongoDB] Query / $slice  (0) 2014.02.01
[MongoDB] Query / $all  (0) 2014.02.01
[MongoDB] Query / find  (0) 2014.02.01
[MongoDB] Update Modifiers / Part 9. - findAndModify  (0) 2014.01.31
Comments