gpt4 book ai didi

MongoDB 查询 - 返回切片数组

转载 作者:可可西里 更新时间:2023-11-01 09:31:27 27 4
gpt4 key购买 nike

我正在尝试针对以下架构指定特定查询:

executionSchema = new Schema(
timestamp: {type: Number},
components: [{
uid: { type: String },
type: { type: String },
control_ports: [ { name: String, values: [type: Number] } ]
input_samples: [ { name: String, values: [type: Number] } ]
output_samples: [ { name: String, values: [type: Number] } ]
execution_times: [type: Number]
}]
)

我想返回一个带有切片数组(input_samples 和 output_samples)的组件,由时间戳指定。这是我到目前为止所做的:

exports.getSamples = (req, res) ->
timestamp = req.param("timestamp")
uid = req.param("uid")
skip = req.param("skip")
amount = req.param("amount")
#query = Execution.findOne({'timestamp':timestamp}, 'components.input_samples components.output_samples')
query = Execution.findOne({'timestamp':timestamp})
query.where('components.uid').equals(uid)
query.slice('components.input_samples.values', 5)
query.slice('components.output_samples.values', 5)
query.select('components.$.')
#query.slice('values', 5)
query.exec ( err, samples )->
if err
console.log "Error: "
console.log err
res.json err
else
console.dir samples
res.json samples
return
return

它实际上返回正确的组件位,它包括数组内的每个元素。不知何故,我设法用另一个查询对数组进行切片,但结果包含每个可用的组件。我想我仍然需要习惯 MongoDb ..

谢谢。

编辑这是我得到的:

{
"_id": "5326ca6558f41c510a2659ad",
"components": [
{
"uid": "sine#0",
"type": "SW",
"_id": "5326ca6558f41c510a2659b5",
"execution_times": [
500,
450,
700
],
"output_samples": [
{
"name": "Output_Port",
"_id": "5326ca6558f41c510a2659b6",
"values": [
0,
0.8414709848078965,
0.9092974268256817,
0.1411200080598672,
-0.7568024953079282,
-0.9589242746631385,
-0.27941549819892586,
0.6569865987187891,
0.9893582466233818,
0.4121184852417566,
...,
-0.5440211108893698,
-0.9999902065507035,
0.5878193939808536,
0.9983436270438855,
0.4909953335002932
]
}
],
"input_samples": [],
"control_ports": [
{
"name": "Control 1",
"_id": "5326ca6558f41c510a2659b7",
"values": [
0,
0.8414709848078965,
0.9092974268256817,
0.1411200080598672,
-0.7568024953079282,
-0.9589242746631385,
-0.27941549819892586,
0.6569865987187891,
0.9893582466233818,
0.4121184852417566,
...,
-0.5440211108893698,
-0.9999902065507035,
0.5878193939808536,
0.9983436270438855,
0.4909953335002932
]
}
]
}
]

以及我想要的(返回这些数组的一个子集):

  {
"_id": "5326ca6558f41c510a2659ad",
"components": [
{
"uid": "sine#0",
"type": "SW",
"_id": "5326ca6558f41c510a2659b5",
"execution_times": [
500,
450,
700
],
"output_samples": [
{
"name": "Output_Port",
"_id": "5326ca6558f41c510a2659b6",
"values": [
0,
0.8414709848078965,
0.9092974268256817,
0.1411200080598672,
-0.7568024953079282,
-0.9589242746631385
]
}
],
"input_samples": [],
"control_ports": [
{
"name": "Control 1",
"_id": "5326ca6558f41c510a2659b7",
"values": [
0,
0.8414709848078965,
0.9092974268256817,
0.1411200080598672,
-0.7568024953079282
]
}
]
}
]

编辑 2:

So this does bring to question, "do you actually mean to have arrays"? Only saying this because as the information is presented, then it seems the only parts that are actually arrays are the "values" fields.

我猜数据比您想象的要复杂一点。组件数组可以保存任意数量的具有唯一“uid”的组件,每个“执行”都可以不同。因此我想我肯定必须为组件使用数组。

The problem with $slice in this context is that you actually don't know "which" array element out of the nesting to operate on. So the style you have presented will not work because the elements could be and any possible index. The correct form, if it were supported, and which it is not would be something like:

数组中特定组件的位置是我唯一不知道的索引,因为数据创建应用程序会在值更新之前初始化执行。因此,我只需要一个“位置 $ 运算符”。它应该看起来像 { "components.$.output_samples.0.values": { "$slice": 5 } }

Nested lists are going to be a problem for various things and are notoriously bad for updating. Where possible you should consider alternatives. In this case if you want to limit your output, then the only practical way is to retrieve the whole document, then process the arrays in code to limit the returned results.

我想这是一个小错误,简化了我的真实意图。我想使用切片运算符跳过前 n 个元素并检索以下 m 个元素。这两个变量应由用户指定。但是,output_samples.0.values 字段可能包含数百万个值。甚至更多。这就是为什么我想收集用户需要的尽可能多的值。

非常感谢您详细的回答。

最佳答案

关于 $slice 的问题这里或任何类型的数组投影都是您在定义的架构中嵌套数组的事实。我还注意到 positional $ 的使用运算符(operator)偷偷溜进去看一眼,所以最好在文档的上下文中从那里进行解释。

所以在那个页面上有这个:

  • The $ projection operator limits the content of the field to the first element that matches the query document.
  • The field must appear in the query document

因此对于初学者来说,您并不是在查询中要求匹配任何数组的任何元素中的特定字段。仅凭这一点,就无法对匹配元素进行投影。在任何情况下,只有第一个匹配的数组元素可能被匹配,即使您这样做也是如此。

在您的结构中,您唯一可以匹配的是数组“components”中的顶部位置。就在顶部。

$slice 的问题在这种情况下,您实际上不知道“哪个” 嵌套之外的数组元素要对其进行操作。因此,您所呈现的样式将不起作用,因为元素可能是任何可能的索引。正确的形式,如果它支持,而它应该是这样的:

{ "components.0.output_samples.0.values": { "$slice": 5 } }

因为您需要指定指向您实际谈论的元素的索引路径。

所以这确实让人产生疑问,“你真的意味着拥有数组”吗?之所以这么说,是因为在呈现信息时,似乎只有 “值” 字段才是真正的数组部分。

嵌套列表将成为各种问题,并且众所周知不利于更新。在可能的情况下,您应该考虑替代方案。在这种情况下,如果您想限制输出,那么唯一可行的方法是检索整个文档,然后在代码中处理数组以限制返回的结果。

考虑到所有“其他”数组嵌套有一个

即使是下面的“非常复杂”的咒语也只会(“有点”)以它现在的形式起作用em>一个元素。所以它使这成为可能但不切实际:

db.components.aggregate([
{ "$unwind": "$components" },
{ "$unwind": "$components.output_samples"},
{ "$unwind": "$components.control_ports"},
{ "$unwind": "$components.output_samples.values"},
{ "$limit": 5 },
{ "$group": {
"_id": {
"_id": "$_id",
"components": {
"uid": "$components.uid",
"type": "$components.type",
"_id": "$components._id",
"execution_times": "$components.execution_times",
"output_samples": {
"name": "$components.output_samples.name",
"_id": "$components.output_samples._id"
},
"input_samples": "$components.input_samples",
"control_ports": "$components.control_ports"
}
},
"output_samples_values": {"$push": "$components.output_samples.values" }
}},
{ "$project": {
"_id": {
"_id": "$_id._id",
"components": {
"uid": "$_id.components.uid",
"type": "$_id.components.type",
"_id": "$_id.components._id",
"execution_times": "$_id.components.execution_times",
"output_samples": {
"name": "$_id.components.output_samples.name",
"_id": "$_id.components.output_samples._id",
"values": "$output_samples_values"
},
"input_samples": "$_id.components.input_samples",
"control_ports": {
"name": "$_id.components.control_ports.name",
"_id": "$_id.components.control_ports._id"
}
}
},
"control_ports_values": "$_id.components.control_ports.values"
}},
{ "$unwind": "$control_ports_values" },
{ "$limit": 5 },
{ "$group": {
"_id": {
"_id": "$_id._id",
"components": {
"uid": "$_id.components.uid",
"type": "$_id.components.type",
"_id": "$_id.components._id",
"execution_times": "$_id.components.execution_times",
"output_samples": {
"name": "$_id.components.output_samples.name",
"_id": "$_id.components.output_samples._id",
"values": "$_id.components.output_samples.values"
},
"input_samples": "$_id.components.input_samples",
"control_ports": {
"name": "$_id.components.control_ports.name",
"_id": "$_id.components.control_ports._id"
}
}
},
"control_ports_values": {"$push": "$control_ports_values" }
}}
])

所有这些只是按前 5 个值对两个数组进行切片。

因此,如果您需要嵌套数组,则在检索结果时在代码中进行“切片”。否则将架构更改为更实际地适合您的目的的内容。

关于MongoDB 查询 - 返回切片数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22453513/

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