I am trying to write R script in which I have to do some operations on mongo database. Therefore, I have a few questions:
How to use double condition? I know how to use single condtion
mongoDB$find(query = '{"id" : { "$in" : ["1","2"]}}')
mongoDB$find(query = '{"date" : { "$in" : "2019-08-09"}}')
How should I connect both conditions in one query? How to write such code?
How to use parameter in mongo code? In my script I will have vector with dynamic number of IDs. How I should write query? I am looking for something like:
VectorWithIDs <- c(1:1000)
mongoDB$find(query = '{"id" : { "$in" : VectorWithIDs}}')
Any Ideas how to solve both problems? Thanks in advance!
Just do the query with a "," as a separation token.
For example:
mongoDB$find(query = '{"id" : { "$in" : ["1","2"]}, '': "date" : { "$in" : "2019-08-09"}}'}')
Related
I want to perform a partial match query on a MongoDB in R. I've tried to specify a query that matches the MongoDB query format like so:
library(mongolite)
foo <- mongo(url = "myConnectionString")
bar <- foo$find(
query = '{"_id": /idContainsThis/}',
fields = '{}'
)
But when I try this, I get the following error:
Error: Invalid JSON object: {"_id": /idContainsThis/}
I can't use this solution because if I put quotes round the term, the / is taken as a string literal, not the wildcard I need.
Does anyone know how to make this work with mongolite?
You'll have to use the regex function like this
query = '{"_id": { "$regex" : "idContainsThis", "$options" : "i" }}'
The "$options" : "i" is in case you want it to be case insensitive.
However I am not sure if this will work on an _id
I am trying to setup a simple connect between a NodeJS application and an R-script.
So far I've managed to set up the basic connection and the running of the cript, by using r-script (found on npm).
However, I am not able to pass a simple json to the R so that it can be converted to a dataframe using jsonlite.
NodeJS Code:
var R = require("r-script");
var out = R("script.R")
.data({"Name" : "Mario", "Age" : 32, "Occupation" : "Plumber"}, {"Name" : "Peach", "Age" : 21, "Occupation" : "Princess"}, {}, {"Name" : "Bowser", "Occupation" : "Koopa"})
.callSync();
console.log(out);
script.R:
library("jsonlite")
mydf <- fromJSON(input[[1]])
This gives the output:
'Argument 'txt' must be a JSON string, URL or file.'
I have tried removing the indexation of the vector (and give the full list to the fromJSON) but it also doesn't work.
Has anyone sucessfully passed JSON to an R script with this npm module?
Thanks in advance!
EDIT:
Also, if place the JSON between "'", it gives me "trailing errors" in spaces, and if they are removed, on the { char.
I have no idea about how R works, however the error could point to the fact that what you are trying to pass is a javascript object, not a JSON. Javascript objects look like but are not identical to JSONs (see discussion here Javascript object Vs JSON)
One thing that you can try is passing in the data function a JSON string by calling the JSON.stringify function on your objects. Something like this:
var R = require("r-script");
var out = R("script.R")
.data(JSON.stringify({"Name" : "Mario", "Age" : 32, "Occupation" : "Plumber"}), JSON.stringify( {"Name" : "Peach", "Age" : 21, "Occupation" : "Princess"}),JSON.stringify ({}), JSON.stringify({"Name" : "Bowser", "Occupation" : "Koopa"}))
.callSync();
console.log(out);
It's a longshot but it shows a general direction on which you can debug
I managed to found a simple solution, the Manos Agelidis answer did put me in the right direction.
Basically, I have able to parse the input string using:
string = paste(input, collapse=",")
string = paste("[",string,"]")
frame = fromJSON(string)
and in NodeJS:
var out = R("script.R")
.data('{"Name":"Mario","Age":32,"Occupation":"Plumber"}',
'{"Name":"Peach","Age":21,"Occupation":"Princess"}',
'{}',
'{"Name":"Bowser","Occupation":"Koopa"}')
.callSync();
fromJSON requires a vector but in string form, and not a literal vector. Therefore, what I needed to do what to create a string using the elements of the input and to add [ and ] to the begging and end of the string, respectively. This did convert to a data frame properly.
I'm trying to find the correct syntax for doing an If/Case type of statement in an Azure ComsmosDB SQL query. Here is the document that I have
{
"CurrentStage": "Stage2",
"Stage1": {
"Title": "Stage 1"
},
"Stage2": {
"Title": "Stage 2"
},
"Stage3": {
"Title": "Stage 3"
}
}
What I want to do is create a query that looks something like
Select c.CurrentStage,
if (CurrentStage == 'Stage1') { c.Stage1.Title }
else if (CurrentStage == 'Stage2') { c.Stage2.Title }
else if (CurrentStage == 'Stage3') { c.Stage3.Title } as Title
From c
Obviously the document and query that I have is a lot more complicated then this, but this gives you the general idea of what I'm trying to do. I have 1 of the fields in the select to be variable based on some other fields in the document.
While udf suggested by Jay Gong may be more comfortable to use if you need to reuse this function a lot, you can do this without udf using ternary operator syntax.
For example:
select
c.CurrentStage = 'stage1' ? c.Stage1.Title
: c.CurrentStage = 'stage2' ? c.Stage2.Title
: c.CurrentStage = 'stage3' ? c.Stage3.Title
: 'your default value should you wish one'
as title
from c
Advice: Provider SQL solution has the benefit over UDF that it is self-contained and does not require setting up the logic on the server before executing. Also, note that logic versioning is simpler if logic is stored in client apps entirely, not shared across client and server as in the UDF case. UDF does have it's uses (ex:heavy reuse across queries), but usually it's better to do without.
I suggest you using User Defined Function in Cosmos DB.
udf code:
function stage(c){
switch(c.CurrentStage){
case "Stage1" : return c.Stage1.Title;
case "Stage2" : return c.Stage2.Title;
case "Stage3" : return c.Stage3.Title;
default: return "";
}
}
SQL :
Select c.CurrentStage,
udf.stage(c) as Title
From c
Output result:
Hope it helps you.
I have a an array of ids which are generated using the firebase.ref().push() method.
For example -
myarray =
[-KoDeiUiqsMKDuXuwhzi,
-KoDrz6ngJtNrr51tguD,
-KoDyC-_ZeSIAN1_oFk4]
I have a node of items some of which match these ids -
/mynode
--KoDeiUiqsMKDuXuwhzi
-itemprop1 : "abc"
-itemprop2 : "xyz"
-id : "KoDeiUiqsMKDuXuwhzi"
--KnurYH554YNW6bcWch5
-itemprop1 : "opq"
-itemprop2 : "zyc"
-id : "KnurYH554YNW6bcWch5"
--KoDrz6ngJtNrr51tguD
-itemprop1 : "mon"
-itemprop2 : "ooo"
-id : "KoDrz6ngJtNrr51tguD"
..
..
..
..
..and so on.. basically the array of id's is a subset of this node.
I want to retreive a all objects from this node where the id matches in the array.
Currently the way i am doing is like this -
firebase.database().ref("mynode").once('value').then(function(snap){
snap.forEach(function(childsnap){
if(myarray.indexOf(childsnap.key) != -1){
//add to result
}
})
})
Although this code works but i think i can use orderByChild, startAt and endAt filtering to get the list of objects.
Is there a way to get the result by filtering instead of getting all data and then filtering out? Will it be more efficient than this code?
I try to create a loop that will create data frames by using different sql queries that have the same name exept the day number.For example here is the name for query that is for day 1 :SF_2013_Day1_BaseLine and this is for day 6: SF_2013_Day6_BaseLine. I wrote this code (below) but I got an error : Error: unexpected ',' in "for(i in 3," .So any Idea how can i get this code to work ?
Thank you
for (i in 1,3,6,10,14,21,30) {
SF_FinalviewQ3_2013_Day[i]_BaseLine<-sqlQuery(DB,"select * from [SF_2013_Day[i]_BaseLine]");
dim(SF_Day[i]_BaseLine)}
After a change based on #Pgibas edvice to this code :
for (i in c(1,3,6,10,14,21,30)) {
SF_FinalviewQ3_2013_Day[i]_BaseLine<-sqlQuery(DB,"select * from [SF_2013_Day[i]_BaseLine]");
dim(SF_Day[i]_BaseLine)}
I got this error: Error: unexpected input in "for(i in c(3,6)){glm_d[i]_" . What can I do to resolve the problem?
You need to resolve i within the names first:
for (i in c(1,3,6,10,14,21,30)) {
set <- sqlQuery(DB, paste0("select * from [SF_2013_Day[", i, "]_BaseLine]"))
eval(parse(text = paste0("SF_FinalviewQ3_2013_Day", i, "_BaseLine <- set"))
dim(set)
}