집계 프레임워크 (Aggregation Framework)

문법은 MongoShell과 똑같지만 다시 한번 remind한다는 생각으로 작성해 보았다.

db.collection.aggregate(pipeline, options)
  • Example

mydb.mycollection.aggregate([
{‘$match’ : {‘$text’ : {‘$search’ : ‘ value ’}}},
{‘$sort’ : SON([(‘ field ’, 1)])}, # from bson.son import SON
{‘$project’ : {‘ field1 ’ : 1, ‘ field2 ’ : 1, ‘ field3 ’ : 0}}
])
BSON

index를 사용하는것은 pipeline상 무조건 앞에 쓰는 것이 좋다.

pipeline상 뒤에 있게 되면 index를 사용하지 못하게 될 수 있으니, 질의를 수행하지 못하게 될수도 있어서 에러를 내뱉을 수 있다.

4.0버전에서는 geospatial과 text query는 무조건 맨 앞 stage에 작성해야 한다.

GeoNear

  • only use $geoNear as the first stage of a pipeline.

mydb.mycollection.aggregate([{
‘$geoNear’ : {
‘near’ : { type: ‘ value ’, ‘coordinates’: [ longtitude , latitude ] },
‘distanceField’ : ‘ stringvalue ’,
‘query: {key:value},
‘maxDistance’ : value
}}])

Last updated

Was this helpful?