Querying a MongoDB database using a list of values

84 Views Asked by At

I have a database of cricket matches where each match has been assigned a unique MatchId. I have a list which contains the Matchids of a few select matches that I need to query from the MongoDB database named "id_list"

the query i use in pymongo on python is query = { 'MatchId': {'$in': id_list} } i tried using the same query on Rmongo

library(RMongo)
mongo <- mongoDbConnect("db_name", "127.0.0.1", 27017)
df_t20 <- dbGetQuery(mongo, 'match_info', '{"MatchType": "T20"}')
id_list<-as.vector(df_t20$MatchId)
t20 <- dbGetQuery(mongo, 'deliveries', '{"MatchId": { $in: id_list} }')
head(t20)

Error in .jcall(rmongo.object@javaMongo, "S", "dbGetQuery", collection, : com.mongodb.util.JSONParseException: {"MatchId": { $in: id_list} } ^

1

There are 1 best solutions below

0
On
library(RMongo)
mongo <- mongoDbConnect("db_name", "127.0.0.1", 27017)
df_t20 <- dbGetQuery(mongo, 'match_info', '{"MatchType": "T20"}')
id_list<-as.vector(df_t20$MatchId)
t20 <- dbGetQuery(mongo, 'deliveries', '{"MatchId": { "$in": id_list} }')
head(t20)

or try this way

convert {"MatchId": { "$in": id_list} } query in to json object using rJson, then it cast to string ,