我试图从一个小的nodeJS应用程序中使用MongoDB中的聚合方法。我的目标是简单地计算特定键具有值的次数。我不在乎价值是什么。我只想为该密钥计算每个值。
以下是我尝试的内容:
const fs = require("fs");
const MongoClient = require("mongodb").MongoClient;
// Connection URL
const url = 'mongodb://localhost:27017/implant_history_audit_PROD';
// write to our mongo database
MongoClient.connect(url, (err, client) => {
const db = client.db("implant_history_audit_PROD");
db.collection("implants").aggregate([
{
$group: {
_id: "$person_id",
count: {
$sum: 1
}
}
}
], (err, result) => {
result.toArray().then(res => console.log(res), err => console.log(err));
// fs.writeFileSync("patient_counts.txt", JSON.stringify(result));
});
client.close();
});
返回以下内容:[ { _id: null, count: 58059 } ]
以下是每个文档的架构:
{
"IMPLANT_HISTORY_ID":0.000000,
"IMPLANT_ITEM_ID":0.000000,
"IMPLANT_ITEM_FT":"Some history free text",
"IMPLANT_TYPE_CD":0.000000,
"BIOLOGICAL_IMP_SRC_CD":0.000000,
"NON_BIOLOGICAL_IMP_TYPE_CD":0.000000,
"BODY_SITE_CD":0.000000,
"COMMENTS":"This is a comment",
"PERSON_ID":123456.000000,
"ENCNTR_ID":0.000000,
"EXPIRATION_DT_TM":"\/Date(0000-00-00T00:00:00.000+00:00)\/",
"EXPLANT_DT_TM":"\/Date(0000-00-00T00:00:00.000+00:00)\/",
"EXPLANT_DT_TM_PREC_FLAG":0,
"EXPLANT_REASON_CD":0.000000,
"IMPLANTED_FACILITY_CD":0.000000,
"IMPLANTED_FACILITY_FT":"",
"IMPLANTED_PRSNL_ID":0.000000,
"IMPLANTED_PRSNL_FT":"",
"IMPLANTED_DT_TM":"\/Date(1901-01-01T00:00:00.000-04:00)\/",
"IMPLANTED_DT_TM_PREC_FLAG":3,
"LOT_NUMBER_TXT":"",
"MR_CLASSIFICATION_CD":0.000000,
"PARENT_ENTITY_ID":0.000000,
"PARENT_ENTITY_NAME":"",
"PROCEDURE_CD":0.000000,
"PROCEDURE_NOMEN_ID":0.000000,
"PROCEDURE_FT":"",
"IMPLANTED_QUANTITY":1,
"SERIAL_NUMBER_TXT":"",
"UDI_TXT":"",
"DONOR_NUMBER_TXT":"",
"MANUFACTURED_DT_TM":"\/Date(0000-00-00T00:00:00.000+00:00)\/",
"MANUFACTURER_CD":0.000000,
"MANUFACTURER_FT":"",
"MANUFACTURER_MODEL_NBR_TXT":"",
"ACTIVE_IND":1,
"SURG_CASE_PROC_ID":0.000000,
"DEVICE_IDENTIFIER_TXT":""
}
我很困惑,因为每个文件都有一个" person_id"键。感谢您的见解。
修改
我还要提到文档中的键是大写的。但是,使用" $ PERSON_ID"导致脚本失败。我从中得到的错误是:
{MongoNetworkError:连接被破坏,无法实例化游标 at nextFunction(C:\ Users \ cgoepfrich2 \ Documents \ projects \ implant migration \ PROD \ node_modules \ mongodb-core \ lib \ cursor.js:707:9) 在AggregationCursor.Cursor.next(C:\ Users \ cgoepfrich2 \ Documents \ projects \ implant migration \ PROD \ node_modules \ mongodb-core \ lib \ cursor.js:814:3) 在AggregationCursor.Cursor._next(C:\ Users \ cgoepfrich2 \ Documents \ projects \ implant migration \ PROD \ node_modules \ mongodb \ lib \ cursor.js:201:36) at fetchDocs(C:\ Users \ cgoepfrich2 \ Documents \ projects \ implant migration \ PROD \ node_modules \ mongodb \ lib \ cursor.js:950:10) 在C:\ Users \ cgoepfrich2 \ Documents \ projects \ implant migration \ PROD \ node_modules \ mongodb \ lib \ cursor.js:976:7 在handleCallback(C:\ Users \ cgoepfrich2 \ Documents \ projects \ implant migration \ PROD \ node_modules \ mongodb-core \ lib \ cursor.js:199:5) at nextFunction(C:\ Users \ cgoepfrich2 \ Documents \ projects \ implant migration \ PROD \ node_modules \ mongodb-core \ lib \ cursor.js:804:5) 在C:\ Users \ cgoepfrich2 \ Documents \ projects \ implant migration \ PROD \ node_modules \ mongodb-core \ lib \ cursor.js:686:7 在queryCallback(C:\ Users \ cgoepfrich2 \ Documents \ projects \ implant migration \ PROD \ node_modules \ mongodb-core \ lib \ cursor.js:263:16) 在C:\ Users \ cgoepfrich2 \ Documents \ projects \ implant migration \ PROD \ node_modules \ mongodb-core \ lib \ connection \ pool.js:541:18 名称:' MongoNetworkError', 消息:'连接被破坏,无法实例化光标' }