简体   繁体   English

使用rmongodb将mongo objectid转换为字符串

[英]Convert mongo objectid to string using rmongodb

I'm trying to convert a mongo collection to an R data frame. 我正在尝试将mongo集合转换为R数据帧。 I'm using rmongodb package to do the same. 我正在使用rmongodb包来做同样的事情。 I'm facing a problem with fields with the datatype "ObjectId" (in the code below, the column "_id"), rmongodb does not seem to recognize them as valid fields and thus excluding them from the OP dataframe. 我遇到了数据类型为“ObjectId”的字段的问题(在下面的代码中,列为“_id”),rmongodb似乎没有将它们识别为有效字段,因此将它们从OP数据帧中排除。 Is there a way to bulk convert ObjectId fields to string or extract their values? 有没有办法将ObjectId字段批量转换为字符串或提取其值?

query <- list(
mongo.bson.from.JSON('
{
"$project" : {
"_id" :0,
"app_id" : "$_id",
"column_1" : "$mongo_column_1",
#########################

I've already tried the functions: 我已经尝试过这些功能:

ObjectId.toString()

and

ObjectId.valueOf()

But I think I'm making some syntactical errors and can't seem to find the right syntax to work in this situation. 但我认为我正在制作一些语法错误,似乎无法在这种情况下找到合适的语法。

PS I'm parsing the entire collection and not individual records. PS我正在解析整个集合而不是单个记录。

I had the same problem this morning. 今天早上我遇到了同样的问题。 Try using the mongo.oid.to.string function. 尝试使用mongo.oid.to.string函数。

l = mongo.bson.to.list(mongo.cursor.value(cursor))

k <- mongo.oid.to.string(l[["_id"]])
k
"5242aef73edb350519b2bf3f"

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

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