rmongodb:在查询中使用$ or [英] rmongodb: using $or in query
问题描述
我正在努力在R和rmongodb中使用$ or创建查询.我要模拟的是来自cmdline mongo的:
I'm struggling to create a query using $or within R and rmongodb. What I'd like to emulate is this from cmdline mongo:
db.people.find( { $or : [ {"person.cell": { $exists : true } }, {"person.home": { $exists : true } } ] })
我想提取其中person.cell不为null或person.home不为null的记录.我可以分别查询每个对象,但是当我使用$ or在rmongodb中创建缓冲区时,无法获取数据,使用rmongodb的R代码如下所示:
I'd like to pull records where either person.cell is not null, or person.home is not null. I can query each individually, but cannot get data back when I create the buffer in rmongodb with the $or, the R code using rmongodb looks like this:
l <- list("$exists"="true")
buf <- mongo.bson.buffer.create()
mongo.bson.buffer.start.array(buf, "$or")
mongo.bson.buffer.append.list(buf, "person.cell", l)
mongo.bson.buffer.append.list(buf, "person.home", l)
mongo.bson.buffer.finish.object(buf)
b <- mongo.bson.from.buffer(buf)
mongo.find(mongo, "work.people", b)
不返回任何记录,没有错误,只是一个空集.如前所述,我可以在person.cell或person.home上进行查找并获取结果,但是当我尝试执行$ or(在rmongodb中)以使我可以使用person.cell或person.home提取记录时,则无法获取结果.
That returns no records, no error, just an empty set. As I mentioned, I can do a find on either person.cell or person.home and get results, but not when I try to do an $or (in rmongodb) so that I pull records with either person.cell or person.home.
我也尝试过:
buf <- mongo.bson.buffer.create()
mongo.bson.buffer.start.array(buf, "$or")
mongo.bson.buffer.start.object(buf, "person.cell")
mongo.bson.buffer.append(buf, "$exists", "true")
mongo.bson.buffer.finish.object(buf)
mongo.bson.buffer.start.object(buf, "person.home")
mongo.bson.buffer.append(buf, "$exists", "true")
mongo.bson.buffer.finish.object(buf)
mongo.bson.buffer.finish.object(buf)
b <- mongo.bson.from.buffer(buf)
mongo.find(mongo, "work.people", b)
但是我得到了相同的空集结果(当我查看它时,"b"看起来是一样的).我被困在这一个.
But I get the same empty set result (and "b" looks the same when I view it). I'm stuck on this one.
推荐答案
为避免不得不编写mongo.bson.buffer-statements序列,我编写了一个程序包(
To avoid having to compose the sequence of mongo.bson.buffer-statements I wrote a package (rmongodbHelper) that will translate a JSON or a list() to a BSON object which can then be used with rmongodb.
首先让我们设置环境:
library(rmongodb)
# install rmongodbHelper package from GitHub
library(devtools)
devtools::install_github("joyofdata/rmongodbHelper")
library(rmongodbHelper)
# the MongoDB instance
ns <- "dbx.collx"
M <- mongo.create()
mongo.is.connected(M)
mongo.remove(M, ns, json_to_bson("{}"))
# inserting a number of dummy objects
# JSON keys currently are expected to be wrapped in double quotes!
objs <- c(
'{"_id":-1}',
'{"_id":-2, "person":{}}',
'{"_id":-3, "person":{"x":0}}',
'{"_id":1, "person":{"cell":0}}',
'{"_id":2, "person":{"home":0}}',
'{"_id":3, "person":{"cell":0,"home":0}}'
)
for(obj in objs) {
mongo.insert(M, ns, json_to_bson(obj))
}
让我们通过MongoDB shell看一下是否成功插入了它们:
Let's see via MongoDB shell if they were successfully inserted:
> use dbx
switched to db dbx
> db.collx.find().pretty()
{ "_id" : -1 }
{ "_id" : -2, "person" : { } }
{ "_id" : -3, "person" : { "x" : 0 } }
{ "_id" : 1, "person" : { "cell" : 0 } }
{ "_id" : 2, "person" : { "home" : 0 } }
{ "_id" : 3, "person" : { "cell" : 0, "home" : 0 } }
现在让我们通过查询来搜索文档:
Now let's search for documents with a query:
# searching for those objects
# JSON keys currently are expected to be wrapped in double quotes!
json_qry <-
'{
"$or" : [
{"person.cell": { "$exists" : true } },
{"person.home": { "$exists" : true } }
]
}'
cur <- mongo.find(M, "dbx.collx", json_to_bson(json_qry))
while(mongo.cursor.next(cur)) {
print(mongo.cursor.value(cur))
}
这就是我们最终得到的:
And this is what we get in the end:
_id : 1 1.000000
person : 3
cell : 1 0.000000
_id : 1 2.000000
person : 3
home : 1 0.000000
_id : 1 3.000000
person : 3
cell : 1 0.000000
home : 1 0.000000
这篇关于rmongodb:在查询中使用$ or的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!