我正在使用mongodb native 驱动来获取查询结果的光标。 我正在使用“评论”字段,它基本上是一个字符串,下面是我正在使用的代码片段
let leve1_n = 'labreports';
let matchLabReports = { level1_n: "labreports" };
let sortCondition = { _id: 1 };
let projection = {comment : 1 , color: 1, value: 1, value_n: 1, value_d: 1, rangeStart: 1, rangeEnd: 1, level2: 1 };
let collectionName = collectionNames.biomarkerAggregations;
let cursor = dbModule.findCursorSort(dbModule.getDbHandle(dbName), collectionName, matchLabReports, projection,sortCondition);
cursor.forEach(doc => {
// Lambda Expression
// Do something with doc.
})
查找光标排序
function findCursorSort(db, collectionName, query, projection, sortCondition, options) {
if (!errorCheck(db)) {
let cursor = db.collection(collectionName).find(query, projection).sort(sortCondition);
if (!options) {
return cursor;
}
for (let i = 0; i < options.length; i++) {
cursor = cursor.addOption(options[i]);
}
return cursor;
}
}
但这给了我这样的错误..
name: 'MongoError', message: 'Failed to parse: comment: 1. \'comment\' field must be of BSON type string.', ok: 0, errmsg: 'Failed to parse: comment: 1. \'comment\' field must be of BSON type string.', code: 9, codeName: 'FailedToParse' }
从项目中删除评论字段后,一切正常。 任何帮助将不胜感激。
最佳答案
mongodb原生驱动的collection.js中有一个功能,可以检查哪些字段是查询选项,哪些字段是投影。如果投影中的所有字段包含注释、限制、排序等字段(写在 testForFields 变量中),则它们将作为选项。
尝试像这样解决这个问题:
原始代码:
db.collection(collectionName).find(query, projection)
固定代码:
db.collection(collectionName).find(query).project(projection)
它帮助我解决了问题。
关于javascript - 无法解析 : comment: 1.\'comment\' 字段必须是 BSON 类型字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44255786/