[英]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.