gpt4 book ai didi

node.js - 查找 MongoDB/Mongoose 中给定数组中包含子集合所有元素的特定字段的所有文档

转载 作者:太空宇宙 更新时间:2023-11-03 23:37:46 24 4
gpt4 key购买 nike

使用 MongoDB 和 Mongoose,例如,我有以下架构:

var componentSchema = mongoose.Schema({
name : String,
price : Number
});

var partSchema = mongoose.Schema({
componentId : { type: Schema.Types.ObjectId, ref: 'Component' },
quantity : Number
});

var robotSchema = mongoose.Schema({
name : String,
type : Number,
parts : [partSchema]
});

每个机器人都需要构建一组组件。

由于机器人可能需要多个组件副本(例如 10 个 bolt 、5 个螺钉、1 个晶体管...),因此我们在机器人模型内存储一组部件,其中每个部件都包含对组件的引用以及一个附加字段、数量。

现在,我感兴趣的是,给定一组组件名称(或者最终给定一组组件ID),我可以使用这些类型的组件构建的所有机器人(请注意,组件不包括数量,我只是假设我有无限数量的这些组件),并按使用给定数组中最多组件的机器人排序。

Robot A: 2 bolts, 2 transistors 
Robot B: 10 bolts, 2 capacitors, 3 bars of plutonium
Robot C: 5 bolts, 1 capacitor, 5 transistors

I have [bolts, capacitors, transistors].
Query results:

Robot C
Robot A

(In this order!)

是否可以使用复杂的 MongoDB 查询?

最佳答案

当然。您希望查找机器人文档,使得对于组件列表中的每个组件,信息中该组件的数量小于机器人文档中列出的数量,并且您的组件列表耗尽了机器人的组件列表。

// robot docs
{
"name" : "My Fun Robot v3",
"type" : 999,
"parts" : [
{ "_id" : 0, "quantity" : 2 },
{ "_id" : 1, "quantity" : 5 },
{ "_id" : 2, "quantity" : 1 },
{ "_id" : 3, "quantity" : 7 }
]
},
{
"name" : "My Pun Robot v1",
"type" : 222,
"parts" : [
{ "_id" : 4, "quantity" : 3 },
{ "_id" : 1, "quantity" : 4 },
{ "_id" : 2, "quantity" : 8 },
{ "_id" : 6, "quantity" : 1 }
]
},
{
"name" : "My Sun Robot v876",
"type" : 9834,
"parts" : [
{ "_id" : 0, "quantity" : 6 },
{ "_id" : 1, "quantity" : 400 },
{ "_id" : 2, "quantity" : 800 },
{ "_id" : 3, "quantity" : 1000 }
]
},
{
"name" : "My Gun Robot v0",
"type" : 1337,
"parts" : [
{ "_id" : 0, "quantity" : 1 },
{ "_id" : 1, "quantity" : 1 },
{ "_id" : 2, "quantity" : 1 },
]
}

和组件信息

\\ component info
var components = [
{ "_id" : 0, "quantity" : 20 },
{ "_id" : 1, "quantity" : 20 },
{ "_id" : 2, "quantity" : 20 },
{ "_id" : 3, "quantity" : 20 },
{ "_id" : 4, "quantity" : 20 }
]

查询的构造:

// for each component, we either need the component not to be required for the robot, or for the robot's quantity required to be less than the quantity we have
var query = {}
var comp_cond = []
components.forEach(function(comp) {
comp_cond.push({ "$or" : [{ "parts" : { "$elemMatch" : { "_id" : comp._id, "quantity" : { "$lte" : comp.quantity } } } }, { "parts._id" : { "$ne" : comp._id } } ]})
})
query["$and"] = comp_cond
// we need to make sure that there's no part in `parts` that's not in the components array
query["parts"] = { "$not" : { "$elemMatch" : { "_id" : { "$nin" : components.map(function(comp) { return comp._id }) } } } }

我不会显示上面示例的实际查询,因为它真的很长。您可以在 mongo shell 中运行代码并查看它。

> db.robots.find(query, { "_id" : 0, "name" : 1 })
{ "name" : "My Fun Robot v3" }
{ "name" : "My Gun Robot v0" }

我相信这是正确的结果。您应该进行更详尽的测试,以确保它能够处理组件列表与机器人零件列表无法匹配的所有方式。

关于node.js - 查找 MongoDB/Mongoose 中给定数组中包含子集合所有元素的特定字段的所有文档,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29643813/

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