Json 返回在mongoDB中具有数组的字段,并返回该数组中的第一个和最后一个值 场景:考虑在名为TwittCur.集合中的MunGDB中存在的文档。 { "_id" : ObjectId("53d1340478441a1c0d25c40c"), "items" : [ { "date" : ISODate("2014-07-22T22:18:05.000Z"), "value" : 4, "_id" : ObjectId("53d134048b3956000063aa72") }, { "date" : ISODate("2014-07-21T22:09:20.000Z"), "value" : 10, "_id" : ObjectId("53d134048b3956000063aa71") } ... ], "ticker" : "OM:A1M"
} 我只想获取“项目”中的第一个和最后一个日期。。我试过很多不同的“查询”。但我不能把它弄对。“股票代码”是唯一的 下面的查询是唯一返回某些内容的查询,但它返回了所有(预期的)内容 所以,最后我希望查询返回类似这样的内容[2013-02-012014-07-24];Json 返回在mongoDB中具有数组的字段,并返回该数组中的第一个和最后一个值 场景:考虑在名为TwittCur.集合中的MunGDB中存在的文档。 { "_id" : ObjectId("53d1340478441a1c0d25c40c"), "items" : [ { "date" : ISODate("2014-07-22T22:18:05.000Z"), "value" : 4, "_id" : ObjectId("53d134048b3956000063aa72") }, { "date" : ISODate("2014-07-21T22:09:20.000Z"), "value" : 10, "_id" : ObjectId("53d134048b3956000063aa71") } ... ], "ticker" : "OM:A1M",json,node.js,mongodb,mongoose,aggregation-framework,Json,Node.js,Mongodb,Mongoose,Aggregation Framework,} 我只想获取“项目”中的第一个和最后一个日期。。我试过很多不同的“查询”。但我不能把它弄对。“股票代码”是唯一的 下面的查询是唯一返回某些内容的查询,但它返回了所有(预期的)内容 所以,最后我希望查询返回类似这样的内容[2013-02-012014-07-24]; 我真的需要这方面的帮助,手动/核心/聚合上的所有链接都是紫色的,我不知道从哪里可以获得更多信息。很难说您的意图是使用符合您条件的单个文档还是多个文档。正如所建议的那样,单个文档实际上只需要在单个结果上使用JavaScript固有的s
我真的需要这方面的帮助,手动/核心/聚合上的所有链接都是紫色的,我不知道从哪里可以获得更多信息。很难说您的意图是使用符合您条件的单个文档还是多个文档。正如所建议的那样,单个文档实际上只需要在单个结果上使用JavaScript固有的
shift
和pop
方法来获取数组的第一个和最后一个元素。您可能还需要在此处使用数组排序
twitCount.findOne({ "ticker": "OM:A1M" },function(err,doc) {
doc.items = doc.items.sort(function(a,b) {
return ( a.date.valueOf() > b.date.valueOf() ) ? 1
: ( a.date.valueOf() < b.date.valueOf() ) ? -1 : 0;
});
doc.items = [doc.items.shift(),doc.items.pop()];
console.log( doc );
})
如果您真的希望将“项”作为数组返回,那么您可以稍微改变一下:
twitCount.aggregate([
// Match your "documents" first
{ "$match": { "ticker": "OM:A1M" } },
// Unwind the array
{ "$unwind": "$items" },
// Sort the values
{ "$sort": { "items.date": 1 } },
// Group with $first and $last items
{ "$group": {
"_id": "$ticker",
"first": { "$first": "$items" },
"last": { "$last": "$items" },
"type": { "$first": { "$const": [true,false] } }
}},
// Unwind the "type"
{ "$unwind": "$type" },
// Conditionally push to the array
{ "$group": {
"_id": "$_id",
"items": {
"$push": {
"$cond": [
"$type",
"$first",
"$last"
]
}
}
}}
],function(err,result) {
或者,如果您的语句仅用于选择,并且希望从每个文档“\u id”中选择“first”和“last”,则只需将首字母中的键更改为“$\u id”,而不是“$ticker”字段值:
twitCount.aggregate([
// Match your "documents" first
{ "$match": { "ticker": "OM:A1M" } },
// Unwind the array
{ "$unwind": "$items" },
// Sort the values
{ "$sort": { "items.date": 1 } },
// Group with $first and $last items
{ "$group": {
"_id": "$_id",
"ticker": { "$first": "$ticker" },
"first": { "$first": "$items" },
"last": { "$last": "$items" },
"type": { "$first": { "$const": [true,false] } }
}},
// Unwind the "type"
{ "$unwind": "$type" },
// Conditionally push to the array
{ "$group": {
"_id": "$_id",
"ticker": { "$first": "$ticker" },
"items": {
"$push": {
"$cond": [
"$type",
"$first",
"$last"
]
}
}
}}
],function(err,result) {
在最后一种情况下,根据您提供的数据,您将得到如下结果:
{
"_id" : ObjectId("53d1340478441a1c0d25c40c"),
"ticker" : "OM:A1M",
"items" : [
{
"date" : ISODate("2014-07-21T22:09:20Z"),
"value" : 10,
"_id" : ObjectId("53d134048b3956000063aa71")
},
{
"date" : ISODate("2014-07-22T22:18:05Z"),
"value" : 4,
"_id" : ObjectId("53d134048b3956000063aa72")
}
]
}
您可以在文档中找到。值得了解的是,根据您所做的工作,聚合框架是一个非常有用的工具。我很懒,只需投影整个数组,然后对结果执行shift和pop^。^Mongodb有一个示例,其中一个示例是删除数组的第一个元素,如果可以的话,我会发布一个真实的答案,但现在就用它来玩吧。另外,这里有一个使用slice解决类似问题的答案:@JakeSellers你的第一个陈述可能是正确的,但其他两个不适用
$pop
将修改集合中存储的文档,$slice
不能在一次查询中同时返回“first”和“last”。因此,选择实际上可以归结为一个文档的JavaScript或解释多个文档的聚合过程。同意,$pop在阅读文档后立即出现,我花了一些时间玩$slice和不同的组合,但无法让它工作。
twitCount.aggregate([
// Match your "documents" first
{ "$match": { "ticker": "OM:A1M" } },
// Unwind the array
{ "$unwind": "$items" },
// Sort the values
{ "$sort": { "items.date": 1 } },
// Group with $first and $last items
{ "$group": {
"_id": "$_id",
"ticker": { "$first": "$ticker" },
"first": { "$first": "$items" },
"last": { "$last": "$items" },
"type": { "$first": { "$const": [true,false] } }
}},
// Unwind the "type"
{ "$unwind": "$type" },
// Conditionally push to the array
{ "$group": {
"_id": "$_id",
"ticker": { "$first": "$ticker" },
"items": {
"$push": {
"$cond": [
"$type",
"$first",
"$last"
]
}
}
}}
],function(err,result) {
{
"_id" : ObjectId("53d1340478441a1c0d25c40c"),
"ticker" : "OM:A1M",
"items" : [
{
"date" : ISODate("2014-07-21T22:09:20Z"),
"value" : 10,
"_id" : ObjectId("53d134048b3956000063aa71")
},
{
"date" : ISODate("2014-07-22T22:18:05Z"),
"value" : 4,
"_id" : ObjectId("53d134048b3956000063aa72")
}
]
}