简体   繁体   English

Rmongodb检测和清除不良文件

[英]Rmongodb detection and removal of bad documents

I've got a mongo collection that sometimes got messed up by using mongo.insert() instead of mongo.insert.batch() on bson objects that were created with mongo.bson.from.df(). 我有一个mongo集合,有时会在使用mongo.bson.from.df()创建的bson对象上使用mongo.insert()而不是mongo.insert.batch()弄乱了。

Please see this reproducible example, first I introduce a bad document followed by several good documents and I've tried to detect and remove the bad documents. 请参阅此可重现的示例,首先介绍一个不良文档,然后再介绍几个不良文档,然后尝试检测并删除不良文档。 The detection seems to be successful, but not the removal with mongo.remove() by _id. 检测似乎已成功,但未通过_id使用mongo.remove()删除。

If this script worked, length(x) should be one record shorter after the detection and removal. 如果该脚本有效,则在检测和删除后,length(x)应该短一个记录。

Thanks. 谢谢。

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)

Use x <- mongo.find.all(mongo,ns, mongo.oid2character = F) . 使用x <- mongo.find.all(mongo,ns, mongo.oid2character = F) In your query mongodb oid s were converted into characters. 在查询中,mongodb oid转换为字符。

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

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