gpt4 book ai didi

javascript - 失踪 child

转载 作者:可可西里 更新时间:2023-11-01 10:43:08 24 4
gpt4 key购买 nike

将 Mongoose 与 MongoDB 结合使用,我的 Schema 如下:

var PartSchema = new Schema({
partcode: String,
children: [String]
});

数据如下所示:

[{"partcode":"A1","children":["B1","B2","B3","B4"]},
{"partcode":"B1","children":["C11","C21","C31","C41"]},
{"partcode":"B3","children":["C13","C23","C33","C43"]},

我可以使用以下静态调用查询 A1 的子字段:

PartSchema.static('getChildren', function (partcode, callback) {
var self = this;
self.findOne({ partcode: partcode }, childrenOnly)
.exec(function (err, doc) {
return self.find({"partcode": {"$in": doc.children} }, exclId, callback);
});
});

返回(通过 express )

 [{"partcode":"B1","children":["C11","C21","C31","C41"]},
{"partcode":"B3","children":["C13","C23","C33","C43"]}]

我需要的是返回所有找不到的 child ,例如:

[{"children":["B2","B4"}]

最佳答案

您可以使用 _.difference() lodash库中计算数组集差的方法:

var _ = require("lodash");
PartSchema.static('getChildren', function (partcode, callback) {
var self = this;
self.findOne({ partcode: partcode }, childrenOnly)
.exec(function (err, doc) {
var promise = self.find({"partcode": {"$in": doc.children} }, exclId).lean().exec();
promise.then(function (res){
var codes = res.map(function (m) {
return m.children;
}),
obj = {"children": _.difference(doc.children, codes)},
result = [];
result.push(obj);
return result;
});
});
});

-- 更新 --

使用 MongoDB 的 aggregation framework ,就可以达到想要的效果。让我们先在 mongoshell 中演示一下。

假设您在零件集合中插入以下测试文档:

db.part.insert([
{"partcode":"A1","children":["B1","B2","B3","B4"]},
{"partcode":"B1","children":["C11","C21","C31","C41"]},
{"partcode":"B3","children":["C13","C23","C33","C43"]}
])

聚合在这里很有用,因为您有一个给定零件代码的子代码数组,比如 "A1",即 ["B1","B2", “B3”,“B4”]。在这种情况下,您的聚合管道将包含以下聚合管道阶段:

  1. $match - 您需要它来过滤子代码不在 ["B1","B2","B3","B4"] 数组中的那些文档。这是使用 $nin 实现的运营商。

  2. $group - 这将来自前一个流的所有文档分组,并创建一个具有父部分代码的附加数组字段。通过使用 $addToSet 成为可能累加器运算符。

  3. $project - 通过添加新字段 partcode(最终将成为结果对象的一部分)并抑制 _id 字段来 reshape 流中的每个文档。在这里您可以获得条件中的父部分代码与管道文档中不存在的父部分代码之间的数组差异,这可以使用 $setDifference 实现。设置运算符。

您最终的聚合运算符将如下所示(使用 mongoshell):

var children = ["B1","B2","B3","B4"];
db.part.aggregate([
{
"$match": {
"children": { "$nin": children }
}
},
{
"$group": {
"_id": null,
"parents": {
"$addToSet": "$partcode"
}
}
},
{
"$project": {
"_id": 0,
"partcode": {
"$setDifference": [ children, "$parents" ]
}
}
}
]).pretty()

输出:

/* 0 */
{
"result" : [
{
"partcode" : ["B2","B4"]
}
],
"ok" : 1
}

在您的 Mongoose 模式方法中使用相同的概念:

PartSchema.static('getChildren', function (partcode, callback) {
var self = this;
self.findOne({ partcode: partcode }, childrenOnly)
.exec(function (err, doc) {
var pipeline = [
{
"$match": {
"children": { "$nin": doc.children }
}
},
{
"$group": {
"_id": null,
"parents": {
"$addToSet": "$partcode"
}
}
},
{
"$project": {
"_id": 0,
"partcode": {
"$setDifference": [ doc.children, "$parents" ]
}
}
}
],
self.aggregate(pipeline).exec(callback);
});
});

或使用 Mongoose aggregation pipeline builder 流畅的通话:

PartSchema.static('getMissedChildren', function (partcode, callback) {
var self = this;
self.findOne({ partcode: partcode }, childrenOnly)
.exec(function (err, doc) {
var promise = self.aggregate()
.match({"children": { "$nin": doc.children }})
.group({"_id": null,"parents": {"$addToSet": "$partcode"}})
.project({"_id": 0,"partcode": {"$setDifference": [ doc.children, "$parents" ]}})
.exec(callback);
});
});

关于javascript - 失踪 child ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30054557/

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