ในการนับจำนวนเอกสารในคอลเลกชัน MongoDB คุณสามารถใช้ไวยากรณ์ด้านล่าง -
db.getCollectionNames().map(function(anyVariableName) { return { "yourVariableName": yourVariableName, "count": db[yourVariableName].count() } });
ที่นี่เราใช้ฐานข้อมูล 'ทดสอบ'
ให้เราใช้ไวยากรณ์ข้างต้นเพื่อรับการนับจำนวนเอกสารในคอลเล็กชัน MongoDB -
> db.getCollectionNames().map(function(ColName) { ... return { "ColName": ColName, "TotalDocument": db[ColName].count() } ... });
สิ่งนี้จะสร้างผลลัพธ์ต่อไปนี้ -
[ { "ColName" : "ConvertStringToDateDemo", "TotalDocument" : 4 }, { "ColName" : "Employee_Information", "TotalDocument" : 2 }, { "ColName" : "IdUpdateDemo", "TotalDocument" : 3 }, { "ColName" : "IndexingDemo", "TotalDocument" : 1 }, { "ColName" : "NotAndDemo", "TotalDocument" : 5 }, { "ColName" : "ProductsInformation", "TotalDocument" : 2 }, { "ColName" : "addCurrentDateTimeDemo", "TotalDocument" : 4 }, { "ColName" : "addFieldDemo", "TotalDocument" : 1 }, { "ColName" : "addNewFieldToEveryDocument", "TotalDocument" : 4 }, { "ColName" : "addingNewPropertyDemo", "TotalDocument" : 3 }, { "ColName" : "aggregateSumDemo", "TotalDocument" : 4 }, { "ColName" : "aggregationFrameworkWithOrMatchDemo", "TotalDocument" : 6 }, { "ColName" : "aggregationSortDemo", "TotalDocument" : 6 }, { "ColName" : "andOrDemo", "TotalDocument" : 2 }, { "ColName" : "arrayInnerElementsDemo", "TotalDocument" : 1 }, { "ColName" : "arrayLengthGreaterThanOne", "TotalDocument" : 3 }, { "ColName" : "arrayOfArraysDemo", "TotalDocument" : 2 }, { "ColName" : "averageAggregationDemo", "TotalDocument" : 5 }, { "ColName" : "avoidDuplicateEntriesDemo", "TotalDocument" : 2 }, { "ColName" : "caseInsensitiveDemo", "TotalDocument" : 13 }, { "ColName" : "caseInsesitiveDemo", "TotalDocument" : 3 }, { "ColName" : "castingDemo", "TotalDocument" : 6 }, { "ColName" : "changeDataType", "TotalDocument" : 1 }, { "ColName" : "changeIntegerToStringDemo", "TotalDocument" : 4 }, { "ColName" : "changeType", "TotalDocument" : 1 }, { "ColName" : "charactersAllowedDemo", "TotalDocument" : 2 }, { "ColName" : "charactersDemo", "TotalDocument" : 3 }, { "ColName" : "check", "TotalDocument" : 1 }, { "ColName" : "checkDemo", "TotalDocument" : 2 }, { "ColName" : "checkFieldContainsStringDemo", "TotalDocument" : 6 }, { "ColName" : "checkFieldExistsOrNotDemo", "TotalDocument" : 4 }, { "ColName" : "checkIfFieldIsNumberDemo", "TotalDocument" : 4 }, { "ColName" : "checkSequenceDemo", "TotalDocument" : 3 }, { "ColName" : "cloneObjectDemo", "TotalDocument" : 5 }, { "ColName" : "collectionOnDifferentDocumentDemo", "TotalDocument" : 3 }, { "ColName" : "combinationOfArrayDemo", "TotalDocument" : 3 }, { "ColName" : "comparingFieldDemo", "TotalDocument" : 4 }, { "ColName" : "comparingTwoFieldsDemo", "TotalDocument" : 4 }, { "ColName" : "componentOfDateDemo", "TotalDocument" : 1 }, { "ColName" : "concatStringAndIntDemo", "TotalDocument" : 5 }, { "ColName" : "concatenateStringsDemo", "TotalDocument" : 5 }, { "ColName" : "conditionalSumDemo", "TotalDocument" : 8 }, { "ColName" : "conditionalUpdatesDemo", "TotalDocument" : 3 }, { "ColName" : "conditionallyUpsertDemo", "TotalDocument" : 3 }, { "ColName" : "convertStringToNumberDemo", "TotalDocument" : 3 }, { "ColName" : "convertingISODateToNumerical", "TotalDocument" : 3 }, { "ColName" : "copyThisCollectionToSampleDatabaseDemo", "TotalDocument" : 3 }, { "ColName" : "countDemo", "TotalDocument" : 4 }, { "ColName" : "countNumberOfDocumentsDemo", "TotalDocument" : 3 }, { "ColName" : "countPerformanceDemo", "TotalDocument" : 7 }, { "ColName" : "createSequenceDemo", "TotalDocument" : 1 }, { "ColName" : "creatingUniqueIndexDemo", "TotalDocument" : 3 }, { "ColName" : "dateDemo", "TotalDocument" : 1 }, { "ColName" : "dateTimeValueLessThanNowDemo", "TotalDocument" : 4 }, { "ColName" : "deleteAllElementsInArrayDemo", "TotalDocument" : 2 }, { "ColName" : "deleteDocumentsDemo", "TotalDocument" : 2 }, { "ColName" : "deleteFieldDemo", "TotalDocument" : 3 }, { "ColName" : "deleteRecordDemo", "TotalDocument" : 4 }, { "ColName" : "deletingDocumentDemo", "TotalDocument" : 3 }, { "ColName" : "demo.insertCollection", "TotalDocument" : 1 }, { "ColName" : "determineFieldExistsDemo", "TotalDocument" : 4 }, { "ColName" : "distinctAggregation", "TotalDocument" : 3 }, { "ColName" : "distinctCountValuesDemo", "TotalDocument" : 2 }, { "ColName" : "distinctRecordDemo", "TotalDocument" : 7 }, { "ColName" : "distinctWithMultipleKeysDemo", "TotalDocument" : 5 }, { "ColName" : "doubleNestedArrayDemo", "TotalDocument" : 2 }, { "ColName" : "emailUniqueDemo", "TotalDocument" : 2 }, { "ColName" : "embeddedCollectionDemo", "TotalDocument" : 0 }, { "ColName" : "embeddedDocumentDemo", "TotalDocument" : 0 }, { "ColName" : "employeeInformation", "TotalDocument" : 5 }, { "ColName" : "equivalentForSelectColumn1Column2Demo", "TotalDocument" : 3 }, { "ColName" : "exactMatchArrayDemo", "TotalDocument" : 2 }, { "ColName" : "existsDemo", "TotalDocument" : 1 }, { "ColName" : "existsReturnDemo", "TotalDocument" : 1 }, { "ColName" : "extractSubArrayDemo", "TotalDocument" : 1 }, { "ColName" : "fieldIsNullOrNotSetDemo", "TotalDocument" : 5 }, { "ColName" : "filterArray", "TotalDocument" : 1 }, { "ColName" : "findADocumentWithCondition", "TotalDocument" : 1 }, { "ColName" : "findAllDuplicateKeyDocumentDemo", "TotalDocument" : 7 }, { "ColName" : "findAllNonDistinctDemo", "TotalDocument" : 8 }, { "ColName" : "findByFieldName", "TotalDocument" : 2 }, { "ColName" : "findByMultipleArrayDemo", "TotalDocument" : 4 }, { "ColName" : "findDocumentDoNotHaveCertainFields", "TotalDocument" : 3 }, { "ColName" : "findDocumentNonExistenceFieldDemo", "TotalDocument" : 2 }, { "ColName" : "findDocumentWithObjectIdDemo", "TotalDocument" : 3 }, { "ColName" : "findDuplicateByKeyDemo", "TotalDocument" : 6 }, { "ColName" : "findDuplicateRecordsDemo", "TotalDocument" : 6 }, { "ColName" : "findMinValueDemo", "TotalDocument" : 5 }, { "ColName" : "findMultipleWordsDemo", "TotalDocument" : 6 }, { "ColName" : "findRecordByIdDemo", "TotalDocument" : 4 }, { "ColName" : "findSpecificValue", "TotalDocument" : 2 }, { "ColName" : "findValueInArrayWithMultipleCriteriaDemo", "TotalDocument" : 2 }, { "ColName" : "firstDocumentDemo", "TotalDocument" : 3 }, { "ColName" : "firstElementOfArray", "TotalDocument" : 2 }, { "ColName" : "firstItemInArrayToNewFieldDemo", "TotalDocument" : 2 }, { "ColName" : "firstLevelArrayDemo", "TotalDocument" : 2 }, { "ColName" : "flightInformation", "TotalDocument" : 2 }, { "ColName" : "foo2", "TotalDocument" : 2 }, { "ColName" : "getAllValue", "TotalDocument" : 2 }, { "ColName" : "getDistinctListOfSubDocumentFieldDemo", "TotalDocument" : 3 }, { "ColName" : "getDistinctWithSortedDataDemo", "TotalDocument" : 7 }, { "ColName" : "getDocuementsByTagsDemo", "TotalDocument" : 3 }, { "ColName" : "getError", "TotalDocument" : 1 }, { "ColName" : "getFirstElementInArrayDemo", "TotalDocument" : 3 }, { "ColName" : "getFirstItemDemo", "TotalDocument" : 4 }, { "ColName" : "getIndexDemo", "TotalDocument" : 1 }, { "ColName" : "getIndexSizeDemo", "TotalDocument" : 4 }, { "ColName" : "getLastNRecordsDemo", "TotalDocument" : 11 }, { "ColName" : "getLastXRecordsDemo", "TotalDocument" : 11 }, { "ColName" : "getNThElementDemo", "TotalDocument" : 2 }, { "ColName" : "getOldestAndYoungestPostDemo", "TotalDocument" : 4 }, { "ColName" : "getParticularElementFromArrayDemo", "TotalDocument" : 2 }, { "ColName" : "getPartuclarElement", "TotalDocument" : 1 }, { "ColName" : "getSizeDemo", "TotalDocument" : 1 }, { "ColName" : "getSizeOfArray", "TotalDocument" : 3 }, { "ColName" : "gettingHighestValueDemo", "TotalDocument" : 9 }, { "ColName" : "groupByDateDemo", "TotalDocument" : 6 }, { "ColName" : "handlingAndEmptyDataDemo", "TotalDocument" : 3 }, { "ColName" : "hashDemo", "TotalDocument" : 2 }, { "ColName" : "hideidDemo", "TotalDocument" : 3 }, { "ColName" : "highestNumericValueDemo", "TotalDocument" : 1 }, { "ColName" : "highestNumericValueOfAColumnDemo", "TotalDocument" : 4 }, { "ColName" : "identifyLastDocuementDemo", "TotalDocument" : 6 }, { "ColName" : "improveQueryDemo", "TotalDocument" : 1 }, { "ColName" : "incrementInNestedArrayDemo", "TotalDocument" : 1 }, { "ColName" : "incrementValueDemo", "TotalDocument" : 1 }, { "ColName" : "incrementValueInNestedArrayDemo", "TotalDocument" : 0 }, { "ColName" : "indexDemo", "TotalDocument" : 3 }, { "ColName" : "indexOptimizationDemo", "TotalDocument" : 3 }, { "ColName" : "indexOrQueryDemo", "TotalDocument" : 0 }, { "ColName" : "indexTimeDemo", "TotalDocument" : 0 }, { "ColName" : "index_Demo", "TotalDocument" : 2 }, { "ColName" : "indexingDemo", "TotalDocument" : 3 }, { "ColName" : "insertDemo", "TotalDocument" : 1 }, { "ColName" : "insertFieldWithCurrentDateDemo", "TotalDocument" : 3 }, { "ColName" : "insertIfNotExistsDemo", "TotalDocument" : 4 }, { "ColName" : "insertIntegerDemo", "TotalDocument" : 3 }, { "ColName" : "insertOneRecordDemo", "TotalDocument" : 3 }, { "ColName" : "likeDemo", "TotalDocument" : 5 }, { "ColName" : "limitAnArrayDemo", "TotalDocument" : 1 }, { "ColName" : "listAllValuesOfCeratinFieldsDemo", "TotalDocument" : 4 }, { "ColName" : "loopThroughCollectionDemo", "TotalDocument" : 4 }, { "ColName" : "matchBetweenFieldsDemo", "TotalDocument" : 2 }, { "ColName" : "mongoExportDemo", "TotalDocument" : 1 }, { "ColName" : "multipleElementMatchDemo", "TotalDocument" : 2 }, { "ColName" : "multipleIndexesDemo", "TotalDocument" : 0 }, { "ColName" : "multipleOrDemo", "TotalDocument" : 4 }, { "ColName" : "my-collection", "TotalDocument" : 1 }, { "ColName" : "nanDemo", "TotalDocument" : 4 }, { "ColName" : "nestedArrayDemo", "TotalDocument" : 3 }, { "ColName" : "nestedIndexDemo", "TotalDocument" : 1 }, { "ColName" : "nestedObjectDemo", "TotalDocument" : 1 }, { "ColName" : "nestedQueryDemo", "TotalDocument" : 2 }, { "ColName" : "newProperty", "TotalDocument" : 2 }, { "ColName" : "new_Collection", "TotalDocument" : 3 }, { "ColName" : "noAutoCommandForIndexDemo", "TotalDocument" : 4 }, { "ColName" : "notEqaulToDemo", "TotalDocument" : 4 }, { "ColName" : "notEqaultToDemo", "TotalDocument" : 0 }, { "ColName" : "notLikeOpeartorDemo", "TotalDocument" : 5 }, { "ColName" : "numberofKeysInADocumentDemo", "TotalDocument" : 1 }, { "ColName" : "objectInAnArrayDemo", "TotalDocument" : 2 }, { "ColName" : "objectidToStringDemo", "TotalDocument" : 4 }, { "ColName" : "onlyInsertIfValueIsUniqueDemo", "TotalDocument" : 4 }, { "ColName" : "orConditionDemo", "TotalDocument" : 6 }, { "ColName" : "orDemo", "TotalDocument" : 6 }, { "ColName" : "orderDocsDemo", "TotalDocument" : 7 }, { "ColName" : "paginationDemo", "TotalDocument" : 5 }, { "ColName" : "performRegex", "TotalDocument" : 2 }, { "ColName" : "preventObjectIdDemo", "TotalDocument" : 1 }, { "ColName" : "priceStoredAsStringDemo", "TotalDocument" : 8 }, { "ColName" : "priceStoredDemo", "TotalDocument" : 7 }, { "ColName" : "pullAndAddToSetDemo", "TotalDocument" : 1 }, { "ColName" : "pushObjectDemo", "TotalDocument" : 1 }, { "ColName" : "queryArrayElementsDemo", "TotalDocument" : 3 }, { "ColName" : "queryByKeyDemo", "TotalDocument" : 1 }, { "ColName" : "queryBySubFieldDemo", "TotalDocument" : 2 }, { "ColName" : "queryForBooleanFieldsDemo", "TotalDocument" : 5 }, { "ColName" : "queryInSameDocumentsDemo", "TotalDocument" : 2 }, { "ColName" : "queryLengthDemo", "TotalDocument" : 5 }, { "ColName" : "queryToEmbeddedDocument", "TotalDocument" : 1 }, { "ColName" : "queryingMongoDbCaseInsensitiveDemo", "TotalDocument" : 4 }, { "ColName" : "readSpecificKeyValueDemo", "TotalDocument" : 1 }, { "ColName" : "regExpOnIntegerDemo", "TotalDocument" : 5 }, { "ColName" : "regexSearchDemo", "TotalDocument" : 5 }, { "ColName" : "removeAllDocumentsExceptOneDemo", "TotalDocument" : 1 }, { "ColName" : "removeArrayDemo", "TotalDocument" : 1 }, { "ColName" : "removeArrayElement", "TotalDocument" : 1 }, { "ColName" : "removeArrayElementByItsIndexDemo", "TotalDocument" : 1 }, { "ColName" : "removeArrayElements", "TotalDocument" : 1 }, { "ColName" : "removeDocumentOnBasisOfId", "TotalDocument" : 4 }, { "ColName" : "removeDocumentsExceptOneFromCollection", "TotalDocument" : 1 }, { "ColName" : "removeDuplicateDocumentDemo", "TotalDocument" : 4 }, { "ColName" : "removeDuplicateDocuments", "TotalDocument" : 3 }, { "ColName" : "removeDuplicatesDemo", "TotalDocument" : 1 }, { "ColName" : "removeElementFromDoublyNestedArrayDemo", "TotalDocument" : 2 }, { "ColName" : "removeFieldCompletlyDemo", "TotalDocument" : 3 }, { "ColName" : "removeIdDemo", "TotalDocument" : 4 }, { "ColName" : "removeMultipleDocumentsDemo", "TotalDocument" : 3 }, { "ColName" : "removeObject", "TotalDocument" : 1 }, { "ColName" : "removeRecordDemo", "TotalDocument" : 5 }, { "ColName" : "removingidElementDemo", "TotalDocument" : 5 }, { "ColName" : "renameFieldDemo", "TotalDocument" : 5 }, { "ColName" : "renameIdDemo", "TotalDocument" : 3 }, { "ColName" : "renamingColumnNameDemo", "TotalDocument" : 3 }, { "ColName" : "replaceSpecificArrayValueDemo", "TotalDocument" : 1 }, { "ColName" : "retrieveDocumentsWithEndsWithParticularCharacterDemo", "TotalDocument" : 5 }, { "ColName" : "retrieveFirstDocumentDemo", "TotalDocument" : 4 }, { "ColName" : "retrieveValueFromAKeyDemo", "TotalDocument" : 3 }, { "ColName" : "retunFieldInFindDemo", "TotalDocument" : 4 }, { "ColName" : "returnOnlyValueOfFieldDemo", "TotalDocument" : 4 }, { "ColName" : "returnQueryFromDate", "TotalDocument" : 4 }, { "ColName" : "reverseRegexDemo", "TotalDocument" : 2 }, { "ColName" : "s", "TotalDocument" : 2 }, { "ColName" : "saveStudentInformation", "TotalDocument" : 4 }, { "ColName" : "searchArrayDemo", "TotalDocument" : 2 }, { "ColName" : "searchByPropertyName", "TotalDocument" : 5 }, { "ColName" : "searchDocumentDemo", "TotalDocument" : 10 }, { "ColName" : "searchDocumentWithSpecialCharactersDemo", "TotalDocument" : 4 }, { "ColName" : "searchForNestedKeysDemo", "TotalDocument" : 3 }, { "ColName" : "searchMultipleFieldsDemo", "TotalDocument" : 3 }, { "ColName" : "secondDocumentDemo", "TotalDocument" : 3 }, { "ColName" : "selectFieldAsAnotherNameDemo", "TotalDocument" : 4 }, { "ColName" : "selectInWhereIdDemo", "TotalDocument" : 6 }, { "ColName" : "selectMongoDBDocumentsWithSomeCondition", "TotalDocument" : 7 }, { "ColName" : "selectRecordsHavingKeyDemo", "TotalDocument" : 4 }, { "ColName" : "selectSingleFieldDemo", "TotalDocument" : 12 }, { "ColName" : "setMaxDemo", "TotalDocument" : 0 }, { "ColName" : "singleFieldDemo", "TotalDocument" : 4 }, { "ColName" : "singlePropertyIdDemo", "TotalDocument" : 5 }, { "ColName" : "sortDemo", "TotalDocument" : 6 }, { "ColName" : "sortInnerArrayDemo", "TotalDocument" : 1 }, { "ColName" : "sortingDemo", "TotalDocument" : 8 }, { "ColName" : "sourceCollection", "TotalDocument" : 2 }, { "ColName" : "sqlLikeDemo", "TotalDocument" : 7 }, { "ColName" : "storeQueryResultDemo", "TotalDocument" : 4 }, { "ColName" : "stringFieldLengthDemo", "TotalDocument" : 7 }, { "ColName" : "stringToDateDataTypeDemo", "TotalDocument" : 3 }, { "ColName" : "stringToObjectIdDemo", "TotalDocument" : 1 }, { "ColName" : "subsetOfAnArrayDemo", "TotalDocument" : 1 }, { "ColName" : "test.js", "TotalDocument" : 1 }, { "ColName" : "toLowerDemo", "TotalDocument" : 4 }, { "ColName" : "topLevelDocumentDemo", "TotalDocument" : 1 }, { "ColName" : "translateDefinitionDemo", "TotalDocument" : 5 }, { "ColName" : "twoRandomDocumentDemo", "TotalDocument" : 6 }, { "ColName" : "twoSeparateArraysDemo", "TotalDocument" : 3 }, { "ColName" : "unconditionalUpdatesDemo", "TotalDocument" : 4 }, { "ColName" : "uniqueArrayDemo", "TotalDocument" : 4 }, { "ColName" : "uniqueIndexOnArrayDemo", "TotalDocument" : 3 }, { "ColName" : "unprettyJsonDemo", "TotalDocument" : 2 }, { "ColName" : "unsetAnAttributeDemo", "TotalDocument" : 1 }, { "ColName" : "unwindOperatorDemo", "TotalDocument" : 1 }, { "ColName" : "updateDemo", "TotalDocument" : 3 }, { "ColName" : "updateExactField", "TotalDocument" : 1 }, { "ColName" : "updateIdDemo", "TotalDocument" : 3 }, { "ColName" : "updateListOfKeyValuesDemo", "TotalDocument" : 1 }, { "ColName" : "updateManyDocumentsDemo", "TotalDocument" : 4 }, { "ColName" : "updateNestedValueDemo", "TotalDocument" : 1 }, { "ColName" : "updateObjects", "TotalDocument" : 1 }, { "ColName" : "updateOnlySpecificFieldDemo", "TotalDocument" : 3 }, { "ColName" : "updatingEmbeddedDocumentPropertyDemo", "TotalDocument" : 2 }, { "ColName" : "userStatus", "TotalDocument" : 5 } ]