How to use $and in pymongo without syntax problem? - pymongo

I'm confused why this isn't working
for record in mycollection.find({$and: [ {"PARTNER_SOURCE":"InLife"}, {"HOLDINGMATCHES": {"$exists": "true"}} ] }):
give this error:
File "./outputleads.py", line 15
for record in mycollection.find({$and: [ {"PARTNER_SOURCE":"InLife"}, {"HOLDINGMATCHES": {"$exists": "true"}} ] }):
^
SyntaxError: invalid syntax
It's giving syntax error right on the $ on $and
What is wrong with it?
This works just fine as a filter right on the mongodb interface:
{$and: [ {"PARTNER_SOURCE":"InLife"}, {"HOLDINGMATCHES": {"$exists": "true"}} ] }

$and needs to be in quotes... everything needs to be in "" apparently.
for record in mycollection.find({"$and": [ {"PARTNER_SOURCE":"InLife"}, {"HOLDINGMATCHES": {"$exists": "true"}} ] }):

Related

What format should a REST API use for ULIDs? i.e. Base32 or RFC4122

ULID Base32 01GMEX2SA207FNV8E19QM8EJ4M is the same as RFC4122 01851dd1-6542-01df-5da1-c14de8874894.
Postgresql appears to store the ULIDs in RFC4122 format.
Both GET /assets/01GMEX2SA207FNV8E19QM8EJ4M/resource_acl and GET /assets/01851dd1-6542-01df-5da1-c14de8874894/resource_acl return the same successful response:
{
"id": "/assets/01GMEX2SA207FNV8E19QM8EJ4M/resource_acl",
"asset": "/assets/01GMEX2SA207FNV8E19QM8EJ4M",
"members": [
"/resource_acl_members/acl=01GMEX2SA207FNV8E19QM8EJ4M;user=01GMEX2SA1FEZHVQJYTW48P7FE"
]
}
GET /resource_acl_members/acl=01851dd1-6542-01df-5da1-c14de8874894;user=01851dd1-6541-7bbf-1dde-5ed7088b1dee returns the desired successful response.
{
"id": "/resource_acl_members/acl=01GMEX2SA207FNV8E19QM8EJ4M;user=01GMEX2SA1FEZHVQJYTW48P7FE",
"acl": "/assets/01GMEX2SA207FNV8E19QM8EJ4M/resource_acl",
"user": "/tenant_users/01GMEX2SA1FEZHVQJYTW48P7FE",
"roles": [
"ROLE_MANAGE_PROJECT"
]
}
GET /resource_acl_members/acl=01GMEX2SA207FNV8E19QM8EJ4M;user=01GMEX2SA1FEZHVQJYTW48P7FE, however, returns Postgresql error: Invalid text representation: 7 ERROR: invalid input syntax for type uuid: \"01GMEX2SA207FNV8E19QM8EJ4M\"\nCONTEXT: unnamed portal parameter $1 = '...'",.
Should I be consistent and use either solely Base32 or RFC4122? If so, which format?

Mongodb query problem, how to get the matching items of the $or operator

Thank you for first.
MongoDB Version:4.2.11
I have a piece of data like this:
{
"name":...,
...
"administration" : [
{"name":...,"job":...},
{"name":...,"job":...}
],
"shareholder" : [
{"name":...,"proportion":...},
{"name":...,"proportion":...},
]
}
I want to match some specified data through regular expressions:
For a example:
db.collection.aggregate([
{"$match" :
{
"$or" :
[
{"name" : {"$regex": "Keyword"}}
{"administration.name": {"$regex": "Keyword"}},
{"shareholder.name": {"$regex": "Keyword"}},
]
}
},
])
I want to set a flag when the $or operator successfully matches any condition, which is represented by a custom field, for example:{"name" : {"$regex": "Keyword"}}Execute on success:
{"$project" :
{
"_id":false,
"name" : true,
"__regex_type__" : "name"
}
},
{"administration.name" : {"$regex": "Keyword"}}Execute on success:"__regex_type__" : "administration.name"
I try do this:
{"$project" :
{
"_id":false,
"name" : true,
"__regex_type__" :
{
"$switch":
{
"branches":
[
{"case": {"$regexMatch":{"input":"$name","regex": "Keyword"}},"then" : "name"},
{"case": {"$regexMatch":{"input":"$administration.name","regex": "Keyword"}},"then" : "administration.name"},
{"case": {"$regexMatch":{"input":"$shareholder.name","regex": "Keyword"}},"then" : "shareholder.name"},
],
"default" : "Other matches"
}
}
}
},
But $regexMatch cannot match the array,I tried to use $unwind again, but returned the number of many array members, which did not meet my starting point.
I want to implement the same function as mysql this SQL statement in mongodb, like this:
SELECT name,administration.name,shareholder.name,(
CASE
WHEN name REGEXP("Keyword") THEN "name"
WHEN administration.name REGEXP("Keyword") THEN "administration.name"
WHEN shareholder.name REGEXP("Keyword") THEN "shareholder.name"
END
)AS __regex_type__ FROM db.mytable WHERE
name REGEXP("Keyword") OR
shareholder.name REGEXP("Keyword") OR
administration.name REGEXP("Keyword");
Maybe this method is stupid, but I don’t have a better solution.
If you have a better solution, I would appreciate it!!!
Thank you!!!
Since $regexMatch does not handle arrays, use $filter to filter individual array elements with $regexMatch, then use $size to see how many elements matched.
[{"$match"=>{"$or"=>[{"a"=>"test"}, {"arr.a"=>"test"}]}},
{"$project"=>
{"a"=>1,
"arr"=>1,
"src"=>
{"$switch"=>
{"branches"=>
[{"case"=>{"$regexMatch"=>{"input"=>"$a", "regex"=>"test"}},
"then"=>"a"},
{"case"=>
{"$gte"=>
[{"$size"=>
{"$filter"=>
{"input"=>"$arr.a",
"cond"=>
{"$regexMatch"=>{"input"=>"$$this", "regex"=>"test"}}}}},
1]},
"then"=>"arr.a"}],
"default"=>"def"}}}}]
[{"_id"=>BSON::ObjectId('5ffb2df748966813f82f15ad'), "a"=>"test", "src"=>"a"},
{"_id"=>BSON::ObjectId('5ffb2df748966813f82f15ae'),
"arr"=>[{"a"=>"test"}],
"src"=>"arr.a"}]

ios picker multi dimensional array

pickerData = [[["A"], ["i","ii","iii"]],[["B"],["iv","v","vi","vii"]]]
}
Error: Contextual type 'String' cannot be used with array literal
Please advice how to use the array
Heterogenous literal collection types must be annoated:
Try this:
let pickerData: [[[String]]] =
[
[
["A"],
["i","ii","iii"]
],
[
["B"],
["iv","v","vi","vii"]
]
]

Error when running query in backand: not a valid constant

Hi when working in Backand I try to run the following query:
{
"object": "dr_persons",
"q": {
"person_type" : "4"
},
"fields": ["first_name", "last_name"]
}
person_type is a table in mysql db with "4" as a value.
When I run it I get this error:
Errors in Query
Please fix the following errors in the query:
not a valid constant for field person_type of object dr_persons
The only thing I can see is that when I sync my db it makes it a "float" which I can't change. Can anyone give me some direction on this?
The error message is due to the constant "4" being a string. According to the field type, float, it should be a number. Hence your query should be:
{
"object": "dr_persons",
"q": {
"person_type" : 4
},
"fields": ["first_name", "last_name"]
}

Rebol switch and type?

Why do I have to cast typeof to string with switch to make it work ?
This doesn't work:
typeof: type? get 'optional
switch typeof [
word! [
print "word"
]
string! [
print "string"
]
]
This works:
typeof: type? get 'optional
switch to-string typeof [
"word" [
print "word"
]
"string" [
print "string"
]
]
switch type?/word :optional [
word! [ print "word" ]
string! [ print "string" ]
]
OR
switch type? :optional reduce [
word! [ print "word" ]
string! [ print "string" ]
]
The reason is that the REBOL doesn't reduce ("evaluate") the cases in the switch statement. Without the /word refinement, the type? function returns a datatype!, but the switch statement tries to match this against a word!, and it fails.
I realize this might be confusing, so your best bet is either to convert the type to a string (as you did), or use one of the two idioms I've suggested. I prefer the first one, using type?/word.