我正在使用mongo 2.2.3和java驱动程序.我的困境,我必须把一个字段和值推入一个数组,但我似乎无法弄清楚如何做到这一点.我的数据样本:
"_id" : 1,
"scores" : [
{
"type" : "homework",
"score" : 78.97979
},
{
"type" : "homework",
"score" : 6.99
},
{
"type" : "quiz",
"score" : 99
}
]
Run Code Online (Sandbox Code Playgroud)
我可以$推入shell:
db.collection.update({_id:1},{$push:{scores:{type:"quiz", score:99}}})
Run Code Online (Sandbox Code Playgroud)
但是当我把它翻译成java时,我迷惑自己并将键盘放在墙上.
到目前为止我的java代码(不完整和错误):
DBObject find = new BasicDBObject("_id", 1);
DBObject push = new BasicDBObject("$push", new BasicDBObject(
"scores", new BasicDBObject()));
Run Code Online (Sandbox Code Playgroud) 我正在尝试重新构建一个SQL数据库Cassandra,以便我可以找到与SQL查询等效的Cassandra.我使用CQL 3和Cassandra v1.2.我在cassandra中对db设计进行了建模,以便它支持子句和非规范化表的顺序以支持连接操作.但是,当谈到DISTINCT,SUM()和GROUPBY等价时,我在海上
SELECT a1,MAX(b1) FROM demo1 group by a1.
SELECT DISTINCT (a2) FROM demo2 where b2='sea'
SELECT sum(a3), sum(b3) from demo3 where c3='water' and d3='ocean'
Run Code Online (Sandbox Code Playgroud)
在过去的几天里,这就像我的工作一样.在Cassandra中有没有办法,我可以建模db模式来支持这类查询?我无法想象卡桑德拉的任何方式.如何使用Cassandra实现此类查询?
我读到Cassandra上的hive层可能会使这些查询起作用.我只是想知道这是否是在Cassandra中支持此类查询的唯一方法..?请建议任何其他可能的方法..
我需要在ES中使用3个字段聚合(分组).
我可以在1个查询中执行此操作,还是需要为每列使用facet + iterate?
谢谢
我们将 Cassandra 用于 OLTP DB、存储 DB 事务并评估报告解决方案的要求。
我们正在评估使用 Cassandra 来报告具有扁平架构的数据库。
使用 Cassandra 作为报告数据库的优势/缺陷是什么?
我正在努力在PostgreSQL数据库中的JSONB字段上进行聚合.这可能更容易用一个例子解释,所以如果创建并填充一个名为analysis2列(id和analysis)的表,如下所示: -
create table analysis (
id serial primary key,
analysis jsonb
);
insert into analysis
(id, analysis) values
(1, '{"category" : "news", "results" : [1, 2, 3, 4, 5 , 6, 7, 8, 9, 10, 11, 12, 13, 14, null, null]}'),
(2, '{"category" : "news", "results" : [11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, null, 26]}'),
(3, '{"category" : "news", "results" : [31, 32, 33, 34, …Run Code Online (Sandbox Code Playgroud) 我是 MongoDB 的新手,所以我不确定我是否正确地表达了我的问题。
我有一个集合,其中的数据如下所示:
{
"_id" : ObjectId("66666"),
"Id" : 994,
"PostType" : 1,
"AnswerId" : 334,
"CreationDate" : ISODate("1994-09-05T09:34:36.177+0000"),
"Tags" : "test",
"Parent" : null,
}
{
"_id" : ObjectId("8888"),
"Id" : 334,
"PostTypeId" : 2,
"AnswerId" : NaN,
"CreationDate" : ISODate("20005-08-03T11:29:42.880+0000"),
"ParentId" : 994
}
Run Code Online (Sandbox Code Playgroud)
两个文档都在同一个集合中,我尝试使用 PostType:1 文档中的 AnswerId,并使用该值作为主 ID 来提取其 CreationDate。
这是我试图实现的逻辑,但它似乎不起作用:
db.collection.aggregate([
{$project:{_id:"$Id", Title:"$Title", Answerid:"$AnswerId", QuestionDate:"$CreationDate"}},
{$match:{Id:"$Answerid"}},
{$project:{AnswerDate:"$CreationDate"}}
])
Run Code Online (Sandbox Code Playgroud)
我愿意接受任何建议。
mongodb mongo-shell mongodb-query nosql-aggregation aggregation-framework
谁能解释一下为什么在 Java 中,当我使用“$out”进行聚合管道时,当我只写以下内容时,不会将结果写入新集合中:
Document match = new Document("$match", new Document("top_speed",new Document("$gte",350)));
Document out=new Document("$out", "new_collection");
coll.aggregate(Arrays.asList(
match,out
)
);
Run Code Online (Sandbox Code Playgroud)
当我保存聚合结果并对其进行迭代时,会创建新集合并且匹配的结果在里面(在这种情况下,Java 显然有错误):
AggregateIterable<Document> resultAgg=
coll.aggregate(Arrays.asList(
match,out
)
);
for (Document doc : resultAgg){
System.out.println("The result of aggregation match:-"+ doc.toJson());
}
Run Code Online (Sandbox Code Playgroud)
我不明白为什么。
我是Cassandra的新手,并使用linq为我的Cassandra数据库创建了一个通用存储库。对于我的Single()方法,我将where标准作为参数传递。
这是我的单一方法:
Single(Expression<Func<T, bool>> exp)
Run Code Online (Sandbox Code Playgroud)
这是我用来查询cassandra数据库的linq代码
public async Task<T> Single(Expression<Func<T, bool>> exp)
{
return await GetTable.Where<T>(exp).FirstOrDefault().ExecuteAsync();
}
Run Code Online (Sandbox Code Playgroud)
这是调用单个方法的方法
public override Task OnConnected()
{
if (Context.User != null)
{
string userName = Context.User.Identity.Name;
this.Groups.Add(userName, Context.ConnectionId);
***** this is the line with the issue ******
Notification notification = Task.Run(() => _notificationRepository.Single(x => x.UserName.Equals(userName))).Result;
if (notification == null)
{
_notificationRepository.CreateInstance(new NotificationUserMapping { Connections = new string[] { Context.ConnectionId }, UserName = Context.User.Identity.Name });
}
else if (!notification.Connections.Contains(Context.ConnectionId))
{
notification.Connections.Add(Context.ConnectionId);
_notificationRepository.Save(notification);
}
}
return …Run Code Online (Sandbox Code Playgroud) 这是我正在使用的集合:
- sign1: A1
- sign2: A2
- sign1: A2
- sign2: A5
- sign1: A2
- sign2: A6
- sign1: A2
- sign2: A8
- sign1: A5
- sign2: A8
Run Code Online (Sandbox Code Playgroud)
查询应该找到从 A1 到 A8 的路径
例如,它应该找到:
path1:
A1 A2 -> A2 A5 -> A5 A8
path2:
A1 A2 -> A2 A8
path3:
A1 A2 -> A2 A6 -> should be ignored since not finishes with A8
Run Code Online (Sandbox Code Playgroud)
目前,我尝试了这个(@ray 的部分解决方案):
我的查询的第一个问题是它返回所有路径,即使它不以 A8 结尾
第二个问题是不分离路径将所有内容放在一个数组中
mongodb mongodb-query nosql-aggregation aggregation-framework
MongoDB 聚合管道的各个阶段始终按顺序执行。管道处理的文档可以在阶段之间更改吗?例如,如果stage1与collection1中的某些文档匹配,而stage2与collection2中的某些文档匹配,是否可以在stage1期间或之后(即stage2之前)写入collection2中的某些文档?如果是这样,可以防止这种行为吗?
为什么这很重要:假设stage2是一个$lookup阶段。Lookup 是相当于 SQL join 的 NoSQL。在典型的 SQL 数据库中,连接查询与写入是隔离的。这意味着在解析连接时,受连接影响的数据不能更改。我想知道 MongoDB 是否有同样的保证。请注意,我来自 noSQL 世界(只是不是 MongoDB),并且我很好地理解这个范例。不需要建议例如复制数据,如果有这样的解决方案,我就不会这么问。
根据我的研究,MongoDb 读取查询获取共享(读取)锁,该锁可防止在同一集合上写入,直到解决为止。然而,MongoDB 文档没有提及任何有关聚合管道锁的内容。聚合管道是否对其读取的所有集合持有读(共享)锁?或者只是当前管道阶段使用的集合?
更多上下文:我需要通过多个集合运行具有多个“连接”的“查询”。查询是动态生成的,我不预先知道将“连接”哪些集合。聚合管道是实现这一点的假定方法。但是,为了获得一致的“查询”数据,我需要确保管道各阶段之间不会交错写入。
$match例如,阶段之间的删除$lookup可能会删除已连接(“查找”)的文档之一,从而导致整个结果不正确/不一致。这会发生吗?如何预防呢?
database mongodb nosql nosql-aggregation aggregation-framework
mongodb ×5
nosql ×4
cassandra ×3
java ×2
c# ×1
cql ×1
cql3 ×1
database ×1
jongo ×1
jsonb ×1
linq ×1
mongo-java ×1
mongo-shell ×1
postgresql ×1