Skip to main content

👐 $count

我们如何确定此管道返回的正好是15本书?

let moreThan150pages =  {pages: {$gte: 150}}
let booksFrom1985WithMoreThan150pages = {$match: {$and: [{year: 1985}, moreThan150pages]}};
let showOnlyTheseFields = {$project: {_id: 0, title: 1, year: 1, totalInventory: 1, available: 1}};
let getJust15books = {$limit: 15};

db.books.aggregate([
booksFrom1985WithMoreThan150pages,
showOnlyTheseFields,
getJust15books,
]);

计数书籍!让我们向管道中添加一个新阶段来计数这些书籍:

let moreThan150pages =  {pages: {$gte: 150}}
let booksFrom1985WithMoreThan150pages = {$match: {$and: [{year: 1985}, moreThan150pages]}};
let showOnlyTheseFields = {$project: {_id: 0, title: 1, year: 1, totalInventory: 1, available: 1}};
let getJust15books = {$limit: 15};
let count = {$count: "totalBooks"}

db.books.aggregate([
booksFrom1985WithMoreThan150pages,
showOnlyTheseFields,
getJust15books,
count,
]);

在这里,$count将计数结果文档并返回一个只有一个字段的文档:totalBooks

{
totalBooks: 15
}

👐 我们的authors集合中有多少作者?

答案
db.authors.aggregate([{$count: "authorCount"}])

阶段的顺序很重要

如果我们在$match阶段之后进行计数,我们传递给下一步骤的文档只包含totalBooks字段。因此,$project阶段将返回一个空文档。

let moreThan150pages =  {pages: {$gte: 150}}
let booksFrom1985WithMoreThan150pages = {$match: {$and: [{year: 1985}, moreThan150pages]}};
let showOnlyTheseFields = {$project: {_id: 0, title: 1, year: 1, totalInventory: 1, available: 1}};
let getJust15books = {$limit: 15};
let count = {$count: "totalBooks"}

db.books.aggregate([
booksFrom1985WithMoreThan150pages,
count,
showOnlyTheseFields,
getJust15books,
]);