[英]Mongoose countDocuments not returning as expected
I am attempting to write a function to find if a document within a mongoose database exists before submitting a query.我正在尝试编写一个函数来在提交查询之前查找 mongoose 数据库中的文档是否存在。 However, it keeps saying that the document does not exist, even though it does.
但是,它一直说该文档不存在,即使它存在。
/**
* Determines if a Journal already exists (via ISSN numer)
* @param {string} data - The ISSN number of the Journal to be checked
* @return {boolean} - True = journal exists, false it doesnt exist.
*/
async function getJournalByISSN(data) {
const issn = String(data);
const docCount = await Journal.countDocuments().or([
{issn_electronic: issn},
{issn_print: issn}]);
let value = false;
console.log(docCount, value);
if (docCount != 0) value = true;
return value;
}
The console.log of the data given returns a correctly formatted ISSN.给定数据的 console.log 返回格式正确的 ISSN。 When I debug the mongoose query it looks well-formed.
当我调试猫鼬查询时,它看起来格式良好。
console.log控制台日志
journals.countDocuments {"$or":[{"issn_electronic":"1234-1234"},{"issn_print":"1234-1234"}]} {}
When manually specifying the search field, ie手动指定搜索字段时,即
{issn_electronic: '1234-1234'}, {issn_print: '1234-1234'}]}
It returns the correct document count value.它返回正确的文档计数值。
Interestingly copying and pasting the debug of mongoose shows that there has been blank spaces injected into the string有趣的是复制粘贴mongoose的debug,发现字符串中注入了空格
eg例如
journals.countDocuments {"$or":[{"issn_electronic":"1234- 1234"},{"issn_print":"1234- 1234"}]} {}
This was an issue with hidden characters这是隐藏字符的问题
String has hidden characters, Can't able to verify , 字符串有隐藏字符,无法验证,
By replacing the data string via it solved the issue通过替换数据字符串解决了这个问题
data = data.replace(/[\u200c\u200b]/g, '')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.