rmongodb检测和清除坏文件
Rmongodb detection and removal of bad documents
我有一个 mongo 集合,有时会因为在使用 [= 创建的 bson 对象上使用 mongo.insert() 而不是 mongo.insert.batch() 而弄乱21=]().
请看这个可重现的例子,首先我介绍了一个坏文件,然后是几个好文件,我试图检测并删除坏文件。检测似乎成功了,但用 mongo.remove() by _id 删除却没有成功。
如果此脚本有效,长度(x) 应该在检测和删除后缩短一个记录。
谢谢。
library(rmongodb)
ns <- "testdb.del"
mongo <- mongo.create()
mongo.drop(mongo,ns)
df1 <- data.frame(numbers=rnorm(7),alphas=letters[1:7],monumbs=1:7)
df1
b <- mongo.bson.from.df(df1)
mongo.insert(mongo,ns,b) #insert bad document
mongo.insert.batch(mongo,ns,b) #insert good documents
x <- mongo.find.all(mongo,ns)
length(x)
for(i in 1:length(x)){
if(is.na(names(x[[i]]["numbers"]))){ #bad documents don't have the names in the right place
print("bad document. Trying to remove.")
rm.id <- mongo.bson.from.list(x[[i]]["_id"]) #grab _id of bad document and turn to bson
mongo.remove(mongo,ns,rm.id) #remove it.
}
}
x <- mongo.find.all(mongo,ns)
length(x)
使用x <- mongo.find.all(mongo,ns, mongo.oid2character = F)
。在您的查询中 mongodb oid
已转换为字符。
我有一个 mongo 集合,有时会因为在使用 [= 创建的 bson 对象上使用 mongo.insert() 而不是 mongo.insert.batch() 而弄乱21=]().
请看这个可重现的例子,首先我介绍了一个坏文件,然后是几个好文件,我试图检测并删除坏文件。检测似乎成功了,但用 mongo.remove() by _id 删除却没有成功。
如果此脚本有效,长度(x) 应该在检测和删除后缩短一个记录。
谢谢。
library(rmongodb)
ns <- "testdb.del"
mongo <- mongo.create()
mongo.drop(mongo,ns)
df1 <- data.frame(numbers=rnorm(7),alphas=letters[1:7],monumbs=1:7)
df1
b <- mongo.bson.from.df(df1)
mongo.insert(mongo,ns,b) #insert bad document
mongo.insert.batch(mongo,ns,b) #insert good documents
x <- mongo.find.all(mongo,ns)
length(x)
for(i in 1:length(x)){
if(is.na(names(x[[i]]["numbers"]))){ #bad documents don't have the names in the right place
print("bad document. Trying to remove.")
rm.id <- mongo.bson.from.list(x[[i]]["_id"]) #grab _id of bad document and turn to bson
mongo.remove(mongo,ns,rm.id) #remove it.
}
}
x <- mongo.find.all(mongo,ns)
length(x)
使用x <- mongo.find.all(mongo,ns, mongo.oid2character = F)
。在您的查询中 mongodb oid
已转换为字符。