【发布时间】:2021-07-27 19:04:31
【问题描述】:
我正在尝试通过 MongoDB-Native 驱动程序从 Nodejs 代码中获取来自 Mongodb 的数据。这是我收藏的模型:
{
name: 'Himkar Dwivedi',
role: 'Software Engineer',
address: {
flat: '163',
building: 'B6',
area: 'DLF',
contact: {
type: 'phone',
num: '910xxxx882'
}
}
}
我从以下查询中得到了正确的响应:
{ role: 'Software Engineer', name: 'Himkar Dwivedi', 'address.flat': '163' }
但我需要形成这样的查询:(真正的 json 查询对象)
{ role: 'Software Engineer', name: 'Himkar Dwivedi', address:{flat: '163' }}
这没有给我任何回应。请帮我解决。
【问题讨论】:
-
您的第二个查询将仅匹配整个
address子文档为{flat: '163'}的文档(没有其他字段)。这就是为什么您需要使用点表示法。你问的是这个吗? -
感谢 Johnny,我使用 DOT 解决了我的问题。但它应该支持纯json查询
标签: json node.js mongodb mongodb-query node-mongodb-native