动态查询MongoDB [英] Dynamic Query MongoDB

查看:82
本文介绍了动态查询MongoDB的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试根据从客户端搜索表单接收到的数据来构建MongoDB查询对象.我的目标是使用用户提供的所有条件查询数据库,同时允许用户选择保留某些搜索字段为空.

I'm trying to build a MongoDB query object based on data received from a client-side search form. My goal is to query the database with any and all criteria provided by the user, whilst allowing the user to leave some search fields blank if they choose to.

这是我当前对查询对象的尝试:

This is my current attempt at a query object is:

var q = {}; // declare the query object
  q['$and']=[]; // filter the search by any criteria given by the user
  if((req.body.learninglanguages).length > 0){ // if the criteria has a value or values
    q["$and"].push('{learningLanguages: {$in: ' + req.body.learninglanguages.split(",") + '}}'); // add to the query object
  }
  if((req.body.spokenlanguages).length > 0){
    q["$and"].push('{spokenLanguages: {$in: ' + req.body.spokenlanguages.split(",") + '}}');
  }
  if((req.body.country).length > 0){
    q["$and"].push('{country: {$in: ' + req.body.country.split(",") + '}}');
  }
  if((req.body.commethod).length > 0){
    q["$and"].push('{comMethod: {$in: ' + req.body.commethod.split(",") + '}}');
  }

但是生成的对象是:

{ '$and': 
   [ '{learningLanguages: {$in: Albanian,American Sign Language,Amharic,Arabic,Arabic (Egyptian)}}',
     '{spokenLanguages: {$in: Akan,Albanian,American Sign Language,Amharic}}',
     '{country: {$in: Åland Islands}}',
     '{comMethod: {$in: whatsapp,email,face to face,skype}}' ] }

如何从req.body对象正确构建MongoDB $ in查询?

How can I correctly build a MongoDB $in query from req.body objects?

推荐答案

查询的问题是您试图构建一个字符串,而不是直接构建一个对象,例如mongoDB&猫鼬接受:

The problem with your query is you are attempting to build a string instead of directly building an object, like mongoDB & mongoose accept:

var q = {}; // declare the query object
  q['$and']=[]; // filter the search by any criteria given by the user
  if((req.body.learninglanguages).length > 0){ // if the criteria has a value or values
    q["$and"].push({ learningLanguages: {$in: req.body.learninglanguages.split(",") }}); // add to the query object
  }
  if((req.body.spokenlanguages).length > 0){
    q["$and"].push({ spokenLanguages: {$in: req.body.spokenlanguages.split(",") }});
  }
  if((req.body.country).length > 0){
    q["$and"].push({ country: {$in: req.body.country.split(",") }});
  }
  if((req.body.commethod).length > 0){
    q["$and"].push({ comMethod: {$in: req.body.commethod.split(",") }});
  }

您可以看到,我没有推送一个字符串,而是推送了一个符合文档规范的直接对象.

You can see that instead of pushing in a string, I am instead pushing in a direct object that fits the documentation specifications.

有关更多信息,请参见此处的文档:

See the documentation here for more information:

  1. https://docs.mongodb.com/manual/reference/operator /query/and/
  2. https://docs.mongodb.com/manual/reference/operator /query/in/
  1. https://docs.mongodb.com/manual/reference/operator/query/and/
  2. https://docs.mongodb.com/manual/reference/operator/query/in/

这是一个可以工作的jsbin,您可以使用:

And here's a working jsbin you can play with:

  1. http://jsbin.com/cequbipiso/edit?js,console

这篇关于动态查询MongoDB的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆