R - filter data from MongoDB collection - r

I want to use R to load a collection from mongoDB to R, with filter to increase the speed. The filter can be Or condition or IN a R data.
MongoDB collection
Name Type
A M
B P
C M
D P
E O
RFilter
Criteria
M
P
RData <- MongoCollection$find('{"Type" in RFilter$Criteria}',
fields = '{
"Name" : true,
"Type" : true
}')
I expect the output:
RData
Name Type
A M
B P
C M
D P

If you need to check-in database to check if name or type is P or M try $or in criteria as below:
{$or:[{Name:{$in:["P","M"]}},{Type:{$in:["P","M"]}}]}
The above $or condition will check in DB if the name is "P" or "M" it'll return document else it'll check in type if it values is "P" or "M" else won't return document if both are not matched.

Related

How to extract a certain element from a dictionary

For example:
d = [{'symbol': 'ETH', 'available': '1'}, {'symbol': 'DOGE', 'available': '4'} , {'symbol': 'USD', 'available': '10'}]
I need to extract the available USD which is 10 in the above dictionary, could you please provide me with a proper code?
You can use next() built-in method:
out = next(dct["available"] for dct in d if dct["symbol"] == "USD")
print(out)
Prints:
10
Well, if your language is python you can do:
print([x for x in d if d['symbol'] == 'USD'][0]['available'])
Do keep in mind that USD must exist in the list for it to work.

Iterate over a list in a Match query

I have a relation that has a list of ids s_ids as a property of the relation. each id in the list correspond to another node that has a sentence corresponding to an id.I used:
MATCH (c: term)-[r: semrel]->(t: term), (b: Sentence)
Where r.source = "xyz" And b.sentence_id IN r.s_id
return r,b
to return all sentences corresponding to the relation,
the result looks like :
r b
w abc
w rty
w zxv
e nmx
e qrt
the relation r is repeated for every sentence how can I group the list of sentences corresponding to each relation to get
r b
w abc, rty, zxv
e nmx,qrt
Thanks
This should return each r and its collection of sentences:
MATCH (c: term)-[r: semrel]->(t: term), (b: Sentence)
WHERE r.source = "xyz" AND b.sentence_id IN r.s_i
RETURN r, COLLECT(b) AS sentences;
For better performance, if you create an index on :Sentence(sentence_id), like this:
CREATE INDEX ON :Sentence(sentence_id);
then this query (which adds a hint to use the index) should be faster (as the b nodes can be found using the index):
MATCH (c: term)-[r: semrel]->(t: term), (b: Sentence)
USING INDEX b:Sentence(sentence_id)
WHERE r.source = "xyz" AND b.sentence_id IN r.s_i
RETURN r, COLLECT(b) AS sentences;

Cypher conditions on all nodes in collection behaving incorrectly?

I have this database:
CREATE (A:A {name:"A"})-[:R]->(B:B {name:"B"})-[:R]->(C:B {name:"C"})-[:R]->(D:A {name:"D"})-[:R]->(E:A {name:"E"})
This query
MATCH p = (:A)-[*]->(:B) WITH NODES(p)[1..] AS p_nodes RETURN p_nodes
returns edge (B)-->(C). And B and C have both label B. Why then does this query
MATCH p = (:A)-[*]->(:B) WITH NODES(p)[1..] AS p_nodes
WHERE ALL(x IN p_nodes[0..] WHERE LABELS(x) = "B") RETURN p_nodes
return nothing (no rows)? The only thing it does is make sure that p_nodes contains B labeled nodes only. And as the first query showed it does.
The labels(x) function will return a collection of strings, not a string. This is because nodes can have multiple labels.
So instead of comparing labels(x) = "B" use the IN operator "B" in labels(x):
MATCH p =(:A)-[*]->(:B)
WITH NODES(p)[1..] AS p_nodes
WHERE ALL (x IN p_nodes[0..]
WHERE "B" IN LABELS(x))
RETURN p_nodes

Check if an argument is a dictionary or not in Tcl

I want have a proc which does something if its' argument is a Tcl 8.5 and above dictionary or not.
I could not find anything straightforward from Tcl dict command.
The code which I could get working is:
proc dict? {dicty} {
expr { [catch { dict info $dicty } ] ? 0 : 1 }
}
Is there anything w/o using catch, something built in?Thanks.
You can test if a value is a dictionary by seeing if it is a list and if it has an even number of elements; all even length lists may be used as dictionaries (though many are naturally not canonical dictionaries because of things like duplicate keys).
proc is-dict {value} {
return [expr {[string is list $value] && ([llength $value]&1) == 0}]
}
You can peek at the actual type in Tcl 8.6 with tcl::unsupported::representation but that's not advised because things like literals are converted to dictionaries on the fly. The following is legal, shows what you can do, and shows the limitations (
% set value {b c d e}
b c d e
% tcl::unsupported::representation $value
value is a pure string with a refcount of 4, object pointer at 0x1010072e0, string representation "b c d e"
% dict size $value
2
% tcl::unsupported::representation $value
value is a dict with a refcount of 4, object pointer at 0x1010072e0, internal representation 0x10180fd10:0x0, string representation "b c d e"
% dict set value f g;tcl::unsupported::representation $value
value is a dict with a refcount of 2, object pointer at 0x1008f00c0, internal representation 0x10101eb10:0x0, no string representation
% string length $value
11
% tcl::unsupported::representation $value
value is a string with a refcount of 2, object pointer at 0x1008f00c0, internal representation 0x100901890:0x0, string representation "b c d e f g"
% dict size $value;tcl::unsupported::representation $value
value is a dict with a refcount of 2, object pointer at 0x1008f00c0, internal representation 0x1008c7510:0x0, string representation "b c d e f g"
As you can see, types are a bit slippery in Tcl (by design) so you're strongly advised to not rely on them at all.
Your approach is flawed because Tcl has dynamic type system where the actual type of a value is able to morph dynamically and depends on the commands applied to it—observe:
$ tclsh
% info pa
8.5.11
% dict info {a b}
1 entries in table, 4 buckets
number of buckets with 0 entries: 3
number of buckets with 1 entries: 1
number of buckets with 2 entries: 0
number of buckets with 3 entries: 0
number of buckets with 4 entries: 0
number of buckets with 5 entries: 0
number of buckets with 6 entries: 0
number of buckets with 7 entries: 0
number of buckets with 8 entries: 0
number of buckets with 9 entries: 0
number of buckets with 10 or more entries: 0
average search distance for entry: 1.0
% llength {a b}
2
% string len {a b}
3
%
As you can see, the same value {a b} is a dictionary, a list and a string: in each case, the value acquires its "real" type in the very moment a Tcl command expecting a value of certain type converts the "default" type of the value, which is string, to the one the command operates on.
You should understand by now that trying to make a call dict? {a b} has little sence as the value {a b} is a perfect dict as well as a perfect list as well as a perfect string, and it could be, say, a perfect tuple if there are custom commands in the current interpreter working on tuples (lists of fixed length).
Hence the real approach you should take is to just blindly use dict command on those values passed to your commands you expect to contain dictionaries. If a user will manage to pass to your command something which is not interpretable as a dictionary, the dict command will fail, and that's a good thing to do as such an error is not really recoverable (it's a programming error).
Any attempt to count on a value's specific type is going again the grain of the very idea of the Tcl's implicit/dynamic typing. It's even true for the Tcl C API.
If you really meant to ask how to be sure the current Tcl version supports dict command, and not about the type of a particular value, test the Tcl's version somewhere at startup and save this as a flag, like this:
set hasDicts [expr {[package vcompare [info tclversion] 8.5] >= 0}]
But note that your code relying on the hasDicts value is now in some gray zone because if the user is not supplying you values you process with the dict command then what command you use to process them?
Please also note that the dict command can be added to a Tcl 8.4 interpreter in the form of the loadable module (see this).

Wordpress alphabetical index

I have a question.
How can do an alphabetical index with links without plugin ?
Example : A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
If a character contains posts, it will be a link.
Else it will text.
Anyone can help me ?
Update : Or something like this http://dribbble.com/tags
You could write a few lines of code to query the database and do a like search on the post titles. The below is not formatted for a WP query, but it should give you a good idea:
SELECT count(*)
FROM wp_posts
WHERE post_title LIKE 'a%';
If the count is greater than 0, you know there are posts and you can add a hyperlink to the letter.

Resources