如何在 mongo 选择器中使用过滤器的默认值?
How to use default values for filters in a mongo selector?
我正在尝试将过滤器应用于 mongo 查找查询。这个想法是,如果过滤器具有值,mongo 选择器将限制正在 returned 的内容,但如果未指定过滤器(过滤器具有空值或默认值),则不应限制查询.如果指定了过滤器,我知道如何让过滤器工作,但我不确定如果没有指定过滤器,如何确保它 returns 未过滤。如果过滤器未指定或使用默认值,我如何获取对 return 集合中所有文档的查找查询?
仅供参考:我在 Meteor 项目中使用它,并将过滤器设为会话变量,使 returned 动态化。
示例集合:
/* example documents in SampleCollection
{ name: "sample1", fieldA: "foo", fieldB: "foo" }
{ name: "sample2", fieldA: "foo", fieldB: "bar" }
{ name: "sample3", fieldA: "bar", fieldB: "foo" }
{ name: "sample4", fieldA: "bar", fieldB: "bar" }
*/
示例 JS 代码:
var filters = {
fieldA: null,
fieldB: null
};
var getFieldASelector = function () {
if (filters.fieldA) {
return { $eq: fieldA };
} else {
/* fieldA has a falsey value which is the default
and therefore should not limit the find query */
// not sure what to return here
return {};
};
};
var getFieldBSelector = function () {
if (filters.fieldB) {
return { $eq: fieldB };
} else {
/* fieldB has a falsey value which is the default
and therefore should not limit the find query */
// not sure what to return here
return {};
};
};
var results = SampleCollection.find({
fieldA: getFieldASelector(),
fieldB: getFieldBSelector()
});
在此示例中 results
应该 return 所有四个文件。如果 filter = { fieldA: "foo", fieldB: null };
那么 results
应该 return 文档 sample1 和 sample2.
我建议您创建一个 selector
对象并根据给定的过滤器填充它。虽然我不确定这是否是您所要求的。
function getResults(filter){
var selector = {};
// what this does is filters aways keys with non-truthy values
Object.keys(filter).reduce(function (prev, curr){
var val = filter[curr];
if (!!val)
prev[curr] = filter[curr];
return prev;
}, selector);
return SampleCollection.find(selector);
}
当您实际想要过滤具有非真实值(例如 0
或空字符串)的字段时,会出现意外行为。
假设每个文档都有两个键,你可以return {$ne:null}
。如果你想在键存在的情况下工作,但它的值为空,你也可以 return {$exists:true}
我正在尝试将过滤器应用于 mongo 查找查询。这个想法是,如果过滤器具有值,mongo 选择器将限制正在 returned 的内容,但如果未指定过滤器(过滤器具有空值或默认值),则不应限制查询.如果指定了过滤器,我知道如何让过滤器工作,但我不确定如果没有指定过滤器,如何确保它 returns 未过滤。如果过滤器未指定或使用默认值,我如何获取对 return 集合中所有文档的查找查询?
仅供参考:我在 Meteor 项目中使用它,并将过滤器设为会话变量,使 returned 动态化。
示例集合:
/* example documents in SampleCollection
{ name: "sample1", fieldA: "foo", fieldB: "foo" }
{ name: "sample2", fieldA: "foo", fieldB: "bar" }
{ name: "sample3", fieldA: "bar", fieldB: "foo" }
{ name: "sample4", fieldA: "bar", fieldB: "bar" }
*/
示例 JS 代码:
var filters = {
fieldA: null,
fieldB: null
};
var getFieldASelector = function () {
if (filters.fieldA) {
return { $eq: fieldA };
} else {
/* fieldA has a falsey value which is the default
and therefore should not limit the find query */
// not sure what to return here
return {};
};
};
var getFieldBSelector = function () {
if (filters.fieldB) {
return { $eq: fieldB };
} else {
/* fieldB has a falsey value which is the default
and therefore should not limit the find query */
// not sure what to return here
return {};
};
};
var results = SampleCollection.find({
fieldA: getFieldASelector(),
fieldB: getFieldBSelector()
});
在此示例中 results
应该 return 所有四个文件。如果 filter = { fieldA: "foo", fieldB: null };
那么 results
应该 return 文档 sample1 和 sample2.
我建议您创建一个 selector
对象并根据给定的过滤器填充它。虽然我不确定这是否是您所要求的。
function getResults(filter){
var selector = {};
// what this does is filters aways keys with non-truthy values
Object.keys(filter).reduce(function (prev, curr){
var val = filter[curr];
if (!!val)
prev[curr] = filter[curr];
return prev;
}, selector);
return SampleCollection.find(selector);
}
当您实际想要过滤具有非真实值(例如 0
或空字符串)的字段时,会出现意外行为。
假设每个文档都有两个键,你可以return {$ne:null}
。如果你想在键存在的情况下工作,但它的值为空,你也可以 return {$exists:true}