다음은 MongoDB 컬렉션의 문서 수를 계산하는 구문입니다.
let anyVariableName= db.getCollection(‘yourCollectionName’); yourVariableName.count();
먼저 문서로 컬렉션을 생성하겠습니다.
> db.countNumberOfDocumentsDemo.insertOne({"CustomerName":"Bob"});
{
"acknowledged" : true,
"insertedId" : ObjectId("5c9a5e2015e86fd1496b38a1")
}
>db.countNumberOfDocumentsDemo.insertOne({"CustomerName":"Ramit","CustomerAge":23});
{
"acknowledged" : true,
"insertedId" : ObjectId("5c9a5e3515e86fd1496b38a2")
}
>db.countNumberOfDocumentsDemo.insertOne({"CustomerName":"Adam","CustomerAge":27,"CustomerCountryName":"US"});
{
"acknowledged" : true,
"insertedId" : ObjectId("5c9a5e4c15e86fd1496b38a3")
} 다음은 find() 메서드를 사용하여 컬렉션의 모든 문서를 표시하는 쿼리입니다.
> db.countNumberOfDocumentsDemo.find().pretty();
그러면 다음과 같은 출력이 생성됩니다.
{ "_id" : ObjectId("5c9a5e2015e86fd1496b38a1"), "CustomerName" : "Bob" }
{
"_id" : ObjectId("5c9a5e3515e86fd1496b38a2"),
"CustomerName" : "Ramit",
"CustomerAge" : 23
}
{
"_id" : ObjectId("5c9a5e4c15e86fd1496b38a3"),
"CustomerName" : "Adam",
"CustomerAge" : 27,
"CustomerCountryName" : "US"
} 다음은 MongoDB 컬렉션의 문서 수를 계산하는 쿼리입니다.
> let myCollectionName = db.getCollection('countNumberOfDocumentsDemo');
> myCollectionName.count(); 그러면 다음과 같은 출력이 생성됩니다.
3