- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在尝试在 Mongoose 中构建一种复杂的查询,我的问题是该查询可以接收的所有参数都是非强制性的,这给我带来了问题。现在我这样做了:
let query = {
publicationType: req.body.searchParams.publicationType
};
if (req.body.searchParams.address) {
query.address = {};
if (req.body.searchParams.address.city) {
query.address.city = req.body.searchParams.address.city;
}
}
try {
const dwellings = await model.Dwelling.find({
$and: [{$or: [{publicationType: req.body.searchParams.publicationType}]},
{
$or: [{
address: [query.address]
}]
}]
}).lean().exec();
console.log(dwellings);
res.send({dwellings});
} catch (err) {
next(err);
}
});
这是为了尝试城市参数是否有效,如果我仅按发布类型搜索,我会得到所需的结果,如果我添加城市,即使我应该获得更多结果,我也会得到 0 个结果,我调试了 mongoose 以查看查询是如何构建的,如下所示:
Mongoose: dwelling.find({ '$and': [ { '$or': [ { publicationType: 'Venta' } ] }, { '$or': [ { address: { '$in': [ { city: 'La Plata' } ] } } ] } ] }, { fields: {} })
[]
最佳答案
在您的代码中,您正在构建一个 $and expression两个 $or expressions 但是每个 $or 表达式只有一个参数,因此您可以有效地构建此 $and find
表达式 (请注意,我使用下面的 mongo
shell 语法):
db.dwellings.find({
'$and': [
{ publicationType: 'Venta' },
{ address: { '$in': [ { city: 'La Plata' } ] } }
]
});
...换句话说:“查找带有publicationType Venta
和 address.city
La Plata
的住宅.
if I add city I get 0 results even when I'm supposed to get more
既然您提到您希望在添加 address.city
时获得更多结果,我怀疑您确实希望顶级表达式为 $or
而不是 $和
,像这样:
db.dwellings.find({
'$or': [
{ publicationType: 'Venta' },
{ address: { '$in': [ { city: 'La Plata' } ] } }
]
});
在将查询表达为代码之前,您可能希望在 mongo
shell 中对查询进行更多试验。如果您的工作站上恰好有 Docker,我发现在容器中运行 mongo 是一种在开发时进行原型(prototype)设计的有用方法,例如:
docker run --name mongo -d mongo:3.7
# after mongo is up and running...
docker exec -it mongo mongo
在交互式 mongo shell 中,您可以插入文档进行测试并使用 find
表达式进行实验,例如:
use stackoverflow;
dwell1 = { publicationType: 'Venta', address: { city: 'Somewhere' } };
dwell2 = { publicationType: 'Something', address: { city: 'La Plata' } };
db.dwellings.insert(dwell1);
db.dwellings.insert(dwell2);
# Try results of $and
db.dwellings.find({ '$and': [
{ publicationType: 'Venta' },
{ address: { '$in': [ { city: 'La Plata' } ] } }
] });
# ... compare to results of $or
db.dwellings.find({ '$or': [
{ publicationType: 'Venta' },
{ address: { '$in': [ { city: 'La Plata' } ] } }
] });
...插入一些文档进行测试后,确认您是否需要在 find
中使用 $and
或 $or
表达式.
关于node.js - Mongoose 没有带来任何结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50452056/
当我为我的项目运行“mvn dependency:tree”时,它显示如下: [INFO] --- maven-dependency-plugin:2.8:tree (default-cli) @ x
我调用 ajax 的脚本 function search_func(value) { $.ajax({ type: "GET", url: "sample.php
我正在使用 SIM 安装 Sitecore 8.1,但在打开启动板时,由于 ajax 请求返回 HTTP 500 错误,出现了几个 Javascript 错误。查看开发人员工具会显示以下消息: htt
按照目前的情况,这个问题不适合我们的问答形式。我们希望答案得到事实、引用或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the
我有一张 table : +-----------+-------------+------+-----+---------+-------+| Field | Type | N
所以,我对这个阵列很生气,第 2 天让我感到疼痛 *.... 我正在开发面向对象的 PHP 脚本。 我得到一个数组: Array ( [0] => Project Object ( [project_
我正在尝试将我的一个 Rails 项目升级到 Ruby 1.9.2。一切进展顺利,但一个 RSpec 测试失败了。在这个测试中,我需要一个Ruby lib: # file spec/models/my
我可以使用 Maven 的插件 spring-boot:run 运行 Spring Boot,但是当我尝试执行打包的 jar 时,它给出: Error creating bean with name
我是一名优秀的程序员,十分优秀!