i am trying to filter with multiple value in loopback 3. 'AND condition' is not working. Numeric value filter working good but string value not matched.
this is my condition is now :
{ "and": [{ "id": { "neq": "5d146f1f7a62651b8162fcf3" } },
{ "gender": "male" }, { "age": { "between": [16, 60] } },
{ "interestId": { "inq": ["5d11c0ce59e2b64e8dc12956", "5d11c0e259e2b64e8dc12958", "5d11c08659e2b64e8dc12953", "5d11c01759e2b64e8dc1294b", "5d11c03759e2b64e8dc1294d"] } },
{ "location": { "near": { "lat": 26.8574194, "lng": 75.7977288 }, "maxDistance": 6, "unit": "kilometers" } },
{ "maritalStatus": { "like": {} } }, { "college": { "like": {} } },
{ "career": { "like": {} } }, { "currentJob": { "like": {} } },
{ "height": { "between": [50, 89] } }, { "weight": { "between": [72, 192] } }, { "bodyBuild": "Slim" }] }
String value is not push in array.
if (interestId.highSchool && interestId.highSchool !="") {
var highschool = new RegExp('.*' + interestId.highSchool + '.*', "i");
query.and.push({
highSchool: { like: highschool }
})
}
where is issue i don't understand. if i did't pass string value it's working fine.
Try following way:-
if (interestId.highSchool && interestId.highSchool !="") {
query.and.push({
highSchool: {regexp: new RegExp('.*' + interestId.highSchool + '.*', "i")}
})
}
I'm trying order some data, and want to have the exact match as first results and then the rest.
I tried with this $eq in $project, but it seems that something won't work.
Is it possible to do something similar?
db.getCollection('words').aggregate([
{ $match: { "kanji.text": /^彼/ } },
{ $project: {
"kanji": 1,
"kana": 1,
"sense":1,
"exact": {
$eq : [ "$kanji.0.text", "彼" ]
}
}
},
{ $sort: {"exact": 1} }
])
Document Structure:
EDIT: I have found a bad workaround
db.getCollection('words').aggregate([
{ $match: { "kanji.text": /^彼/ } },
{ $limit : 10 },
{ $addFields: {
exact: {
$filter: {
input: '$kanji.text',
cond: {
$eq: ['$$this', "彼"]
}
}
}
}},
{ $project: {
"kanji": 1,
"kana": 1,
"sense":1,
"exact": 1
}
},
{ $sort: {"exact.0": -1} }
])
I have two unordered sets of pairs (X,Y) implemented using boost hash and I would like to convert them to a Json file that has a special format.
unordered_set<pair<int,int>> visited, cleaned
. I want them to be represented this way in Json format using nlohmann/json.hpp C++:
{
"visited": [
{
"X": 2,
"Y": 2
},
{
"X": 3,
"Y": 0
},
{
"X": 3,
"Y": 1
},
{
"X": 3,
"Y": 2
}
],
"cleaned": [
{
"X": 2,
"Y": 2
},
{
"X": 3,
"Y": 0
},
{
"X": 3,
"Y": 2
}
],
}
can anyone help me with the c++ code for this part?
my code is
for (auto it = visited.begin(); it != visited.end(); ++it)
{
j2["visited"]["X"]=it->second;
j2["visited"]["Y"] = it->first;
}
for (auto it = cleaned.begin(); it != cleaned.end(); ++it)
{
j2["cleaned"]["X"] = it->second;
j2["cleaned"]["Y"] = it->first;
}
and it produces:
{
"cleaned": {
"X": 3,
"Y": 2
},
"visited": {
"X": 3,
"Y": 2
}
}
The JSON format you want contains arrays. Use something like this to explicitly create
them:
nlohmann::json arr;
for (auto it = visited.begin(); it != visited.end(); ++it) {
nlohmann::json o;
o["X"] = it->second;
o["Y"] = it->first;
arr.push_back(o);
}
j2["visited"] = arr;
And similarly for the second part.
This question already has answers here:
How to initialize an array of struct in C++?
(3 answers)
Closed 5 years ago.
struct Sensors
{
int pin;
int angle;
bool state;
};
Sensors sensor[6];
How can I initialize every field of sensor like a normal array?
This method
mydata data[] = { { 1, 2, 3, 4, 5, 6 },
{ 0, 60, 120, 180, -120, -60 },
{ false, false, false, false, false, false} };
doesn't work, it returns me too many initialization.
You can use aggregate initialization.
struct Sensors
{
int pin;
int angle;
bool state;
};
Sensors sensor[6] = {
{ 0, 0, true },
{ 1, 0, true },
{ 2, 0, false },
{ 3, 0, false },
{ 4, 0, false },
{ 5, 0, false }
};
struct Sensors
{
int pin;
int angle;
bool state;
};
int main()
{
Sensors sensor[6] = {
{ 1, 2, false },
{ 1, 2, true },
{ 1, 3, false },
{ 2, 2, false },
{ -1, -2, true },
{ 1, 2, false }
};
return 0;
}
I've been tasked to remove some compiler warning. I've been able to boil the problem down to the following example, which I am scratching my head why it won't work. I guess I don't know how to initialize stuff in C++. Any help would be appreciated.
I use g++ like so:
g++ init_arr.cpp
Here's the code. I want to initialize all the people at all the tables in Aisle pizza:
// init_arr.cpp
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
struct Person {
int id;
string name;
double money;
};
struct Table {
Person tab[4];
};
struct Aisle {
Table ais[3];
};
int main() {
cout << "main function()" << endl;
Aisle pizza =
{
{ // Table 0
{ 0, "Tom", 100.0 },
{ 1, "Mary", 101.0 },
{ 2, "Jane", 103.0 },
{ 3, "Joe", 104.0 }
},
{ // Table 1
{ 0, "Tom", 100.0 },
{ 1, "Mary", 101.0 },
{ 2, "Jane", 103.0 },
{ 3, "Joe", 104.0 }
},
{ // Table 2
{ 0, "Tom", 100.0 },
{ 1, "Mary", 101.0 },
{ 2, "Jane", 103.0 },
{ 3, "Joe", 104.0 }
}
};
return 0;
}
I thought the above would work, but I get the following error:
g++ init_arr.cpp -std=gnu++0x
init_arr.cpp: In function ‘int main()’:
init_arr.cpp:49: error: too many initializers for ‘Table [3]’
init_arr.cpp:49: error: too many initializers for ‘Aisle’
While #us2012 showed what works and provides a good explanation (+1 for him), I find it not very readable. This is an alternative:
Aisle pizza =
{
Table { // Table 0
Person { 0, "Tom", 100.0 },
Person { 1, "Mary", 101.0 },
Person { 2, "Jane", 103.0 },
Person { 3, "Joe", 104.0 }
},
Table { // Table 1
Person { 0, "Tom", 100.0 },
Person { 1, "Mary", 101.0 },
Person { 2, "Jane", 103.0 },
Person { 3, "Joe", 104.0 }
},
Table { // Table 2
Person { 0, "Tom", 100.0 },
Person { 1, "Mary", 101.0 },
Person { 2, "Jane", 103.0 },
Person { 3, "Joe", 104.0 }
}
};
You're missing lots of pairs of parentheses. I have added comments to make it clearer which bit starts where.
To put it into one sentence, your problem is that an array with three elements can be initialized with {1,2,3} while a struct that contains an array as its single member is an extra layer and therefore has to be initalized with { {1,2,3} } - the outer layer is the struct, the inner layer is the array.
Aisle pizza =
{ // Aisle init
{ // Table ais[3] init
{ // ais[0] init
{ // Person tab[4] init
{ 0, "Tom", 100.0 },
{ 1, "Mary", 101.0 },
{ 2, "Jane", 103.0 },
{ 3, "Joe", 104.0 }
}
},
{ // ais[1] init
{ // Person tab[4] init
{ 0, "Tom", 100.0 },
{ 1, "Mary", 101.0 },
{ 2, "Jane", 103.0 },
{ 3, "Joe", 104.0 }
}
},
{ // ais[2] init
{ // Person tab[4] init
{ 0, "Tom", 100.0 },
{ 1, "Mary", 101.0 },
{ 2, "Jane", 103.0 },
{ 3, "Joe", 104.0 }
}
}
}
};
Each block needs to represent an object. Aisle struct contains an array object (ais). Each element of the ais array contains a Table struct. Each Table struct contains an array object (tab). and so on...
Try this:
Aisle pizza =
{ // Aisle
{ // .ais
{ // .ais[0]
{ // .ais[0].tab
{ 0, "Tom", 100.0 }, // tab[0]
{ 1, "Mary", 101.0 }, // tab[1]
{ 2, "Jane", 103.0 }, // tab[2]
{ 3, "Joe", 104.0 } // tab[3]
}
},
{ // .ais[1]
{ // .ais[1].tab
{ 0, "Tom", 100.0 }, // tab[0]
{ 1, "Mary", 101.0 }, // tab[1]
{ 2, "Jane", 103.0 }, // tab[2]
{ 3, "Joe", 104.0 } // tab[3]
}
},
{ // .ais[2]
{ // .ais[2].tab
{ 0, "Tom", 100.0 }, // tab[0]
{ 1, "Mary", 101.0 }, // tab[1]
{ 2, "Jane", 103.0 }, // tab[2]
{ 3, "Joe", 104.0 } // tab[3]
}
}
}
};