简体   繁体   English

将字段与数组的任何元素匹配的Mongo查询

[英]Mongo query that matches field to any element of array

I am trying to query a Mongo Db through R (rmongodb package). 我正在尝试通过R(rmongodb程序包)查询Mongo Db。 i have a simple requirement: 我有一个简单的要求:

  • Return records where the field "email" matches any of the emails in the vector usr$email. 返回记录,其中字段“ email”与向量usr $ email中的任何电子邮件匹配。 I think I am close but just not able to find the right syntax to pull it through. 我想我很亲密,但无法找到正确的语法来实现它。

I saw this response to an earlier question ( Mongo: If any array position matches single query ) and am trying along the lines: 我看到了对一个较早问题的回答( Mongo:如果任何数组位置都与单个查询匹配 ),并且正在尝试:

eids_l <- paste0("'", unique(usr$email), "'", collapse=", ")
eids_l1 <- sprintf("[ %s ]", eids_l)
q <- sprintf('{"email": {"$in": %s}}', eids_l1)
cursor <- mongo.find.all(mongo, namespace, buf)

I still get an error: 我仍然收到错误消息:

Error in mongo.bson.from.JSON(arg) : 
  Not a valid JSON content: {"email": {"$in": [ 'xx@gmail.com',

cursor <- mongo.find.all(mongo, "namespace", query='{ "email": { "$in": ["xx@gmail.com", "yy@gmail.com", "zz@gmail.com" ] } }') 光标<-mongo.find.all(mongo,“ namespace”,query ='{“ email”:{“ $ in”:[“ xx@gmail.com”,“ yy@gmail.com”,“ zz @ gmail .com“]}}')

Be careful with the use of apostrophes(') and quotation marks("). 请注意使用撇号(')和引号(“)。

I always use the rmongodb Cheat sheet: 我总是使用rmongodb备忘单:

https://cran.r-project.org/web/packages/rmongodb/vignettes/rmongodb_cheat_sheet.pdf https://cran.r-project.org/web/packages/rmongodb/vignettes/rmongodb_cheat_sheet.pdf

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM