๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿ“š Database/Firebase

[Firebase] Firebase ์ฟผ๋ฆฌ ์•Œ์•„๋ณด๊ธฐ(FireStore Query)

by Fomagran ๐Ÿ’ป 2021. 5. 25.
728x90
๋ฐ˜์‘ํ˜•

 

์•ˆ๋…•ํ•˜์„ธ์š” Foma ๐Ÿ‘Ÿ ์ž…๋‹ˆ๋‹ค!

์˜ค๋Š˜์€ ํŒŒ์ด์–ด์Šคํ† ์–ด์—์„œ ์›ํ•˜๋Š” ์กฐ๊ฑด์œผ๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ€์ ธ์˜ฌ ์ˆ˜ ์žˆ๋Š” ์—ฌ๋Ÿฌ ์ฟผ๋ฆฌ๋ฌธ์— ๋Œ€ํ•ด์„œ ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค!


WhereField

 

isEqualTo 

 

ํ•„๋“œ์— ํฌ๋งˆ๋ผ๋Š” ๊ฐ’๊ณผ ๊ฐ™์€ ๋ฌธ์„œ๊ฐ€ ์ „๋ถ€ ๋ฐ˜ํ™˜๋ฉ๋‹ˆ๋‹ค.

db.collection("์ปฌ๋ ‰์…˜ ์ด๋ฆ„").whereField("ํ•„๋“œ๋ช…", isEqualTo: "ํฌ๋งˆ")

isNotEqualTo

 

ํ•„๋“œ์— ํฌ๋งˆ๋ผ๋Š” ๊ฐ’์ด ์•„๋‹Œ ๋ฌธ์„œ๊ฐ€ ์ „๋ถ€ ๋ฐ˜ํ™˜๋ฉ๋‹ˆ๋‹ค.

db.collection("์ปฌ๋ ‰์…˜ ์ด๋ฆ„").whereField("ํ•„๋“œ๋ช…", isNotEqualTo: "ํฌ๋งˆ")

isGreaterThan

 

ํ•„๋“œ๊ฐ’์ด 1000๋ณด๋‹ค ํฐ ๋ฌธ์„œ๊ฐ€ ์ „๋ถ€ ๋ฐ˜ํ™˜๋ฉ๋‹ˆ๋‹ค.

db.collection("์ปฌ๋ ‰์…˜ ์ด๋ฆ„").whereField("ํ•„๋“œ๋ช…", isGreaterThan: 1000)

isLessThan

 

ํ•„๋“œ๊ฐ’์ด 1000๋ณด๋‹ค ์ž‘์€ ๋ฌธ์„œ๊ฐ€ ์ „๋ถ€ ๋ฐ˜ํ™˜๋ฉ๋‹ˆ๋‹ค.

 

db.collection("์ปฌ๋ ‰์…˜ ์ด๋ฆ„").whereField("ํ•„๋“œ๋ช…", isLessThan: 1000)

in

 

ํ•„๋“œ๊ฐ€ ํฌ๋งˆ OR ๊ทธ๋žœ OR iOS ์ธ ๋ฌธ์„œ๋“ค์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

db.collection("์ปฌ๋ ‰์…˜ ์ด๋ฆ„").whereField("ํ•„๋“œ๋ช…", in: ["ํฌ๋งˆ","๊ทธ๋žœ","iOS"])

notIn

 

ํ•„๋“œ๊ฐ€ ํฌ๋งˆ AND ๊ทธ๋žœ AND iOS ๊ฐ€ ์•„๋‹Œ ๋ฌธ์„œ๋“ค์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

db.collection("์ปฌ๋ ‰์…˜ ์ด๋ฆ„").whereField("ํ•„๋“œ๋ช…", in: ["ํฌ๋งˆ","๊ทธ๋žœ","iOS"])

arrayContains

 

ํ•„๋“œ์— ์žˆ๋Š” ๋ฐฐ์—ด ์ค‘ ํฌ๋งˆ๊ฐ€ ํฌํ•จ๋œ ๋ฌธ์„œ๋ฅผ ์ „๋ถ€ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

db.collection("์ปฌ๋ ‰์…˜ ์ด๋ฆ„").whereField("ํ•„๋“œ๋ช…", arrayContains: "ํฌ๋งˆ")

arrayContainsAny

 

ํ•„๋“œ์— ์žˆ๋Š” ๋ฐฐ์—ด ์ค‘ ํฌ๋งˆ ๋˜๋Š” ๊ทธ๋žœ ๋˜๋Š” iOS๊ฐ€ ํฌํ•จ๋œ ๋ฌธ์„œ๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

db.collection("์ปฌ๋ ‰์…˜ ์ด๋ฆ„").whereField("ํ•„๋“œ๋ช…", arrayContainsAny: ["ํฌ๋งˆ","๊ทธ๋žœ","iOS"])

์ œํ•œ์‚ฌํ•ญ

 

OR์ด๋‚˜ AND ์ ˆ์€ ์ตœ๋Œ€ 10๊ฐœ๊นŒ์ง€ ์“ธ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ex)whereField ,in: ["ํฌ๋งˆ","๊ทธ๋žœ","iOS"....์ตœ๋Œ€ 10๊ฐœ]

 


Order

 

ascending

 

ํ•„๋“œ์•ˆ์— ์žˆ๋Š” ๊ฐ’์„ ๊ธฐ์ค€์œผ๋กœ ์˜ค๋ฆ„์ฐจ์ˆœ ์ •๋ ฌํ•ฉ๋‹ˆ๋‹ค.

 

or

db.collection("์ปฌ๋ ‰์…˜ ์ด๋ฆ„").order(by:"ํ•„๋“œ๋ช…",descending:false)

 

descending

 

ํ•„๋“œ์•ˆ์— ์žˆ๋Š” ๊ฐ’์„ ๊ธฐ์ค€์œผ๋กœ ๋‚ด๋ฆผ์ฐจ์ˆœ ์ •๋ ฌํ•ฉ๋‹ˆ๋‹ค.

db.collection("์ปฌ๋ ‰์…˜ ์ด๋ฆ„").order(by:"ํ•„๋“œ๋ช…",descending:true)

limit

 

to

 

๋ฌธ์„œ ์ค‘ ์œ„์—์„œ๋ถ€ํ„ฐ 3๊ฐœ๊นŒ์ง€ ๊ฐ€์ ธ์˜ต๋‹ˆ๋‹ค. ex) [1,2,3,4,5,6,7,8,9] => 1,2,3

db.collection("์ปฌ๋ ‰์…˜ ์ด๋ฆ„").limit(to:3)

 

toLast

 

๋ฌธ์„œ ์ค‘ ๋์—์„œ๋ถ€ํ„ฐ 3๊ฐœ๊นŒ์ง€ ๊ฐ€์ ธ์˜ต๋‹ˆ๋‹ค. ex) [1,2,3,4,5,6,7,8,9] => 7,8,9

db.collection("์ปฌ๋ ‰์…˜ ์ด๋ฆ„").limit(toLast:3)

Start

 

at

 

100์„ ๊ฐ€์ง„ ๋ฌธ์„œ๋ถ€ํ„ฐ ๊ฐ€์ ธ์˜ต๋‹ˆ๋‹ค.

db.collection("์ปฌ๋ ‰์…˜ ์ด๋ฆ„").start(at:[100])

after

 

100์„ ๊ฐ€์ง„ ๋ฌธ์„œ ์ดํ›„๋ถ€ํ„ฐ ๊ฐ€์ ธ์˜ต๋‹ˆ๋‹ค.

db.collection("์ปฌ๋ ‰์…˜ ์ด๋ฆ„").start(after:[100])

atDocument

 

ํŠน์ • ๋ฌธ์„œ๋ถ€ํ„ฐ ๊ฐ€์ ธ์˜ต๋‹ˆ๋‹ค.

db.collection("์ปฌ๋ ‰์…˜ ์ด๋ฆ„").start(atDocument:๋ฌธ์„œ)

afterDocument

 

ํŠน์ • ๋ฌธ์„œ ์ดํ›„๋ถ€ํ„ฐ ๊ฐ€์ ธ์˜ต๋‹ˆ๋‹ค.

db.collection("์ปฌ๋ ‰์…˜ ์ด๋ฆ„").start(afterDocument:๋ฌธ์„œ)

End

 

at

 

100์„ ๊ฐ€์ง„ ๋ฌธ์„œ๊นŒ์ง€ ๊ฐ€์ ธ์˜ต๋‹ˆ๋‹ค.

db.collection("์ปฌ๋ ‰์…˜ ์ด๋ฆ„").end(at:[100])

after

 

100์„ ๊ฐ€์ง„ ๋ฌธ์„œ ์ „๊นŒ์ง€ ๊ฐ€์ ธ์˜ต๋‹ˆ๋‹ค.

db.collection("์ปฌ๋ ‰์…˜ ์ด๋ฆ„").end(before:[100])

atDocument

 

ํŠน์ • ๋ฌธ์„œ๊นŒ์ง€ ๊ฐ€์ ธ์˜ต๋‹ˆ๋‹ค.

db.collection("์ปฌ๋ ‰์…˜ ์ด๋ฆ„").end(atDocument:๋ฌธ์„œ)

afterDocument

 

ํŠน์ • ๋ฌธ์„œ ์ „๊นŒ์ง€ ๊ฐ€์ ธ์˜ต๋‹ˆ๋‹ค.

db.collection("์ปฌ๋ ‰์…˜ ์ด๋ฆ„").end(beforeDocument:๋ฌธ์„œ)

 

728x90
๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€