gpt4 book ai didi

javascript - Failed to parse : comment: 1.\'comment\' 字段必须是BSON类型的字符串

转载 作者:可可西里 更新时间:2023-11-01 10:15:34 26 4
gpt4 key购买 nike

我正在使用 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 native driver的collection.js中有一个特性,就是检查哪些字段是query的options,哪些是projection。如果投影中的所有字段都包含评论、限制、排序等字段(写在 testForFields 变量中),则它们将作为选项。

尝试像这样解决这个问题:

原代码:

db.collection(collectionName).find(query, projection)

固定代码:

db.collection(collectionName).find(query).project(projection)

它帮助我解决了问题。

关于javascript - Failed to parse : comment: 1.\'comment\' 字段必须是BSON类型的字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44255786/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com