- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我正在尝试针对以下架构指定特定查询:
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/
按照目前的情况,这个问题不适合我们的问答形式。我们希望答案得到事实、引用或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the
在编码时,我问了自己这个问题: 这样更快吗: if(false) return true; else return false; 比这个? if(false) return true; return
如何在逻辑条件下进行“返回”? 在这样的情况下这会很有用 checkConfig() || return false; var iNeedThis=doSomething() || return fa
这是我的正则表达式 demo 如问题所述: 如果第一个数字是 1 则返回 1 但如果是 145 则返回 145 但如果是 133 则返回 133 样本数据a: K'8134567 K'81345678
在代码高尔夫问答部分查看谜题和答案时,我遇到了 this solution返回 1 的最长和最晦涩的方法 引用答案, int foo(void) { return! 0; } int bar(
我想在下面返回 JSON。 { "name": "jackie" } postman 给我错误。说明 Unexpected 'n' 这里是 Spring Boot 的新手。 1日龄。有没有正确的方法来
只要“is”返回 True,“==”不应该返回 True 吗? In [101]: np.NAN is np.nan is np.NaN Out[101]: True In [102]: np.NAN
我需要获取所有在 6 号或 7 号房间或根本不在任何房间的学生的详细信息。如果他们在其他房间,简单地说,我不希望有那个记录。 我的架构是: students(roll_no, name,class,.
我有一个表单,我将它发送到 php 以通过 ajax 插入到 mysql 数据库中。一切顺利,php 返回 "true" 值,但在 ajax 中它显示 false 消息。 在这里你可以查看php代码:
我在 Kotlin 中遇到了一个非常奇怪的无法解释的值比较问题,以下代码打印 假 data class Foo ( val a: Byte ) fun main() { val NUM
请注意,这并非特定于 Protractor。问题在于 Angular 2 的内置 Testability service Protractor 碰巧使用。 Protractor 调用 Testabil
在调试窗口中,以下表达式均返回 1。 Application.WorksheetFunction.CountA(Cells(4 + (i - 1) * rows_per_record, 28) & "
我在本地使用 jsonplaceholder ( http://jsonplaceholder.typicode.com/)。我正在通过 extjs rest 代理测试我的 GET 和 POST 调用
这是 Postman 为成功调用我的页面而提供的(修改后的)代码段。 var client = new RestClient("http://sub.example.com/wp-json/wp/v2
这个问题在这里已经有了答案: What to do with mysqli problems? Errors like mysqli_fetch_array(): Argument #1 must
我想我对 C 命令行参数有点生疏。我查看了我的一些旧代码,但无论这个版本是什么,都会出现段错误。 运行方式是 ./foo -n num(其中 num 是用户在命令行中输入的数字) 但不知何故它不起作用
我已经编写了一个类来处理命名管道连接,如果我创建了一个实例,关闭它,然后尝试创建另一个实例,调用 CreateFile() 返回 INVALID_HANDLE_VALUE,并且 GetLastErro
即使 is_writable() 返回 true,我也无法写入文件。当然,该文件存在并且显然是可读的。这是代码: $file = "data"; echo file_get_contents($fil
下面代码中的变量 $response 为 NULL,尽管它应该是 SOAP 请求的值。 (潮汐列表)。当我调用 $client->__getLastResponse() 时,我从 SOAP 服务获得了
我一直在网上的不同论坛上搜索答案,但似乎没有与我的情况相符的... 我正在使用 Windows 7,VS2010。 我有一个使用定时器来调用任务栏刷新功能的应用程序。在该任务栏函数中包含对 LoadI
我是一名优秀的程序员,十分优秀!