這是我的代碼,它在“subs”集合中的所有檔案中搜索“test”一詞并回傳它們。問題是我只需要兩個特定欄位(id 和 name)。
app.get('/', (req, res) => {
db.collection('subs')
.find({
$text: { $search: 'test' },
})
.toArray((err, result) => {
if (err) {
throw new err();
}
res.json({
length: result.length,
body: { result },
});
});
});
uj5u.com熱心網友回復:
所以,你可以使用的投影:
db.collection('subs').find({$text: { $search: 'test' }}, {name: 1 } )。
在此處閱讀更多相關資訊:https : //docs.mongodb.com/manual/tutorial/project-fields-from-query-results/#return-the-specified-fields-and-the-_id-field-only
uj5u.com熱心網友回復:
您可以在find方法的附加引數中設定您需要的欄位:
db.collection('subs').find({
$text: { $search: 'test' }
},
{
name: 1,
otherColumn: 1
}); // select only the "name" & the "otherColumn" column
_id默認情況下始終回傳該列,但您可以通過添加_id: 0.
希望這能解決您的問題。
uj5u.com熱心網友回復:
我終于找到了答案!:
.find(
{
name: { $in: ['Prison Break', 'Dexter'] },
$text: { $search: 'kill' },
},
{
projection: { name: 1 },
}
)
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/368040.html
標籤:MongoDB
