我偶然发现了一些关于函数处理更新(mongo.update()
)的问题。
免责声明
由于我仍在熟悉MongoDB和rmongodb包,所以插图中的问题很可能是由于我缺乏知识;-)
准备工作
确保加载包
pkg <- "rmongodb"
lib <- file.path(R.home(), "library")
if (!suppressWarnings(require(pkg, lib.loc=lib, character.only=TRUE))) {
install.packages(pkg, lib=lib)
require(pkg, lib.loc=lib, character.only=TRUE)
}
全局变量
db <- "__test"
ns.0 <- "user"
ns <- paste(db, ns.0, sep=".")
con <- mongo.create(db=db)
创建数据库的初始状态
确保空数据库
mongo.remove(mongo=con, ns=ns,
criteria=mongo.bson.from.list(lst=list(namefirst="John")))
插入文档
fields <- list(namefirst="John", namelast="Doe")
b <- mongo.bson.from.list(lst=fields)
mongo.insert(mongo=con, ns=ns, b=b)
if (is.null(mongo.find.one(mong=con, ns=ns, query=b))) {
stop("Something went wrong")
}
更新/更新文档
criteria <- list(namefirst="John", namelast="Smith")
fields <- list(namefirst="John", namelast="Smith", age=30)
b <- mongo.bson.from.list(lst=fields)
crit <- mongo.bson.from.list(lst=criteria)
mongo.update(mongo=con, ns=ns, criteria=crit, objNew=b,
flags=mongo.update.upsert)
if (is.null(mongo.find.one(mong=con, ns=ns, query=b))) {
stop("Something went wrong")
}
列出可用的文档
criteria <- list(namefirst="John")
crit <- mongo.bson.from.list(lst=criteria)
cursor <- mongo.find(mongo=con, ns=ns, query=crit)
out <- NULL
while (mongo.cursor.next(cursor=cursor)) {
bson <- mongo.cursor.value(cursor=cursor)
out <- c(out, list(bson))
}
> out
[[1]]
_id : 7 50f484fe3c3b8b8e3daa72e0
namefirst : 2 John
namelast : 2 Doe
[[2]]
_id : 7 50f484ff3c3b8b8e3daa72e1
namefirst : 2 John
namelast : 2 Smith
age : 1 30.000000
此时,集合中有两个文档,这是我们所期望的。
问题 #1:使用默认标志值更新 ( 0
)
criteria <- list(namefirst="John")
fields <- list(age=99)
b <- mongo.bson.from.list(lst=fields)
crit <- mongo.bson.from.list(lst=criteria)
mongo.update(mongo=con, ns=ns, criteria=crit, objNew=b)
cursor <- mongo.find(mongo=con, ns=ns, query=crit)
out <- NULL
while (mongo.cursor.next(cursor=cursor)) {
bson <- mongo.cursor.value(cursor=cursor)
out <- c(out, list(mongo.bson.to.list(bson)))
}
if (out[[1]]$age != 99) {
stop("Something went wrong")
}
> out
[[1]]
[[1]]$`_id`
{ $oid : "50f484ff3c3b8b8e3daa72e1" }
[[1]]$namefirst
[1] "John"
[[1]]$namelast
[1] "Smith"
[[1]]$age
[1] 30
执行更新后,只剩下一个文档。
问题 #2:使用“multi”标志值更新 ( 2
)
从头开始,因为以前的更新似乎已经损坏了数据库:
确保空数据库
mongo.remove(mongo=con, ns=ns,
criteria=mongo.bson.from.list(lst=list(namefirst="John")))
插入文档
fields <- list(namefirst="John", namelast="Doe")
b <- mongo.bson.from.list(lst=fields)
mongo.insert(mongo=con, ns=ns, b=b)
if (is.null(mongo.find.one(mong=con, ns=ns, query=b))) {
stop("Something went wrong")
}
更新/更新文档
criteria <- list(namefirst="John", namelast="Smith")
fields <- list(namefirst="John", namelast="Smith", age=30)
b <- mongo.bson.from.list(lst=fields)
crit <- mongo.bson.from.list(lst=criteria)
mongo.update(mongo=con, ns=ns, criteria=crit, objNew=b,
flags=mongo.update.upsert)
if (is.null(mongo.find.one(mong=con, ns=ns, query=b))) {
stop("Something went wrong")
}
尝试更新多个文档。我在这里追求的是在两个集合中更改age
to的值99
criteria <- list(namefirst="John")
fields <- list(age=99)
b <- mongo.bson.from.list(lst=fields)
crit <- mongo.bson.from.list(lst=criteria)
mongo.update(mongo=con, ns=ns, criteria=crit, objNew=b,
flags=mongo.update.multi)
cursor <- mongo.find(mongo=con, ns=ns, query=crit)
out <- NULL
while (mongo.cursor.next(cursor=cursor)) {
bson <- mongo.cursor.value(cursor=cursor)
out <- c(out, list(mongo.bson.to.list(bson)))
}
调查当前数据库状态
> out
[[1]]
[[1]]$`_id`
{ $oid : "50f485cd3c3b8b8e3daa72e2" }
[[1]]$namefirst
[1] "John"
[[1]]$namelast
[1] "Doe"
[[2]]
[[2]]$`_id`
{ $oid : "50f485ce3c3b8b8e3daa72e3" }
[[2]]$namefirst
[1] "John"
[[2]]$namelast
[1] "Smith"
[[2]]$age
[1] 30
> sapply(out, function(ii) ii$age)
[[1]]
NULL
[[2]]
[3] 30
我希望两个文档都有age = 99
.