如何使用nodejs(express)mongodb搜索文档中的两个字段?

时间:2015-11-25 11:51:13

标签: node.js mongodb express

我创建了ArticleSchema。以下是我的虚拟数据,我想从中找到基于某些字段的文章。

$scope.Articles = [{
      email: "vijaybs@gmail.com",
      Type: "Magazine",
      Storename: "Vijay Book stores",
      Categories:"Business Today,Intelligent Computing CHIP magazine,Electronics For You"

    }, {

      email: "news.martin@gmail.com",
      Type: "Newspaper",
      Storename: "Martin morning news",
      Categories:"The Deccan Times,"


    }, {

      email: "vinay@gmail.com",
      Type: "Newspaper,Magazine",
      Storename: "google news center",
      Categories:"Electronics For You,Business Today,Business Line,Calicut Review,The Financial Express"


    }, {

      email: "hotnews@gmail.com",
      Type: "Newspaper",
      Storename: "Hot news center",
      Categories:"The Hindu,The Deccan Times,Business Line,Business Today"



    }]

我想按类别和类型字段过滤数据。

控制器

'use strict';

/**
 * Module dependencies.
 */
var mongoose = require('mongoose'),
    errorHandler = require('./errors.server.controller'),
    Article = mongoose.model('Article'),
    _ = require('lodash');

/**
 * Create a article
 */
exports.create = function(req, res) {
    var article = new Article(req.body);
    article.user = req.user;

    article.save(function(err) {
        if (err) {
            return res.status(400).send({
                message: errorHandler.getErrorMessage(err)
            });
        } else {
            res.json(article);
        }
    });
};

/**
 * Show the current article
 */
exports.read = function(req, res) {
    res.json(req.article);
};

/**
 * Update a article
 */
exports.update = function(req, res) {
    var article = req.article;

    article = _.extend(article, req.body);

    article.save(function(err) {
        if (err) {
            return res.status(400).send({
                message: errorHandler.getErrorMessage(err)
            });
        } else {
            res.json(article);
        }
    });
};

/**
 * Delete an article
 */
exports.delete = function(req, res) {
    var article = req.article;

    article.remove(function(err) {
        if (err) {
            return res.status(400).send({
                message: errorHandler.getErrorMessage(err)
            });
        } else {
            res.json(article);
        }
    });
};

/**
 * List of Articles
 */
exports.list = function(req, res) {
    Article.find().sort('-created').populate('user', 'displayName').exec(function(err, articles) {
        if (err) {
            return res.status(400).send({
                message: errorHandler.getErrorMessage(err)
            });
        } else {
            res.json(articles);
        }
    });
};

/**
 * Article middleware
 */
exports.articleByID = function(req, res, next, id) {
    Article.findById(id).populate('user', 'displayName').exec(function(err, article) {
        if (err) return next(err);
        if (!article) return next(new Error('Failed to load article ' + id));
        req.article = article;
        next();
    });
};

模型

'use strict';

/**
 * Module dependencies.
 */
var mongoose = require('mongoose'),
    Schema = mongoose.Schema;

/**
 * Article Schema
 */
var ArticleSchema = new Schema({

    email: {
        type: String,
        default: '',
        trim: true,

    },
    Storename: {
        type: String,
        default: '',
        trim: true
    },
    Type: {
        type: String,
        default: '',
        trim: true
    },
    Categories: {
        type: String,
        default: '',
        trim: true
    }


});

mongoose.model('Article', ArticleSchema);

在mongoose中过滤给定数据的查询是什么

1 个答案:

答案 0 :(得分:0)

将其添加到您的代码中并传递请求中的类型和类别。

routes.js

 app.route('/articles/filter')

      .post(articles.getArticles);

Controller.js

 exports.getArticles = function(req, res) {
Article.find({Type:{'$regex':'Magazine'},Categories:{'$regex':'Business Today'}}).exec(function(err, articles) {
 if (err) {
  return res.status(400).send({ message:  errorHandler.getErrorMessage(err) }); 
} 
else { console.log(articles);res.json(articles); } 
}); 
};