我在 mongo db 中有一个学生表,例如
{
"_id": ObjectId("5baa85f61d7859401000002a"),
"Name": "Bella Dave",
"RollNo": 12,
"Class": "Ist",
"TransportDetails": [
{
"RouteId": ObjectId("5baa93a21d7859401000002b"),
"StopId": "abc123",
"Status": "Inactive"
},
{
"RouteId": ObjectId("5baa818d1d78594010000029"),
"StopId": "abc456",
"Status": "Active"
}
]
}
我有这样的路由表
{
"Name": "New york City",
"StopDetails": [
{
"StopId": "abc123",
"Name": "Block no 1"
},
{
"StopId": "abc567",
"Name": "Block no 2"
}
]
我在下面写了这样的聚合查询
$cursor = $this->db->TblStudent->aggregate([
[
'$addFields' => [
'ActiveRouteId' => [
'$map' => [
'input' => '$TransportDetails',
'as' => 'item',
'in' => [
'$cond' => [
['$eq' => ['$$item.Status', "Active"]],
'$$item.RouteId',
false
]
]
]
]
]
],
[
'$addFields' => [
'ActiveStopId' => [
'$map' => [
'input' => '$TransportDetails',
'as' => 'item',
'in' => [
'$cond' => [
['$eq' => ['$$item.Status', "Active"]],
'$$item.StopId',
false
]
]
]
]
]
],
array(
'$lookup' => array(
'from' => 'TblRoute',
'localField' => 'ActiveRouteId',
'foreignField' => '_id',
'as' => 'RouteDetails'
)
),
array(
'$lookup' => array(
'from' => 'TblRoute',
'localField' => 'ActiveStopId',
'foreignField' => 'StopDetails.StopId',
'as' => 'StopDetails'
)
),
])->toArray();
return $cursor;
基本上,我必须获取事件路线和站点信息以及学生数据。因此,我使用 $addFields 和 $map 运算符成功获取了 ActiveRouteId 和 ActiveStopId。基于 ActiveRouteId,我正在执行 $lookup 以获取事件路由信息。我在“RouteDetails”嵌入式文档中成功获得了它。现在我的问题在行中
array(
'$lookup' => array(
'from' => 'TblRoute',
'localField' => 'ActiveStopId',
'foreignField' => 'StopDetails.StopId',
'as' => 'StopDetails'
)
),
此查找未获取任何内容。请帮忙!!!
是否可以同时查找路线和站点信息。我的意思是在路由表中还有许多其他嵌入式文档,如果可以获取所需的嵌入式文档,如
RouteDetails: [
"Name": "New york City",
"StopDetails": [
{
"StopId": "abc123",
"Name": "Block no 1"
}
]
最佳答案
你可以试试下面的聚合
TblStudent.aggregate([
[ "$addFields"=> [
"TransportDetails"=> [
"$cond"=> [
"if"=> [
"$ne"=> [ [ "$type"=> "$TransportDetails" ], "array" ]
],
"then"=> [],
"else"=> "$TransportDetails"
]
]
]],
[ "$addFields"=> [
"ActiveRouteId"=> [
"$filter"=> [
"input"=> "$TransportDetails",
"as"=> "item",
"cond"=> [ "$eq"=> ["$$item.Status", "Active"] ]
]
]
]],
[ "$lookup"=> [
"from"=> "TblRoute",
"let"=> [ "activeRouteId"=> "$ActiveRouteId.RouteId" ],
"pipeline"=> [
[ "$match"=> [ "$expr"=> [ "$in"=> ["$_id", "$$activeRouteId"] ]]]
],
"as"=> "RouteDetails"
]],
[ "$lookup"=> [
"from"=> "TblRoute",
"let"=> [ "activeStopId"=> "$ActiveRouteId.StopId" ],
"pipeline"=> [
[ "$unwind"=> "$StopDetails" ],
[ "$match"=> [ "$expr"=> [ "$in"=> ["$StopDetails.StopId", "$$activeStopId"] ]]],
],
"as"=> "StopDetails"
]]
])
类似于javascript
TblStudent.aggregate([
{ "$addFields": {
"TransportDetails": {
"$cond": {
"if": {
"$ne": [ { "$type": "$TransportDetails" }, "array" ]
},
"then": [],
"else": "$TransportDetails"
}
}
}},
{ "$addFields": {
"ActiveRouteId": {
"$filter": {
"input": "$TransportDetails",
"as": "item",
"cond": { "$eq": ["$$item.Status", "Active"] }
}
}
}},
{ "$lookup": {
"from": "TblRoute",
"let": { "activeRouteId": "$ActiveRouteId.RouteId" },
"pipeline": [
{ "$match": { "$expr": { "$in": ["$_id", "$$activeRouteId"] }}}
],
"as": "RouteDetails"
}},
{ "$lookup": {
"from": "TblRoute",
"let": { "activeStopId": "$ActiveRouteId.StopId" },
"pipeline": [
{ "$unwind": "$StopDetails" },
{ "$match": { "$expr": { "$in": ["$StopDetails.StopId", "$$activeStopId"] }}},
],
"as": "StopDetails"
}}
])
给我以下输出
/* 1 */
{
"_id" : ObjectId("5baa85f61d7859401000002a"),
"Name" : "Bella Dave",
"RollNo" : 12,
"Class" : "Ist",
"TransportDetails" : [
{
"RouteId" : ObjectId("5baa93a21d7859401000002b"),
"StopId" : "abc123",
"Status" : "Inactive"
},
{
"RouteId" : ObjectId("5baa818d1d78594010000029"),
"StopId" : "abc456",
"Status" : "Active"
}
],
"ActiveRouteId" : [
{
"RouteId" : ObjectId("5baa818d1d78594010000029"),
"StopId" : "abc456",
"Status" : "Active"
}
],
"routeDetails" : [
{
"_id" : ObjectId("5baa818d1d78594010000029"),
"Name" : "New york City",
"StopDetails" : [
{
"StopId" : "abc123",
"Name" : "Block no 1"
},
{
"StopId" : "abc567",
"Name" : "Block no 2"
}
]
}
],
"StopDetails" : [
{
"_id" : ObjectId("5baa93a21d7859401000002b"),
"Name" : "New york City",
"StopDetails" : {
"StopId" : "abc456",
"Name" : "Block no 2"
}
}
]
}
关于mongodb - $用 php mongodb 查找,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52511281/
我刚刚被困在这个问题上一段时间了。以这个基地为例: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
我有以下数组: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
我有以下数组:A=[1,2,3,4,5]B=[2,6,7,1]我想找到不相交的元素,如下:output=[3,4,5,6,7]我是这样实现的,output=A+B-(A&B)但它效率低下,因为我添加了两个数组,然后删除了公共(public)元素。它类似于查找不相交的元素。我能做得比这更好吗?如果是,怎么办? 最佳答案 如何只选择A中的元素而不是B中的元素以及B中的元素而不是A中的元素。(A-B)+(B-A) 关于arrays-在两个数组中查找不相交元素的有效方法是什么?,我们在Stack
给定一个数组['a','b','c','d','e','f'],我如何获得包含两个的所有子集的列表、三、四元素?我是Ruby的新手(从C#迁移过来),不确定“Ruby之道”是什么。 最佳答案 查看Array#combination然后是这样的:2.upto(4){|n|array.combination(n)} 关于ruby-使用Ruby在数组中查找大小为N的所有子集,我们在StackOverflow上找到一个类似的问题: https://stackoverf