在mongo数据库投影中的聚合查询操作

我正在研究searchfunction。 我有一个对象包含键值对中的数据,其中键是对象的ID,值是我用一些后台进程计算的分数。 所以我在search数据期间的情况下,我必须添加新的分数之前计数的分数。

我正在为我的search操作使用聚合。 以下是我的代码片段。

文件


{ "_id": ObjectId("57de00eb0b1e50fa66290198"), "id": "205332", "Title": "The relationship between bispectral index values and age in children", "title": "Bispectral index values during general anesthesia in children", "OutcomesAvailable": "No", "DateStart": "2011-03-10T00:00:00-06:00", "DateEnd": { "type": "actual", "value": "2012-05-01T00:00:00-05:00" }, "DateChangeLast": "2014-07-21T11:39:54-05:00", "gender": "N/A", "highAge": NumberInt(12), "lowAge": 0.5, "recordType": "xxxxxxxx", "__v": NumberInt(0) } Object containing key as document id and value as score ------------------------------------------------------- var textSearch = 'cancer'; var test_ids = { '277313', '278237', '278356', '278547' } scoresArr = { '277313': '79.06410256410257', '278237': '65.27777777777777', '278356': '66.83928571428572', '278547': '66.8051948051948' } 

我的查询,我想在新的search分数上添加上述对象的分数。

 var aggregation = TestCollection.aggregate( [{ $match: { $and: [ { "id": { $in : test_ids } }, { $text: {$search: textSearch} } ] } }, { $sort: { score: { $meta: "textScore" } } }, { $project: { id: 1, scoreText: { $meta: "textScore" }, score: { $add: [{ $cond: ["$scoreText", { $meta: "textScore" }, { $meta: "textScore" }] }, { $cond: [{ $gte: [scoresArr["$id"], 0] }, scoresArr["$id"], scoresArr["$id"] ]}, ] } } }, { $sort: { score: -1 } } ] ); aggregation.exec(function(err, data) { // here you will get output }); 

我得分为零。 我知道我正在做一个错误的方式。 请帮我解决这个问题。

谢谢