简体   繁体   English

如何仅使用一个关键字搜索MongoDB集合内的所有键

[英]How to search all keys inside MongoDB collection using only one keyword

Is there a way for MongoDB to search an entire collection's keys' contents using only a single search keyword? MongoDB是否可以仅使用一个搜索关键字来搜索整个集合的键的内容?

Suppose I have the following collection (let's call it foodCollection): 假设我有以下集合(我们称它为foodCollection):

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

I want my search to look for matches that contain "Chocolate", meaning it should return "Chocolate Mousse Cake" and "Dark Bar". 我希望搜索查找包含“巧克力”的匹配项,这意味着它应返回“巧克力慕斯蛋糕”和“暗酒吧”。

I'm trying to do this using the ff: code: 我正在尝试使用ff:代码:

Client-side controller 客户端控制器

// 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 Express.js

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

Server-side controller (I used this reference from the MongoDB docs): 服务器端控制器 (我使用了MongoDB文档中的此参考 ):

// 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);
    });
}

When I executed this, I got nothing. 执行此操作时,我一无所获。 Am I missing something? 我想念什么吗?

Any help would be greatly appreciated. 任何帮助将不胜感激。 Thank you. 谢谢。

UPDATE (FINAL) 更新(最终)

Finally solved this thanks to Joydip's and digit's tips. 最后通过Joydip和digit的技巧解决了这个问题。 Here's my solution in case somebody else gets the same problem as I did: 如果有人遇到与我相同的问题,这是我的解决方案:

Client-side controller 客户端控制器

$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 Express.js

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

Mongoose schema 猫鼬模式

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"});

Server-side controller 服务器端控制器

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

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

Thank you everyone for your help. 感谢大家的帮助。 :) :)

You can try by creating an Index: 您可以尝试通过创建索引:

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

And then by searching for the value, Example: 然后通过搜索值,例如:

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

If you want to search all key, then you can use 如果要搜索所有键,则可以使用

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

then search by 然后搜索

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 MongoDB 中正确创建索引以在整个集合中进行搜索? - How to create index properly in MongoDB to search in entire one collection? 使用find()在MongoDB中搜索嵌套键 - Using find() to search for nested keys in MongoDB MongoDB 按某个字段对集合中的所有键和值进行分组 - MongoDB group all keys and values in a collection by a certain field 如何使用 Mongodb 中的 findOne 在 object 中搜索值 - How can i search for a value inside an object with using findOne in Mongodb 如何使用Node.js将对象添加到MongoDB中另一个集合中的集合 - How to add object to collection inside another collection in MongoDB using Node.js 如何通过javascript搜索关键词只显示关键词的片段? - How to make a search keyword through javascript and only display the snippet of the keyword? 如何搜索mongodb集合条目属性 - How to search mongodb collection entry attributes 如何在 MongoDB 中的 2 个不同集合(带分页)中进行搜索 - How to make a search in 2 different collection (with pagination) in MongoDB 如何连接到mongodb以及如何使用node.js程序从mongodb中的dabase打印所有集合的列表 - How to connect to mongodb and How to print list of all collection from a dabase in mongodb using node.js program 如何使用另一个数组更新MongoDB集合内的数组,但仅更新更改的值? - how to update an array inside a MongoDB collection with another array but update only changed values?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM