簡體   English   中英

如何使用 mongoose's.find() 通過在模式中找到的數組中搜索名稱或項目來返回用戶?

[英]How do I use mongoose's .find() to get a user returned by searching for a name or an item in an array that is found in a schema?

我正在嘗試為現有 node.js 應用程序的用戶搜索功能添加額外的功能,但我仍在了解 node.js、mongoDB161CB758D322FFC251 我厭倦了添加一個額外的索引,但它沒有用,我認為我的問題可能出在.find() function 中。 我有以下架構:

var userSchema = mongoose.Schema({
    name: String,
    fluentIn: [
        {
            code: String,
            English: String, // English name for the language
        },
    ],
    learning: [
        {
            code: String,
            English: String, // English name for the language
        },
    ]
});

userSchema.index({
    name: "text",
    "fluentIn.English": 1,
});

我想要的功能是讓搜索查詢能夠成為用戶的名稱或他們精通的一種語言的名稱,並返回該用戶。 目前,我正在 node.js 應用程序中使用以下 function :

    search(searchQuery, callback) {
        userModel.find(
            { $text: { $search: searchQuery } },
            function (err, result) {
                if (err) {
                    console.log(err);
                } else {
                    callback(result);
                }
            }
        );
    },

我最終找到了一個有效的查詢。 我更換了:

{ $text: { $search: searchQuery } }

和:

{$or:[{name: searchQuery},{"fluentIn.English": searchQuery }]}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM