04-29 02:46
Notice
Recent Posts
Recent Comments
관리 메뉴

Scientific Computing & Data Science

[MongoDB] Query / $all 본문

Data Science/MongoDB

[MongoDB] Query / $all

cinema4dr12 2014. 2. 1. 15:25

by Geol Choi | 

우선 다음과 같이 "food" 컬렉션에 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"]})

"apple"과 "banana" 모두를 포함하는 도큐먼트는 ID 1과 ID 3이다.


"$all"을 통해 이 두 가지 모두를 포함하는 도큐먼트를 검색해 보자.

> db.food.find({fruit : {$all : ["apple", "banana"]}})
{ "_id" : 1, "fruit" : [  "apple",  "banana",  "peach" ] }
{ "_id" : 3, "fruit" : [  "cherry",  "banana",  "apple" ] }

예상대로 ID 1과 3이 검색 되었다.

Comments