gpt4 book ai didi

node.js - 通过消除具有某些标志的数组对象值来查找集合中的文档

转载 作者:太空宇宙 更新时间:2023-11-04 00:49:02 25 4
gpt4 key购买 nike

考虑一个包含以下文档的集合(下面提到了虚拟值):

{
id: "001",
company: "abc",
empDet: [
{
disableFlag: true,
name: 'raj',
age: 45,
designation: 'clerk'
},
{
disableFlag: false,
name: 'ramya',
age: 35,
designation: 'manager'
},
{
disableFlag: false,
name: 'gabriel',
age: 41,
designation: 'asstManager'
},
{
disableFlag: true,
name: 'saran',
age: 55,
designation: 'attender'
},
{
disableFlag: true,
name: 'buela',
age: 25,
designation: 'cashier'
}
],
location: "delhi"
},
{
id: "002",
company: "xyz",
empDet: [
{
disableFlag: true,
name: 'ravi',
age: 45,
designation: 'consultant'
},
{
disableFlag: true,
name: 'reka',
age: 35,
designation: 'manager'
},
{
disableFlag: false,
name: 'geetha',
age: 41,
designation: 'asstManager'
},
{
disableFlag: false,
name: 'varun',
age: 55,
designation: 'attender'
},
{
disableFlag: true,
name: 'balu',
age: 25,
designation: 'clerk'
}
],
location: "mumbai"
},
{
id: "003",
company: "pqr",
empDet: [
{
disableFlag: true,
name: 'bobby',
age: 45,
designation: 'consultant'
},
{
disableFlag: true,
name: 'vinay',
age: 35,
designation: 'manager'
},
{
disableFlag: true,
name: 'faritha',
age: 41,
designation: 'asstManager'
},
{
disableFlag: false,
name: 'arun',
age: 55,
designation: 'attender'
},
{
disableFlag: true,
name: 'kayal',
age: 25,
designation: 'clerk'
},
],
location: "calcutta"
}

使用 node.js 和 mongoose,我想检索 xyz 公司详细信息,但 "empDet" 的值应该仅包含包含 disableFlag 为 true 的值。例如,我期望的输出应该是:

{
id: "002",
company: "xyz",
empDet: [
{
disableFlag: true,
name: 'ravi',
age: 45,
designation: 'consultant'
},
{
disableFlag: true,
name: 'reka',
age: 35,
designation: 'manager'
},
{
disableFlag: true,
name: 'balu',
age: 25,
designation: 'clerk'
}
],
location: "mumbai"
}

如何使用 node.js 和 mongoose 消除某些标志为 false 的数组对象值来查找或检索集合中的文档?

最佳答案

$redact 管道运算符在 aggregation framework 在这种情况下应该适合你。这将递归地遍历文档结构,并根据每个级别的指定条件的评估执行一些操作。这个概念可能有点难以理解,但以下示例演示了这一点:

Model.aggregate([
{
"$match": { "company" : "xyz" }
},
{
"$redact": {
"$cond": {
"if": {
"$eq": [ "$disableFlag", false ]
},
"then": "$$PRUNE",
"else": "$$DESCEND"
}
}
}
]).exec(function (err, results){
// handle error
console.log(results);
});

示例输出:

/* 0 */
{
"result" : [
{
"_id" : ObjectId("562e1398bf03613b4ad1b541"),
"id" : "002",
"company" : "xyz",
"empDet" : [
{
"disableFlag" : true,
"name" : "ravi",
"age" : 45,
"designation" : "consultant"
},
{
"disableFlag" : true,
"name" : "reka",
"age" : 35,
"designation" : "manager"
},
{
"disableFlag" : true,
"name" : "balu",
"age" : 25,
"designation" : "clerk"
}
],
"location" : "mumbai"
}
],
"ok" : 1
}

-- 更新 --如果您想填充属于 xyz 公司的员工的详细信息,您可以使用 populate 使用最新版本的 mongoose,它能够在已添加的查询中递归填充相关文档。但这需要第二个查询,并使用不同的填充方式,如下所示:

var pipeline = [
{
"$match": { "company" : "xyz" }
},
{
"$redact": {
"$cond": {
"if": {
"$eq": [ "$disableFlag", false ]
},
"then": "$$PRUNE",
"else": "$$DESCEND"
}
}
}
];
Model.aggregate(pipeline,
function(err, results) {
if (err) throw err;
Model.populate(results, {"path": "empDet.emp_id"}, function(err, results) {
if (err) throw err;
console.log(JSON.stringify(results, undefined, 4));
});
}
);

关于node.js - 通过消除具有某些标志的数组对象值来查找集合中的文档,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33344531/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com