【问题标题】:How to search all keys inside MongoDB collection using only one keyword如何仅使用一个关键字搜索 MongoDB 集合中的所有键
【发布时间】:2017-05-16 10:56:44
【问题描述】:

有没有办法让 MongoDB 只使用一个搜索关键字来搜索整个集合的键的内容?

假设我有以下集合(我们称之为 foodCollection):

{    
    name: "Chocolate Mousse Cake", 
    type: "Cake" 
},
{
    name: "Mother's Cookies", 
    type: "Cookies" 
},
{
    name: "Dark Bar", 
    type: "Chocolate" 
}

我希望我的搜索查找包含“Chocolate”的匹配项,这意味着它应该返回“Chocolate Mousse Cake”和“Dark Ba​​r”。

我正在尝试使用 ff: 代码:

客户端控制器

// Search Products
$scope.searchProduct = function () {

    $http.get('/api/products/search/' + $scope.searchKeyword).success(function(data){
        console.log(data);
    })
    .error(function(err) {
        console.log("Search error: " + err);
    });
 }

Express.js

app.get('/api/products/search/:param', productController.search); // Search for product

服务器端控制器(我使用了 MongoDB 文档中的 this reference):

// Search
module.exports.search = function(req, res) {

    console.log("node search: " + req.body);

    Product.find({ $or: [{productName: req.body}, 
                         {productType: req.body}]
                 }, function(err, results) {
                        res.json(results);
    });
}

当我执行此操作时,我一无所获。我错过了什么吗?

任何帮助将不胜感激。谢谢。

更新(最终)

感谢 Joydip 和 digit 的提示,终于解决了这个问题。这是我的解决方案,以防其他人遇到与我相同的问题:

客户端控制器

$scope.searchProduct = function () {

    if ($scope.searchKeyword == '') {
        loadFromMongoDB();    // reloads original list if keyword is blank
    }
    else {
        $http.get('/api/products/search/' + $scope.searchKeyword).success(function(data){

            if (data.length === 0) {
                $scope.showNoRec = true; // my flag that triggers "No record found" message in UI
            }
            else {
                $scope.showNoRec = false;
                $scope.productList = data; // passes JSON search results to UI
            }
        });    
    }
}

Express.js

app.get('/api/products/search/:keyword', productController.search);   // Search for product

Mongoose 架构

var mongoose = require('mongoose');

var schema = new mongoose.Schema({
    productName: String,
    productType: String,
    productMaker: String,
    productPrice: Number,
    createDate: Date,
    updateDate: Date
});
schema.index({productName: "text", productType: "text", productMaker: "text"});

服务器端控制器

module.exports.search = function(req, res) {

    Product.find({$text: {$search : req.params.keyword}}, function(err, results){
        res.json(results);
    })
}

感谢大家的帮助。 :)

【问题讨论】:

  • 顺便说一句,我认为您想要 req.params.param 而不是 req.body 作为您的搜索值。
  • 感谢您的回复。是的,这也是我的错误之一。 :)) 更正它使我的搜索功能完美运行。

标签: javascript mongodb mongoose mean-stack


【解决方案1】:

如果你想搜索所有键,那么你可以使用

db.foodCollection.createIndex( { name: "text", description: "text" } )

然后搜索

    db.foodCollection.find({ $text: { $search: "choco" } })

【讨论】:

  • 这对我有用,但我必须像这样创建索引:schema.index({name: "text", description: "text"});。现在我只需要考虑不区分大小写的正则表达式。谢谢!
  • P.S. 原来这也解决了不区分大小写的搜索。一石两鸟!你是救生员。谢谢!
【解决方案2】:

您可以尝试创建一个索引:

db.yourollection.createIndex({"productName":1,"productType":1})

然后通过搜索值,示例:

Product.find({$text:{$search: 'Chocolate'}},{productName:1, productType:1});

【讨论】:

  • 感谢您的回复。这是我第一次看到createIndex。它有什么作用? Product.find({$text:{$search: 'Chocolate'}},{productName:1, productType:1}); 是否意味着无论键名如何,它都会返回所有匹配的记录?
  • 嗨.. 索引以升序 (1) 或降序 (-1) 排序顺序存储对字段的引用。正如我上面的示例是存储对该字段的引用的复合索引。创建索引后,您可以在查询中使用它。
猜你喜欢
  • 1970-01-01
  • 2014-02-18
  • 2018-03-07
  • 2015-09-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-11-28
  • 1970-01-01
相关资源
最近更新 更多