gpt4 book ai didi

node.js - 蒙哥错误: must have $meta projection for all $meta sort keys using Mongo DB Native NodeJS Driver

转载 作者:可可西里 更新时间:2023-11-01 09:20:38 27 4
gpt4 key购买 nike

直接在 MongoDB 上运行以下文本搜索不会产生任何问题:

db.getCollection('schools').find({
$text:
{
$search: 'some query string',
$caseSensitive: false,
$diacriticSensitive: true
}
}, {score: {$meta: "textScore"}}).sort({score:{$meta:"textScore"}})

然而,当尝试使用 native NodeJS driver 运行相同的查询时:

function getSchools(filter) {
return new Promise(function (resolve, reject) {

MongoClient.connect('mongodb://localhost:60001', function(err, client) {
const collection = client.db('schools').collection('schools');

collection.find({
$text:
{
$search: filter,
$caseSensitive: false,
$diacriticSensitive: true
}
}, {score: {$meta: "textScore"}}).sort({score:{$meta:"textScore"}}).toArray(function(err, docs) {
if (err) return reject(err);

resolve(docs);
});
});
});
}

我收到以下错误:

MongoError: must have $meta projection for all $meta sort keys

我在这里做错了什么?

最佳答案

好的,根据this bug自版本 3.0.0 findfindOne no longer support fields 参数和查询需要重写如下:

collection.find({
$text:
{
$search: filter,
$caseSensitive: false,
$diacriticSensitive: true
}
})
.project({ score: { $meta: "textScore" } })
.sort({score:{$meta:"textScore"}})

关于node.js - 蒙哥错误: must have $meta projection for all $meta sort keys using Mongo DB Native NodeJS Driver,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48975707/

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