本文介绍了R-如何在m​​ongolite查询中传递值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用mongolite包连接和从MongoDB检索数据.如何在mongolite查找查询中传递值

I'm using mongolite package to connect and retrieve data from MongoDB.How to pass value in mongolite find query

##connecting mongodb

library(mongolite)

mongo<-mongolite::mongo(collection = "Sample", db = "Test", url = 
                          "mongodb://User:123@Wyyuyu:13333/ty2_U",verbose = TRUE)

## getting all data from collection data from collection below query is working fine.

values <- mongo$find()

## But I want to filter specific value by passing value.

 for(i in c("process","check","queue"))
{    

   values <- mongo$find('{"field" : i}',)
}

如果我尝试上面的代码,我将遇到以下问题.请帮助我解决

if I tried above code i'm getting getting below issues . please help me to resolve

Error: Invalid JSON object: {"field" : i}

推荐答案

鉴于您的i是变量,您需要使用paste0这样的字符串创建字符串:

Given your i is a variable, you need to create the string using something like paste0:

values <- mongo$find(paste0('{"field" : ', i, '}') )

除了循环之外,您还可以使用

but rather than a loop you could also use

values <- mongo$find('{"field" : { "$in" : [ "process", "check", "queue" ] } }')

这篇关于R-如何在m​​ongolite查询中传递值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-30 00:42