我想将 building 与 floors 和 rooms 聚合:
给定以下 3 个集合:
/* buildings */
{
"_id" : ObjectId("59a09abe388f595b15bb5fa6"),
"name" : "home"
}
/* floors */
{
"_id" : ObjectId("59a09abe388f595b15bb5fa3"),
"buildingId" : ObjectId("59a09abe388f595b15bb5fa6"),
"name" : "upstairs"
}
{
"_id" : ObjectId("59a09abe388f595b15bb5fa2"),
"buildingId" : ObjectId("59a09abe388f595b15bb5fa6"),
"name" : "downstairs"
}
/* rooms */
{
"_id" : ObjectId("59a09bce388f595b15bb5fb6"),
"floorId" : ObjectId("59a09abe388f595b15bb5fa3"),
"name" : "bathroom",
"_userId" : ObjectId("590a08dba07c1a1bee87b310")
}
{
"_id" : ObjectId("59a09bce388f595b15bb5fc6"),
"floorId" : ObjectId("59a09abe388f595b15bb5fa3"),
"name" : "living room",
"_userId" : ObjectId("590a08dba07c1a1bee87b310")
}
我想一起查找它们并尝试使用以下查询:
db.getCollection('buildings').aggregate([
{
"$lookup": {
"from": "floors",
"localField": "_id",
"foreignField": "buildingId",
"as": "floors"
}
},
{
"$lookup": {
"from": "rooms",
"localField": "floors._id",
"foreignField": "floorId",
"as": "floors.rooms"
}
}
]);
结果如下:
{
"_id" : ObjectId("59a09abe388f595b15bb5fa6"),
"_userId" : ObjectId("590a08dba07c1a1bee87b310"),
"name" : "home",
"floors" : {
"rooms" : []
}
}
但我会得到以下结果:
{
"_id" : ObjectId("59a09abe388f595b15bb5fa6"),
"_userId" : ObjectId("590a08dba07c1a1bee87b310"),
"name" : "home",
"floors" : [
{
"_id" : ObjectId("59a09abe388f595b15bb5fa3"),
"buildingId" : ObjectId("59a09abe388f595b15bb5fa6"),
"name" : "upstairs",
"rooms": [
{
"_id" : ObjectId("59a09bce388f595b15bb5fb6"),
"floorId" : ObjectId("59a09abe388f595b15bb5fa3"),
"name" : "bathroom"
},
{
"_id" : ObjectId("59a09bce388f595b15bb5fc6"),
"floorId" : ObjectId("59a09abe388f595b15bb5fa3"),
"name" : "living room"
}
]
},
{
"_id" : ObjectId("59a09abe388f595b15bb5fa2"),
"buildingId" : ObjectId("59a09abe388f595b15bb5fa6"),
"name" : "downstairs",
"rooms" : [ ]
}
]
}
如您所见,我想查找所有引用资料以获取建筑结构及其楼层和房间。
我怎样才能做到这一点?
最佳答案
看一眼您的聚合 查询及其结果的输出类型,我觉得 不支持双重 $lookup(在另一个查找中查找) MongoDB(直到 3.4 版本)。因此,您最好的选择是使用 $unwind 并使结果更接近您的预期。
这里是查询:
db.getCollection('buildings').aggregate([
{
"$lookup": {
"from": "floors",
"localField": "_id",
"foreignField": "buildingId",
"as": "floors"
}
},
{"$unwind":"$floors"},
{
"$lookup": {
"from": "rooms",
"localField": "floors._id",
"foreignField": "floorId",
"as": "floors.rooms"
}
}
]);
及其输出:
{
"_id" : ObjectId("59a09abe388f595b15bb5fa6"),
"name" : "home",
"floors" : {
"_id" : ObjectId("59a09abe388f595b15bb5fa3"),
"buildingId" : ObjectId("59a09abe388f595b15bb5fa6"),
"name" : "upstairs",
"rooms" : [
{
"_id" : ObjectId("59a09bce388f595b15bb5fb6"),
"floorId" : ObjectId("59a09abe388f595b15bb5fa3"),
"name" : "bathroom",
"_userId" : ObjectId("590a08dba07c1a1bee87b310")
},
{
"_id" : ObjectId("59a09bce388f595b15bb5fc6"),
"floorId" : ObjectId("59a09abe388f595b15bb5fa3"),
"name" : "living room",
"_userId" : ObjectId("590a08dba07c1a1bee87b310")
}
]
}
}
{
"_id" : ObjectId("59a09abe388f595b15bb5fa6"),
"name" : "home",
"floors" : {
"_id" : ObjectId("59a09abe388f595b15bb5fa2"),
"buildingId" : ObjectId("59a09abe388f595b15bb5fa6"),
"name" : "downstairs",
"rooms" : [ ]
}
}
关于MongoDB 聚合查找,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46142330/
我刚刚被困在这个问题上一段时间了。以这个基地为例:moduleTopclassTestendmoduleFooendend稍后,我可以通过这样做在Foo中定义扩展Test的类:moduleTopmoduleFooclassSomeTest但是,如果我尝试通过使用::指定模块来最小化缩进:moduleTop::FooclassFailure这失败了:NameError:uninitializedconstantTop::Foo::Test这是一个错误,还是仅仅是Ruby解析变量名的方式的逻辑结果? 最佳答案 Isthisabug,or
我正在尝试解析一个CSV文件并使用SQL命令自动为其创建一个表。CSV中的第一行给出了列标题。但我需要推断每个列的类型。Ruby中是否有任何函数可以找到每个字段中内容的类型。例如,CSV行:"12012","Test","1233.22","12:21:22","10/10/2009"应该产生像这样的类型['integer','string','float','time','date']谢谢! 最佳答案 require'time'defto_something(str)if(num=Integer(str)rescueFloat(s
我有一个应用需要发送用户事件邀请。当用户邀请friend(用户)参加事件时,如果尚不存在将用户连接到该事件的新记录,则会创建该记录。我的模型由用户、事件和events_user组成。classEventdefinvite(user_id,*args)user_id.eachdo|u|e=EventsUser.find_or_create_by_event_id_and_user_id(self.id,u)e.save!endendend用法Event.first.invite([1,2,3])我不认为以上是完成我的任务的最有效方法。我设想了一种方法,例如Model.find_or_cr
我想找到给定字符串中的所有匹配项,包括重叠匹配项。我怎样才能实现它?#Example"a-b-c-d".???(/\w-\w/)#=>["a-b","b-c","c-d"]expected#Solutionwithoutoverlappedresults"a-b-c-d".scan(/\w-\w/)#=>["a-b","c-d"],but"b-c"ismissing 最佳答案 在积极的前瞻中使用捕获:"a-b-c-d".scan(/(?=(\w-\w))/).flatten#=>["a-b","b-c","c-d"]参见Rubyde
这应该是一个简单的问题,但我找不到任何相关信息。给定一个Ruby中的正则表达式,对于每个匹配项,我需要检索匹配的模式$1、$2,但我还需要匹配位置。我知道=~运算符为我提供了第一个匹配项的位置,而string.scan(/regex/)为我提供了所有匹配模式。如果可能,我需要在同一步骤中获得两个结果。 最佳答案 MatchDatastring.scan(regex)do$1#Patternatfirstposition$2#Patternatsecondposition$~.offset(1)#Startingandendingpo
不知何故,我似乎无法获得包含我的聚合的响应...使用curl它按预期工作:HBZUMB01$curl-XPOST"http://localhost:9200/contents/_search"-d'{"size":0,"aggs":{"sport_count":{"value_count":{"field":"dwid"}}}}'我收到回复:{"took":4,"timed_out":false,"_shards":{"total":5,"successful":5,"failed":0},"hits":{"total":90,"max_score":0.0,"hits":[]},"a
我有以下数组:arr=[1,3,2,5,2,4,2,2,4,4,2,2,4,2,1,5]我想要一个包含前三个奇数元素的数组。我知道我可以做到:arr.select(&:odd?).take(3)但我想避免遍历整个数组,而是在找到第三个匹配项后返回。我想出了以下解决方案,我相信它可以满足我的要求:my_arr.each_with_object([])do|el,memo|memo但是有没有更简单/惯用的方法来做到这一点? 最佳答案 使用lazyenumerator与Enumerable#lazy:arr.lazy.select(&:o
假设您有一个可执行文件foo.rb,其库bar.rb的布局如下:/bin/foo.rb/lib/bar.rb在foo.rb的header中放置以下要求以在bar.rb中引入功能:requireFile.dirname(__FILE__)+"../lib/bar.rb"只要对foo.rb的所有调用都是直接的,这就可以正常工作。如果你把$HOME/project和符号链接(symboliclink)foo.rb放入$HOME/usr/bin,然后__FILE__解析为$HOME/usr/bin/foo.rb,因此无法找到bar.rb关于foo.rb的目录名.我意识到像rubygems这
是否有内置的Ruby方法或众所周知的库可以返回对象的整个方法查找链?Ruby查看一系列令人困惑的类(如thisquestion中所讨论)以查找与消息对应的实例方法,如果没有类响应消息,则调用接收方的method_missing。我将以下代码放在一起,但我确信它遗漏了某些情况或者它是否100%正确。请指出任何缺陷并指导我找到一些更好的代码(如果存在)。defmethod_lookup_chain(obj,result=[obj.singleton_class])ifobj.instance_of?Classreturnadd_modules(result)ifresult.last==B
什么是Linq聚合方法的ruby等价物。它的工作原理是这样的varfactorial=new[]{1,2,3,4,5}.Aggregate((acc,i)=>acc*i);每次将数组序列中的值传递给lambda时,变量acc都会累积。 最佳答案 这在数学以及几乎所有编程语言中通常称为折叠。它是更普遍的变形概念的一个实例。Ruby从Smalltalk中继承了这个特性的名称,它被称为inject:into:(像aCollectioninject:aStartValueinto:aBlock一样使用。)所以,在Ruby中,它称为inj