查询和排序集合MongoDB Stitch
我正在使用react native和mongoDB stitch。 我的意图是,当我使用关键字运行查询时,结果应该按与关键字最匹配的排序 例如,如果我搜索Aqua, 结果应按以下顺序排序:查询和排序集合MongoDB Stitch,mongodb,sorting,find,meta,mongodb-stitch,Mongodb,Sorting,Find,Meta,Mongodb Stitch,我正在使用react native和mongoDB stitch。 我的意图是,当我使用关键字运行查询时,结果应该按与关键字最匹配的排序 例如,如果我搜索Aqua, 结果应按以下顺序排序: 水族 水族 水族馆 水提取物 水 等等 我找到了此()的文档 它崩溃了,说“未知操作员$meta”。在mongdb stitch文档中未找到任何示例 有什么建议吗 它崩溃了,说“未知操作员$meta”。在mongodb stitch文档中未找到任何示例 有几种方法可以做到这一点。其中一种方法是创建一个可以
- 水族
- 水族
- 水族馆
- 水提取物
- 水
- 等等
exports = function(word){
var coll = context.services.get("mongodb-atlas").db("dbName").collection("collName");
var doc = coll.find(
{"$text": {"$search": word}},
{"score": {"$meta": "textScore"}}
).sort(
{"score":{"$meta":"textScore"}}
).toArray();
doc.forEach(element => console.log(JSON.stringify(element)));
return doc;
};
上面的函数应该打印出与下面类似的内容,并以格式返回数组
请注意,要执行查询,您必须在集合上具有
const query = {
name: {
$regex: searchKeyword,
$options: 'i',
//"$meta": "textScore"
},
score: { "$meta": "textScore" } // not sure where to put it , Saying unknown operator $meta
};
const options = {
"sort": { "score": { $meta: "textScore" }}
};
db.collection(itemNameDB).find( query, options).toArray()
.then(results => {
console.log(results)
})
exports = function(word){
var coll = context.services.get("mongodb-atlas").db("dbName").collection("collName");
var doc = coll.find(
{"$text": {"$search": word}},
{"score": {"$meta": "textScore"}}
).sort(
{"score":{"$meta":"textScore"}}
).toArray();
doc.forEach(element => console.log(JSON.stringify(element)));
return doc;
};
{"_id":"5e262bf99514bb2d81bb8735","item":"Aqua","score":1.1}
{"_id":"5e262c009514bb2d81bb8736","item":"Aquae","score":1}
{"_id":"5e262c109514bb2d81bb8739","item":"Aqua-water","score":0.75}