我开始MongoDB在我的应用程序中使用数据库,为了数据访问,我选择了Spring Data for MongoDB.
我浏览了API参考和文档,我可以看到有map-reduce集成但是聚合框架呢?我可以看到它支持group by操作,这表明它支持$group运营商判断:http://docs.mongodb.org/manual/reference/sql-aggregation-comparison/,但是其他运营商呢,那是现在不支持?
我问的是这个问题,因为我想知道与MongoDBSping Data 有什么样的集成,所以我知道会发生什么,可以这么说.
我有一个看起来像这样的集合
{
"_class" : "User",
"_id" : "id1",
"places" : [
{
"_id" : "1",
"address" : "test1",
"location" : {
"latitude" : 1,
"longitude" : 1
}
},
{
"_id" : "2",
"address" : "test2",
"location" : {
"latitude" : 2,
"longitude" : 2
}
},...
]
}
Run Code Online (Sandbox Code Playgroud)
我试图检索用户的每个地方(在2公里的范围内).此查询不起作用:
db.users.ensureIndex({"places.location":"2d"})
db.users.aggregate([
{$match : { "_id" : "id1" } },
{$unwind : "$places"},
{$project:{_id:0, places:1},
{$match :
{"places.location" :
{ $near :
{ $geometry :
{ type : "2d" , …Run Code Online (Sandbox Code Playgroud) 我有一个聚合管道,其中包含一个这样的项目:
$project: {
start: {
$cond: {
if: {
$eq: ["$start", "EARLY"]
},
then: "$deltastart.start",
else: "$deltastart.end"
}
},...
},...
Run Code Online (Sandbox Code Playgroud)
在mongo shell中工作正常.如何使用Spring-Mongodb中的Aggregation框架来表达这一点?我见过ProjectionOperationBuilder,ExpressionProjectionOperationBuilder类型但不是一个例子如何使用它们...有什么建议吗?
我正在构建将在意大利使用的mongodb和nodejs中的应用程序.意大利时区是+02:00.这意味着如果任何人在7月11日凌晨01点保存一些数据,那么它将被保存为7月10日晚上11点,因为mongo以UTC格式保存日期.我们需要显示日期明智的tx计数.所以我按日期查询了.但它显示前一天的tx.这应该是什么解决方法.
> db.txs.insert({txid:"1",date : new Date("2015-07-11T01:00:00+02:00")})
> db.txs.insert({txid:"2",date : new Date("2015-07-11T05:00:00+02:00")})
> db.txs.insert({txid:"3",date : new Date("2015-07-10T21:00:00+02:00")})
> db.txs.find().pretty()
{
"_id" : ObjectId("55a0a55499c6740f3dfe14e4"),
"txid" : "1",
"date" : ISODate("2015-07-10T23:00:00Z")
}
{
"_id" : ObjectId("55a0a55599c6740f3dfe14e5"),
"txid" : "2",
"date" : ISODate("2015-07-11T03:00:00Z")
}
{
"_id" : ObjectId("55a0a55699c6740f3dfe14e6"),
"txid" : "3",
"date" : ISODate("2015-07-10T19:00:00Z")
}
> db.txs.aggregate([
{ $group:{
_id: {
day:{$dayOfMonth:"$date"},
month:{$month:"$date"},
year:{$year:"$date"}
},
count:{$sum:1}
}}
])
{ "_id" : { "day" : 11, "month" : 7, "year" : 2015 }, "count" : …Run Code Online (Sandbox Code Playgroud) datetime mongodb node.js mongodb-query aggregation-framework
我正在重新解决这个问题,因为我认为这个问题应该是从这个与mongodb-know-index-of-array-element-matched-with-in-operator相关的单独线程.
我正在使用mongoDB,实际上我正在使用查找,更新等简单查询来编写所有查询(无聚合).现在我读了许多SO帖子,看看这个例如mongodb-aggregation-match-vs-find-speed.现在我想到为什么增加服务器上的计算时间,因为好像我将计算更多,然后我的服务器负载将变得更多,所以我尝试使用聚合,我认为我现在正朝着正确的方向.但是后来我之前的问题andreas-limoli告诉我没有使用聚合,因为它很慢并且在服务器上使用简单的查询和计算.现在字面上,我正在处理我应该使用什么的delimma,我正在使用mongoDB一年,但是当数据大小增加时我对它的性能没有任何了解,所以我完全不知道应该选择哪一个.
还有一件事我没有在任何地方找到,如果聚合比因为$ lookup而不是因为$ lookup,因为$ lookup是我考虑使用聚合的最重要的事情因为否则我必须连续执行许多查询然后计算在服务器上,在聚合面前我看起来很差.
我还读到了将数据从一个管道传递到另一个管道时对mongodb聚合的100MB限制,因此人们如何有效地处理这种情况,以及如果他们打开磁盘使用情况,那么因为磁盘使用速度比人们处理这种情况的速度慢.
此外,我获取了30,000个样本集合,并尝试使用$ match运行聚合并查找查询,我发现聚合比查找查询要快一点,聚合需要180ms才能执行,因为查找需要220毫秒才能执行.
请帮助我,请大家帮助我.
结构或多或少像;
[
{id: 1, name: "alex" , children: [2, 4, 5]},
{id: 2, name: "felix", children: []},
{id: 3, name: "kelly", children: []},
{id: 4, name: "hannah", children: []},
{id: 5, name: "sonny", children: [6]},
{id: 6, name: "vincenzo", children: []}
]
Run Code Online (Sandbox Code Playgroud)
children当children数组不为空时,我想用名称替换id .
所以查询的结果是预期的;
[ {id: 1, name: "alex" , children: ["felix", "hannah" , "sonny"]}
{id: 5, name: "sonny", children: ["vincenzo"]}
]
Run Code Online (Sandbox Code Playgroud)
我做了什么来实现这一目标;
db.list.aggregate([
{$lookup: { from: "list", localField: "id", foreignField: "children", as: "children" }},
{$project: …Run Code Online (Sandbox Code Playgroud) {
"ArticleName": "Example Article",
"Comments": [
{
"Text": "Great Article",
"Responses": [
{
"Text": "No it isnt",
"Responses": [
{
"Text": "Yes it is"
}
]
},
{
"Text": "Spot on"
}
]
}
]
}
Run Code Online (Sandbox Code Playgroud)
每次出现的关键"文本"都会被视为注释(因此有4条评论).在Mongo中,最好的方法是什么?
我想在mongodb中使用正则表达式在数组内部进行查询,这些集合包含如下文档:
{
"_id" : ObjectId("53340d07d6429d27e1284c77"),
"company" : "New Company",
"worktypes" : [
{
"name" : "Pompas",
"works" : [
{
"name" : "name 2",
"code" : "A00011",
"price" : "22,22"
},
{
"name" : "name 3",
"code" : "A00011",
"price" : "22,22"
},
{
"name" : "name 4",
"code" : "A00011",
"price" : "22,22"
},
{
"code" : "asdasd",
"name" : "asdads",
"price" : "22"
},
{
"code" : "yy",
"name" : "yy",
"price" : "11"
}
]
},
{ …Run Code Online (Sandbox Code Playgroud) 我想从我的数据库中的集合中检索字段的区域值.该distinct命令是显而易见的解决方案.问题是某些字段具有大量可能的值并且不是简单的原始值(即,复杂的子文档而不仅仅是字符串).这意味着结果很大,导致我将结果传递给客户端.
显而易见的解决方案是对得到的不同值进行分页.但我找不到最佳方法来做到这一点.由于distinct没有分页选项(限制,跳过等),我转向聚合框架.我的基本管道是:
[
{$match: {... the documents I am interested in ...}},
{$group: {_id: '$myfield'},
{$sort: {_id: 1},
{$limit: 10},
]
Run Code Online (Sandbox Code Playgroud)
这给了我前10个唯一值myfield.为了获得下一页,将向管道中添加$ skip运算符.所以:
[
{$match: {... the documents I am interested in ...}},
{$group: {_id: '$myfield'},
{$sort: {_id: 1},
{$skip: 10},
{$limit: 10},
]
Run Code Online (Sandbox Code Playgroud)
但有时候,我从中收集唯一值的字段是一个数组.这意味着我必须在分组之前解开它.所以:
[
{$match: {... the documents I am interested in ...}},
{$unwind: '$myfield'}
{$group: {_id: '$myfield'},
{$sort: {_id: 1},
{$skip: 10},
{$limit: 10},
]
Run Code Online (Sandbox Code Playgroud)
其他时候,我获得唯一值的字段可能不是数组,但它的父节点可能是一个数组.所以:
[
{$match: {... the documents …Run Code Online (Sandbox Code Playgroud) 我试图使用以下代码在现有的Mongo DB集合上执行多次插入
db.dados_meteo.aggregate( [
{ $match : { "POM" : "AguiardaBeira" } },
{ $project : {
_id : { $concat: [
"0001:",
{ $substr: [ "$DTM", 0, 4 ] },
{ $substr: [ "$DTM", 5, 2 ] },
{ $substr: [ "$DTM", 8, 2 ] },
{ $substr: [ "$DTM", 11, 2 ] },
{ $substr: [ "$DTM", 14, 2 ] },
{ $substr: [ "$DTM", 17, 2 ] }
] },
"RNF" : 1, "WET":1,"HMD":1,"TMP":1 } },
{ …Run Code Online (Sandbox Code Playgroud) mongodb ×10
performance ×2
arrays ×1
datetime ×1
lookup ×1
node.js ×1
regex ×1
spring ×1
spring-data ×1