이 스키마에 문서가있는 컬렉션이 있습니다 (그래도 스키마 가 없는 세상이라는 것을 알고 있습니다 ).
{
name: "Real Colicos",
fee: 123,
creator: {},
participants: [{},{}]
}
따라서 참가자 수별로 정렬 된 첫 번째 그룹을 가져올 수 있는 쿼리가 필요합니다 . 물론 참가자 수 속성을 가질 수 있고 사용하여 업데이트시 증가시킬 수 $inc
있지만 순진한 접근 방식이라고 생각합니다. 이 집계 또는 맵이 영역을 줄입니까?
aggregate
.에서 문서 나는이 걸릴 수 :
db.groups.aggregate(
[
{
$project: {
name: 1,
participantsCount: {$size: "$participants"}
}
}
])
이것은 작동합니다 . 이제 검색 페이지를 매기고 참가자 수를 기준으로 정렬하고 일부 문서 속성 만 포함 할 수 있는지 궁금합니다.
난 노력 했어:
db.groups.aggregate(
[
{
$project: {
name: 1,
participantsCount: {$size: "$participants"}
},
$skip: 10,
$limit: 5,
$sort: {participantsCount: -1},
$match: {isPrivate: false}
}
],
function (err, results) {
console.log(results);
}
);
그러나 undefined
결과적으로 던졌습니다 .
또한:
db.groups.aggregate(
[
{
$project: {
name: 1,
participantsCount: {$size: "$participants"}
},
{$skip: 10},
{$limit: 5},
{$sort: {participantsCount: -1}},
{$match: {isPrivate: false}}
}
],
function (err, results) {
console.log(results);
}
);
빈 배열을 던지고 있습니다.
그리고 심지어 :
db.groups.aggregate(
[
{$project: {name: 1, isPrivate: 1, participantsCount: {$size: "$participants"}}},
{$match: {isPrivate: false}},
{$skip: 10},
{$limit: 5},
{$sort: {participantsCount: -1}}
],
function (err, results) {
console.log(results);
}
);
또한 빈 배열을 던집니다.
문서에서 :
집계 파이프 라인은 데이터 처리 파이프 라인의 개념을 기반으로 모델링 된 데이터 집계를위한 프레임 워크입니다. 문서는 문서를 집계 된 결과로 변환하는 다단계 파이프 라인에 들어갑니다.
집계 작업에 컬렉션의 데이터 하위 집합 만 필요한 경우 $ match, $ limit 및 $ skip 단계를 사용하여 파이프 라인 시작 부분에 입력되는 문서를 제한합니다.
따라서 원하는 결과를 얻기 위해 파이프 라인이 처리됩니다. 두 번째 단락에서 알 수 있듯이 파이프 라인 처리 순서가 중요 합니다.
다음과 같이 표시되어야합니다.
collection.aggregate(
[
{$match: {isPrivate: false}},
{$skip: 0},
{$limit: 2},
{$project: {name: 1, participantsCount: {$size: "$participants"}}},
{$sort: {participantsCount: -1}}
]
);
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다