loopback 3 string not matched with and condition - regex

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")}
})
}

Related

Django pymongo search, sort, limit on inner array and count them

I am learning Django with MongoDb and have a collection to search into. Here is a sample document in the collection:
{
"_id": { "$oid": "62615907568ddfca4fef3a25" },
"word": 'entropy,
"count": 4,
"occurrence": [
{"book": "62615907568ddfca4fef3a23", "year": 1942, "sentence": 0 },
{"book": "62615907568ddfca4fef3a23", "year": 1942, "sentence": 5 },
{"book": "62615907568ddfca4fef3a75", "year": 1928, "sentence": 0 },
{"book": "62615907568ddfca4fef3a90", "year": 1959, "sentence": 8 }
]
}
I want to retrieve the array elements of 'occurrence' field of a specific document (word):
Sorted by year
Within an year range
Limited with offset
count the total results (without limit/offset)
What I have done so far is:
offset= 0
limit= 10
search= {'$and': [{"_id": word_id_obj, "occurrence": {"$elemMatch": {"year": {"$gte": 1940, "$lte": 1960}}}}]}
word_docs= wordcollec.aggregate([
{"$match": search},
{"$project":
{"occurrence":
{"$slice": ['$occurrence', offset, limit]}
}
},
{"$sort": {'occurrence.year': -1}}
])
# Count total results
recnt= wordcollec.aggregate([{"$match": search}, {'$group' : {"_id": "$_id", "sno" : {"$sum" : {"$size": "$occurrence"}}}}])
The year range, count are not working and I am unable to sort them. How can I rewrite my query for this?
Thanks in advance.
Use $unwind then $sort
db.collection.aggregate([
{
$match: {
_id: { "$oid": "62615907568ddfca4fef3a25" },
occurrence: { $elemMatch: { year: { $gte: 1940, $lte: 1960 } } }
}
},
{
$set: { totalWithoutLimitOrOffset: { $size: "$occurrence" } }
},
{
$unwind: "$occurrence"
},
{
$match: { "occurrence.year": { $gte: 1940, $lte: 1960 } }
},
{
$sort: { "occurrence.year": -1 }
},
{
$skip: 1
},
{
$limit: 2
},
{
$group: {
_id: "$_id",
word: { $first: "$word" },
count: { $first: "$count" },
totalWithoutLimitOrOffset: { $first: "$totalWithoutLimitOrOffset" },
occurrence: { $push: "$occurrence" }
}
}
])
mongoplayground

C++ std::to_array is undefined

I am using C++ Language Standard C++ 17.
I have been trying to make std::to_array to work but it's telling me it's not a member of namespace std.
My code:
#include <algorithm>
#include <array>
#include <string_view>
#include <Windows.h>
#include "InputUtil.h"
#define WIN32_LINUX(win32, linux) win32
struct Key {
constexpr Key(std::string_view name, int code) : name{ name }, code{ code } { }
std::string_view name;
int code;
};
static constexpr auto keyMap = std::to_array<Key>({
{ "'", WIN32_LINUX(VK_OEM_7, SDL_SCANCODE_APOSTROPHE) },
{ ",", WIN32_LINUX(VK_OEM_COMMA, SDL_SCANCODE_COMMA) },
{ "-", WIN32_LINUX(VK_OEM_MINUS, SDL_SCANCODE_MINUS) },
{ ".", WIN32_LINUX(VK_OEM_PERIOD, SDL_SCANCODE_PERIOD) },
{ "/", WIN32_LINUX(VK_OEM_2, SDL_SCANCODE_SLASH) },
{ "0", WIN32_LINUX('0', SDL_SCANCODE_0) },
{ "1", WIN32_LINUX('1', SDL_SCANCODE_1) },
{ "2", WIN32_LINUX('2', SDL_SCANCODE_2) },
{ "3", WIN32_LINUX('3', SDL_SCANCODE_3) },
{ "4", WIN32_LINUX('4', SDL_SCANCODE_4) },
{ "5", WIN32_LINUX('5', SDL_SCANCODE_5) },
{ "6", WIN32_LINUX('6', SDL_SCANCODE_6) },
{ "7", WIN32_LINUX('7', SDL_SCANCODE_7) },
{ "8", WIN32_LINUX('8', SDL_SCANCODE_8) },
{ "9", WIN32_LINUX('9', SDL_SCANCODE_9) },
{ ";", WIN32_LINUX(VK_OEM_1, SDL_SCANCODE_SEMICOLON) },
{ "=", WIN32_LINUX(VK_OEM_PLUS, SDL_SCANCODE_EQUALS) },
{ "A", WIN32_LINUX('A', SDL_SCANCODE_A) },
{ "ADD", WIN32_LINUX(VK_ADD, SDL_SCANCODE_KP_PLUS) },
{ "B", WIN32_LINUX('B', SDL_SCANCODE_B) },
{ "BACKSPACE", WIN32_LINUX(VK_BACK, SDL_SCANCODE_BACKSPACE) },
{ "C", WIN32_LINUX('C', SDL_SCANCODE_C) },
{ "CAPSLOCK", WIN32_LINUX(VK_CAPITAL, SDL_SCANCODE_CAPSLOCK) },
{ "D", WIN32_LINUX('D', SDL_SCANCODE_D) },
{ "DECIMAL", WIN32_LINUX(VK_DECIMAL, SDL_SCANCODE_KP_DECIMAL) },
{ "DELETE", WIN32_LINUX(VK_DELETE, SDL_SCANCODE_DELETE) },
{ "DIVIDE", WIN32_LINUX(VK_DIVIDE, SDL_SCANCODE_KP_DIVIDE) },
{ "DOWN", WIN32_LINUX(VK_DOWN, SDL_SCANCODE_DOWN) },
{ "E", WIN32_LINUX('E', SDL_SCANCODE_E) },
{ "END", WIN32_LINUX(VK_END, SDL_SCANCODE_END) },
{ "ENTER", WIN32_LINUX(VK_RETURN, SDL_SCANCODE_RETURN) },
{ "F", WIN32_LINUX('F', SDL_SCANCODE_F) },
{ "F1", WIN32_LINUX(VK_F1, SDL_SCANCODE_F1) },
{ "F10", WIN32_LINUX(VK_F10, SDL_SCANCODE_F10) },
{ "F11", WIN32_LINUX(VK_F11, SDL_SCANCODE_F11) },
{ "F12", WIN32_LINUX(VK_F12, SDL_SCANCODE_F12) },
{ "F2", WIN32_LINUX(VK_F2, SDL_SCANCODE_F2) },
{ "F3", WIN32_LINUX(VK_F3, SDL_SCANCODE_F3) },
{ "F4", WIN32_LINUX(VK_F4, SDL_SCANCODE_F4) },
{ "F5", WIN32_LINUX(VK_F5, SDL_SCANCODE_F5) },
{ "F6", WIN32_LINUX(VK_F6, SDL_SCANCODE_F6) },
{ "F7", WIN32_LINUX(VK_F7, SDL_SCANCODE_F7) },
{ "F8", WIN32_LINUX(VK_F8, SDL_SCANCODE_F8) },
{ "F9", WIN32_LINUX(VK_F9, SDL_SCANCODE_F9) },
{ "G", WIN32_LINUX('G', SDL_SCANCODE_G) },
{ "H", WIN32_LINUX('H', SDL_SCANCODE_H) },
{ "HOME", WIN32_LINUX(VK_HOME, SDL_SCANCODE_HOME) },
{ "I", WIN32_LINUX('I', SDL_SCANCODE_I) },
{ "INSERT", WIN32_LINUX(VK_INSERT, SDL_SCANCODE_INSERT) },
{ "J", WIN32_LINUX('J', SDL_SCANCODE_J) },
{ "K", WIN32_LINUX('K', SDL_SCANCODE_K) },
{ "L", WIN32_LINUX('L', SDL_SCANCODE_L) },
{ "LALT", WIN32_LINUX(VK_LMENU, SDL_SCANCODE_LALT) },
{ "LCTRL", WIN32_LINUX(VK_LCONTROL, SDL_SCANCODE_LCTRL) },
{ "LEFT", WIN32_LINUX(VK_LEFT, SDL_SCANCODE_LEFT) },
{ "LSHIFT", WIN32_LINUX(VK_LSHIFT, SDL_SCANCODE_LSHIFT) },
{ "M", WIN32_LINUX('M', SDL_SCANCODE_M) },
{ "MOUSE1", 0 },
{ "MOUSE2", 1 },
{ "MOUSE3", 2 },
{ "MOUSE4", 3 },
{ "MOUSE5", 4 },
{ "MULTIPLY", WIN32_LINUX(VK_MULTIPLY, SDL_SCANCODE_KP_MULTIPLY) },
{ "MWHEEL_DOWN", 0 },
{ "MWHEEL_UP", 0 },
{ "N", WIN32_LINUX('N', SDL_SCANCODE_N) },
{ "NONE", 0 },
{ "NUMPAD_0", WIN32_LINUX(VK_NUMPAD0, SDL_SCANCODE_KP_0) },
{ "NUMPAD_1", WIN32_LINUX(VK_NUMPAD1, SDL_SCANCODE_KP_1) },
{ "NUMPAD_2", WIN32_LINUX(VK_NUMPAD2, SDL_SCANCODE_KP_2) },
{ "NUMPAD_3", WIN32_LINUX(VK_NUMPAD3, SDL_SCANCODE_KP_3) },
{ "NUMPAD_4", WIN32_LINUX(VK_NUMPAD4, SDL_SCANCODE_KP_4) },
{ "NUMPAD_5", WIN32_LINUX(VK_NUMPAD5, SDL_SCANCODE_KP_5) },
{ "NUMPAD_6", WIN32_LINUX(VK_NUMPAD6, SDL_SCANCODE_KP_6) },
{ "NUMPAD_7", WIN32_LINUX(VK_NUMPAD7, SDL_SCANCODE_KP_7) },
{ "NUMPAD_8", WIN32_LINUX(VK_NUMPAD8, SDL_SCANCODE_KP_8) },
{ "NUMPAD_9", WIN32_LINUX(VK_NUMPAD9, SDL_SCANCODE_KP_9) },
{ "O", WIN32_LINUX('O', SDL_SCANCODE_O) },
{ "P", WIN32_LINUX('P', SDL_SCANCODE_P) },
{ "PAGE_DOWN", WIN32_LINUX(VK_NEXT, SDL_SCANCODE_PAGEDOWN) },
{ "PAGE_UP", WIN32_LINUX(VK_PRIOR, SDL_SCANCODE_PAGEUP) },
{ "Q", WIN32_LINUX('Q', SDL_SCANCODE_Q) },
{ "R", WIN32_LINUX('R', SDL_SCANCODE_R) },
{ "RALT", WIN32_LINUX(VK_RMENU, SDL_SCANCODE_RALT) },
{ "RCTRL", WIN32_LINUX(VK_RCONTROL, SDL_SCANCODE_RCTRL) },
{ "RIGHT", WIN32_LINUX(VK_RIGHT, SDL_SCANCODE_RIGHT) },
{ "RSHIFT", WIN32_LINUX(VK_RSHIFT, SDL_SCANCODE_RSHIFT) },
{ "S", WIN32_LINUX('S', SDL_SCANCODE_S) },
{ "SPACE", WIN32_LINUX(VK_SPACE, SDL_SCANCODE_SPACE) },
{ "SUBTRACT", WIN32_LINUX(VK_SUBTRACT, SDL_SCANCODE_KP_MINUS) },
{ "T", WIN32_LINUX('T', SDL_SCANCODE_T) },
{ "TAB", WIN32_LINUX(VK_TAB, SDL_SCANCODE_TAB) },
{ "U", WIN32_LINUX('U', SDL_SCANCODE_U) },
{ "UP", WIN32_LINUX(VK_UP, SDL_SCANCODE_UP) },
{ "V", WIN32_LINUX('V', SDL_SCANCODE_V) },
{ "W", WIN32_LINUX('W', SDL_SCANCODE_W) },
{ "X", WIN32_LINUX('X', SDL_SCANCODE_X) },
{ "Y", WIN32_LINUX('Y', SDL_SCANCODE_Y) },
{ "Z", WIN32_LINUX('Z', SDL_SCANCODE_X) },
{ "[", WIN32_LINUX(VK_OEM_4, SDL_SCANCODE_LEFTBRACKET) },
{ "\\", WIN32_LINUX(VK_OEM_5, SDL_SCANCODE_BACKSLASH) },
{ "]", WIN32_LINUX(VK_OEM_6, SDL_SCANCODE_RIGHTBRACKET) },
{ "`", WIN32_LINUX(VK_OEM_3, SDL_SCANCODE_GRAVE) }
});
As you can see i have included , And that's what i mostly read that you needed to do.
If anyone knows how to make std::to_array actually work, please reply.
Kind Regards.
std::to_array is available in C++20, not earlier versions.

Mongo query to find not null string inside list of objects

I want to query an array of objects with a particular key should have text in it.
this is the query I have tried
to find disclaimer.text exists and not empty. But It always prints 0
db.slideshows.count({"config.slides": { $elemMatch: {disclaimer: {"text" : {"$exists" : true, "$ne" : ""} }} } })
This is my data
{
"id": 1002,
"config": {
"firstSlide": "vehicle",
"slides": [
{
"slideKey": "sk1",
"disclaimer": {
"text": ""
}
},
{
"slideKey": "sk2"
}
]
}
}
{
"id": 1003,
"config": {
"firstSlide": "book",
"slides": [
{
"slideKey": "sk3",
"disclaimer": {
"text": "Hello"
}
},
{
"slideKey": "sk4"
}
]
}
}
{
"id": 1004,
"config": {
"firstSlide": "book",
"slides": [
{
"slideKey": "sk3",
"disclaimer": {
"text": "nope"
}
},
{
"slideKey": "sk4",
"disclaimer": {
"text": ""
}
}
]
}
}
I want all the rows which have not empty disclaimer.text. ex. in the above set I need to get id 1004 and 1003 as a result.
Try
db.slideshows.count({
"config.slides": {
$elemMatch: {
"disclaimer.text": {
"$exists": true,
"$ne": ""
}
}
}
})

MongoDB: Create an Exact Match field

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} }
])

c++ json-nlohmann accessing the list elements

I'm trying to use json-nlohmann library to read JSON files in C++
So far I got along with it preatty well, but now I'm trying to access elements of the list in given json. JSON:
{
"GameMap": "path_to_game_map",
"Objects": [
{ "object_1": { "Transform": { "Position": { "X": 1, "Y": 2 }, "Rotation": { "X": 3.5, "Y": 8.2 }, "Scale": { "X": 1, "Y":1 } },
"Components": [
{ "0": { "data": { "some_data": "false" } } },
{ "1": { "data": { "some_data": "false" } } },
{ "2": { "data": { "some_data": "false" } } }
] } },
{ "object_2": { "Transform": { "Position": { "X": 1, "Y": 2 }, "Rotation": { "X": 3.5, "Y": 8.2}, "Scale": { "X": 1, "Y":1 } },
"Components": [
{ "0": { "data": { "some_data": "false" } } },
{ "1": { "data": { "some_data": "false" } } },
{ "2": { "data": { "some_data": "false" } } }
] } }
]
}
Where I'm trying to access each Component and read it's key value.
I've got an object out for every Component. But I just cannot figure out how to read its key and value.
Help! Please.
When you iterate a JSON object, say j, you can access the key and value like this:
for (json::iterator it = j.begin(); it != j.end(); ++it) {
std::cout << it.key() << " : " << it.value() << "\n";
}
you can also now use for (auto& element : j.items()) and then element.key() and element.value() inside the loop.