Mongo DB条件查询 [英] Mongo db conditional query

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

问题描述

成为mongo的新手,陷入了条件查询中: 我想根据3个条件(名字,姓氏和电子邮件ID)执行搜索: 当所有字段都存在时,下面的查询工作完美:

being a newbie to mongo, stuck in a conditional query: I want to perform a search on the basis of 3 criteria, first name, last name and email id: below query works perfect when all the fields exist:

db.students.find( { $and: [ { first_name:  /^Abc$/i }, { last_name:         'Xyz'},{email_id:'gd@he'} ]})

问题是,当我不提供电子邮件ID时,查询未返回任何结果,因为它认为电子邮件ID为空并搜索了"Abc Firtis空"组合, 我希望满足以下情况: 我有一群学生:

the problem is when I don't give an email id , the query dosen't returns any result as it considers the email id to be null and searches for the combination 'Abc Firtis null', where as I want the below scenario to be fulfilled: I have a collection of students:

- FirstName: 1. ABC 2. ABC 3.ABC
- LastName:  1.XYZ 2. XY 3. XZ
- EmailID:   1.abc@xyz  2.Ab@xy 3.Ab@xz

如果仅在搜索中输入名字,则应返回所有3个结果 如果用户输入名字和姓氏,则应返回前两个结果,如果用户输入所有三个详细信息,则应仅返回1个结果.

if one enters only the first name in the search it should return all the 3 results if user enters first name and last name it should return first two results and if the user enters all three details it should return only 1 result.

任何线索都将受到高度赞赏.

Any leads would be highly appreciated.

推荐答案

您似乎在谈论要输入的查询的输入"数据是不同的,以及如何构造查询以忽略字段作为没有输入的条件.

You seem to be talking about "input" data being different for the queries you want to issue and how to contruct the query to ignore fields as criteria for which you have no input.

这实际上是关于如何处理输入的收集方式,但都归结为您有条件地"构建了查询(无论如何只是一个数据结构),而不是静态地定义了一个查询.并以某种方式忽略null或空数据.

This is all really about how the input is being collected as to how you handle it, but it all boils down to that you "conditionally build" the query ( which is just a data structure anyway ) rather than statically define a query and somehow ignore null or empty data.

因此,如果您有单独的变量,则可以测试每个值并构建查询:

So if you have seperate variables, then you test each value and build the query:

var firstName = "abc",
    lastName = "xy,
    email = null,
    query = {};

if (firstName) {
    query.firstName = new RegExp("^"+firstName,"i")
}

if (lastName) {
    query.lastName = new RegExp("^"+lastName,"i")
}

if (email) {
    query.email = new RegExp("^"+email,"i")
}

db.students.find(query)

这将基于输入构建一个查询对象,最终将像这样:

This would build a query object that would end up like this based on the inputs:

{ "firstName": /^abc/i, "lastName": /^xy/i }

因此,由于email中没有任何值,因此不包括该条件.最终结果是甚至没有查询未提供的条件,然后您获得了相关的匹配项.

So since there is no value in email then the condition is not included. The end result is the condition not provided is not even queried for and then you get the relevant matches.

如果您要从以下结构化输入开始,则基本上可以简化该方法:

The same approach is basically simplified if you have some structured input to begin with:

var params = {
    firstName = "abc",
    lastName = "xy"
};

var query = {};

Object.keys(params).forEach(function(key) {
    if (params[key])
        query[key] = new RegExp("^"+key,"i");
});

db.students.find(query);

这是同一回事,但是由于您将所有参数键都放在一个位置,因此您可以迭代它们以构建查询,而不必单独进行测试.

And it's the same thing, but since you have all parameter keys in one place then you can iterate them to build the query rather than test individually.

通常是这种情况,您需要从Web请求之类的东西中输入参数,具体取决于输入方法,这些参数进入req.params甚至是req.body.因此,如果您构建代码以接受输入到类似对象(或已经拥有)的对象,则可以使用它来构建查询.

This is generally the case where you have input from something like a web request with parameters that come into req.params or even req.body depending on your method of input. So if you structure your code to accept input into a similar object ( or already have it ) then you use it to build your query.

还请注意,根据定义,所有 MongoDB查询参数是隐式的"AND"条件,因此几乎不需要使用$and,除非您为同一文档明确满足多个条件财产.即使那样,通常也有更好的语法替代方法.

Also note that all MongoDB query arguments are implicitly an "AND" condition by definition, so there is rarely any need to use $and unless you explicitly have multiple conditions to meet for the same document property. Even then there are generally better syntax alternates.

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

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