通过引用的文档字段进行Mongoid搜索(Mongoid search by referenced documents fields)
我在使用Mongoid的Rails 3应用程序中遇到搜索查询问题。 我的模特是:
class Offer has_many :waypoints end class Waypoint belongs_to :offer field: address field: order, type: Integer end
因此每个Offer都有几个Waypoint,这些Waypoints有订单。 具有最小顺序的航点是起点,具有最大 - 目的地点。
任务:我们有“收件人”和“发件人”地址。 我们需要找到所有优惠,哪些航点以适当的顺序包含这些地址。
问题:我找不到有用的查询。 在mongoid文档中,我发现了类似的东西
Offer.where(“waypoints.address”=>“柏林”)但它仅在嵌入Waypoints时才有效。
您知道哪种解决方案可以解决这个问题?
PS所以我可能会创建另一个带有字段的表WaypointsCache(对于每个提供,将有几个具有不同的from-to组合的缓存):
class WaypointCache field: from field: to field: offer_id end
I`m having a problem with search query in Rails 3 application using Mongoid. My models are:
class Offer has_many :waypoints end class Waypoint belongs_to :offer field: address field: order, type: Integer end
So each Offer has several Waypoints and these Waypoints has order. Waypoint with the smallest order is start point, with the biggest - destination point.
Task: We have "To" and "From" addresses. We need to find all the offers, which waypoints contain these addresses in proper order.
Problem: I can`t find useful query for it. In mongoid documentation I found smth like
Offer.where("waypoints.address" => "Berlin") but it works only if Waypoints were embedded.
Which solutions for such a problem do you know?
P.S. So I probably will create another table WaypointsCache with field like (for each offer there will be several caches with different from-to combos):
class WaypointCache field: from field: to field: offer_id end
原文:https://stackoverflow.com/questions/6650538
最满意答案
你使用固定尺寸的工作,如果它们在你的情况下大于7个字符,它将产生垃圾。
猜猜你想用C ++编写代码,你应该远离C“字符串函数”
#include <iostream> #include <string> using namespace std; void main( ... ) { int my_int = 666; cout << "string: "; string my_string; cin >> my_string; // concat - separated by semicolon in this example stringstream ss; ss << my_string << ";" << my_int; cout << "string and int in one string: " << ss.str().c_str() << endl; // split string tmpStr = ss.str(); size_t found = tmpStr.find(";"); while( found != string::npos ) { cout << tmpStr.substr(0, found).c_str() << endl; tmpStr = tmpStr.substr( found+1 ); found = tmpStr.find(";"); // this case is true for the last part if( found == string::npos ) cout << tmpStr.c_str() << "\n"; } return 0; }
根据你想要用它做什么,你可能需要尝试将每个字符串部分转换为整数,如果失败,你就知道它是一个字符串,否则你可以将它分配给一个整数。
我认为,即使这种方法不是我在生产代码中真正做的,它应该给你一个提示,告诉你如何从那里做你想做的事情。
Your working with fixed sizes and if they are larger then 7 chars in your case, it will produce garbage.
Guess since you want to code in C++, you should stay away from C "string-functions"
#include <iostream> #include <string> using namespace std; void main( ... ) { int my_int = 666; cout << "string: "; string my_string; cin >> my_string; // concat - separated by semicolon in this example stringstream ss; ss << my_string << ";" << my_int; cout << "string and int in one string: " << ss.str().c_str() << endl; // split string tmpStr = ss.str(); size_t found = tmpStr.find(";"); while( found != string::npos ) { cout << tmpStr.substr(0, found).c_str() << endl; tmpStr = tmpStr.substr( found+1 ); found = tmpStr.find(";"); // this case is true for the last part if( found == string::npos ) cout << tmpStr.c_str() << "\n"; } return 0; }
Depending on what you want to do with it you may need to try to convert each string-parts to an integer and if it fails you know its a string otherwise you can assign it to an integer.
I think, even this approach is not what i would really do in productive code, it should give you a hint how to do what you want to do from there.
相关问答
更多-
TCP/IP模型是一个________。[2023-10-02]
a -
下列中不属于面向对象的编程语言的是?[2022-05-30]
a -
假设: char array[7] = "anana"; 然后: memmove(array+1, array, 6); array[0] = 'B'; memmove函数专门用于数据移动涉及重叠的情况。 Assuming: char array[7] = "anana"; Then: memmove(array+1, array, 6); array[0] = 'B'; The memmove function is specifically for cases where the data mo ...
-
javascript中的字符串通过反斜杠( \ )支持转义序列。 例如,如果您需要在字符串中使用选项卡,则可以在字符串中的任何位置添加\t ,并将其替换为选项卡, \n将替换为新行。 test中的反斜杠要么转换为各自的字符,要么被删除,因为它们是无效的转义序列。 为了解决这个问题,您可以使用另一个反斜杠来获取单个正常的反斜杠。 缺点是这不能在javascript中完成。 通常我将我的字符串粘贴到记事本/ N ++ / Code / Sublime并将所有\替换为\\ 由于您正在对字符串进行硬编码,因此需要转 ...
-
如果你想要的是: WriteSetting(70); 要么 WriteSetting(0x46); 做同样的事情 char z = 0x46; unsigned char y = z; 那么你需要做的就是: void WriteSetting(int x) { unsigned char y = x; } 整数没有任何固有的基础 - 它们只是数字。 在unsigned char y = 0x46;之间没有任何区别unsigned char y = 0x46; 和unsigned char y ...
-
一个简单的c ++连接/拆分字符串和一个char数组中的整数(a simple c++ concatenate/split string and integer in a char array)[2024-01-01]
你使用固定尺寸的工作,如果它们在你的情况下大于7个字符,它将产生垃圾。 猜猜你想用C ++编写代码,你应该远离C“字符串函数” #include#include using namespace std; void main( ... ) { int my_int = 666; cout << "string: "; string my_string; cin >> my_string; // concat - separated b ... -
我无法编译您的代码,但这里有一个明显的错误: char arguments[10]; 然后你做的: arguments[i-1]=*(tokens + i); 你正在写一个指向char的指针。 编辑:我已经编译你的代码,有几个错误。 例如,你过早地释放指针。 #include
#include #include #include char** str_split(char* a_str, const ... -
我建议在循环中使用strtol ,明确地使用它的endptr参数,例如: char* pc = input_string; char* end = NULL; while (*pc) { long l = strtol (pc, &end, 0); if (end == pc) break; do_something_useful_with_the_number (l); pc = end; } 如果要累积数组中的所有数字,请替换do_something_useful_with_t ...
-
strcpy(char* destination, const char* source)将source字符串复制到destination指向的数组中。 但是你正在调用strcpy(temp3, message.c_str()); 尝试将字符串复制到指向常量字符串文字的指针: char *temp3 = "empty pointer"; ,导致不确定的行为 [1] 将temp3从指针更改为将使用此字符串文字初始化的数组: char temp3[] = "empty pointer"; 甚至更好:改用std ...
-
%c读取单个字符,而不是字符串。 你看到3只是未定义行为的巧合。 尝试 int num1; char type; int num2; sscanf("1000P3","%d %c %d", &num1, &type, &num2); 并记得使用%c打印字符。 你还应该检查sscanf的返回值; 如果它不是3(因为我们想解析3个项目),那么你的输入是不正确的。 %c reads a single character, not a character string. That you saw 3 was ju ...