从mongo返回的对象(findOne)中筛选/返回单个嵌套对象
Filtering/Returning a single nested object from mongo returned object (findOne)
尽管围绕这个主题还有一些问题,但我还没有找到一个明确的通用"最佳实践"来过滤返回的流星mongo收集对象。
(仅供参考:我正在使用MeteorJS)
我从configs集合中提取了一个配置文档。
let thisConfig = ClinicConfigs.findOne({_id: "xyz"});
这已经返回了以下
{
_id: "xyz",
name: "john doe's clinic",
activeServices: [
{
name: "teeth whitening",
ref: "teethWhitening",
docs: [
{
docId: "a",
name: "Client questionnaire",
ref: "clientQuestionnaire",
},
{
docId: "b",
name: "Client consent form",
ref: "clientConsentForm",
}
]
},
{
name: "liposuction",
ref: "liposuction",
docs: [
{
docId: "a",
name: "Client questionnaire",
ref: "clientQuestionnaire",
},
{
docId: "b",
name: "Client consent form",
ref: "clientConsentForm",
}
]
}
];
一旦我返回了这个文档/对象,我只需要从activeServices数组中提取一个对象。
虽然这不起作用,但以下是澄清我需要的逻辑:
let thisService = ClinicConfigs.findOne({_id: "xyz"})
.activeServices.findOne({ref: "teethWhitening"});
我尝试了以下操作,但没有成功:
let thisConfig = ClinicConfigs.findOne({_id: "xyz"});
let thisService = thisConfig.activeServices.filter(function(d) {return d.ref === "teethWhitening"})[0];
return thisService.docs;
它可以工作,但我不得不用后面的大括号更正您的thisConfig数据对象。所以一旦它像
var thisCongig = {
_id: "xyz",
name: "john doe's clinic",
activeServices: [
{
name: "teeth whitening",
ref: "teethWhitening",
docs: [
{
docId: "a",
name: "Client questionnaire",
ref: "clientQuestionnaire",
},
{
docId: "b",
name: "Client consent form",
ref: "clientConsentForm",
}
]
},
{
name: "liposuction",
ref: "liposuction",
docs: [
{
docId: "a",
name: "Client questionnaire",
ref: "clientQuestionnaire",
},
{
docId: "b",
name: "Client consent form",
ref: "clientConsentForm",
}
]
}
]
};
var thisService = data.activeServices.find( d => d.ref == "teethWhitening");
并且它被取回。
相关文章:
- javascript函数,它接受两个输入:一个对象和一个键,并返回对象中该键的相应值
- 通过数组值返回对象
- JavaScript:返回对象的函数
- 为什么document.getElementsByClassName(“className”)返回对象
- 如何返回对象
- Promise没有;t返回对象的数组
- 面料:“;鼠标:向下”;在event.target中未返回对象
- Lodash:返回对象的第一个键,该对象的值(即数组)中有一个给定的元素(即字符串)
- Javascript:返回对象
- 为什么 javascript 返回对象位置 true 或 false
- 解析查询查找方法返回对象而不是数组
- 来自函数的返回对象在 jQuery 中未从 $.post 定义
- JavaScript 返回对象未按预期工作
- 返回“[对象对象] NaN” - Javascript
- 何时从创建返回对象
- Javascript:接收对象并返回对象中描述的所有数字的列表的函数
- JSDoc:返回对象结构
- node.js请求返回对象
- 返回对象的ECMAScript 6箭头函数
- 如何从控制器返回对象列表并使用JQuery(SpringMVC,ajax)显示它们