使用 Mongolite 向集合中插入条目时,如何取回 ObjectID?
When using Mongolite to insert an entry into a collection, how do you get back the ObjectID?
我在一个小项目中,Mongo 数据库中有一些集合与其他集合具有一对多关系。
假设我有一个名为 Company
的集合,它与 Employee
具有一对多的关系。在 R 中,如果我刚刚创建了一个公司实例,并且我做了类似 returnValue <- companyCollection$insert(Company)
的操作,我想获得一个 return 值,该值指示新插入的 objectId
公司群岛我想要这个是因为我计划创建 Employee
个实例,其中有一个名为 companyId
的字段,该字段将包含该公司的 objectId
作为一个领域。在使用 mongolite 向集合中插入 1 个条目时,有什么方法可以获得 objectId
returned 吗?
我知道如果直接使用 mongo,您可以使用 db.collection.insertOne()
取回对象 ID,但我没有看到使用 R 的 mongolite 包装器的此类选项。
如果 mongolite 无法做到这一点,您如何指定“_id”属性,以便在将条目插入集合时,mongo 将其视为类型 "ObjectID" 而不是 "String"?目前,如果我提供自己的 _id,mongo 会将 _id 视为字符串而不是对象 ID。 Mongo 罗盘显示我插入的文档 ID 为:
而不是这个:
在将文档插入集合时,我无法找到获取生成的对象 ID 的方法,但我最终只是使用了一种解决方法。解决方法是在您的文档中有一个具有 UUID 的临时字段,然后使用该 uuid 再次查找该对象。之后,您可以获取 mongo 生成的 _id
并删除创建的临时字段。这是一个执行此操作的函数。
# an example of a collection
myTableCollection<- mongo("myTable", url = "mongodb://localhost:27017/myDatabase")
# This is a function to insert a dataframe into mongo collection as a document,
# and get back the ObjectID that was generated by mongo
storeIntoCollection <- function(document, collection){
# so create a temporary ID to find the entry in the database again
temp <- UUIDgenerate()
document$creationID <- temp
# insert the DB Object
returnValue = collection$insert(document)
# query string to look up object using temp id
id_string <- paste('{"creationID" : "' , temp , '"}', sep="")
# Get mongo DB object just inserted
insertedDocument = collection$find(id_string, field = '{}')
# delete the temporary 'creationID' field
update_string <- paste('{ "$unset" : {"creationID": ""} }', sep="")
collection$update(id_string, update_string)
# turn '_id' to 'id'
colnames(document)[colnames(document)=="_id"] <- "id"
return(insertedDocument$id)
}
我在一个小项目中,Mongo 数据库中有一些集合与其他集合具有一对多关系。
假设我有一个名为 Company
的集合,它与 Employee
具有一对多的关系。在 R 中,如果我刚刚创建了一个公司实例,并且我做了类似 returnValue <- companyCollection$insert(Company)
的操作,我想获得一个 return 值,该值指示新插入的 objectId
公司群岛我想要这个是因为我计划创建 Employee
个实例,其中有一个名为 companyId
的字段,该字段将包含该公司的 objectId
作为一个领域。在使用 mongolite 向集合中插入 1 个条目时,有什么方法可以获得 objectId
returned 吗?
我知道如果直接使用 mongo,您可以使用 db.collection.insertOne()
取回对象 ID,但我没有看到使用 R 的 mongolite 包装器的此类选项。
如果 mongolite 无法做到这一点,您如何指定“_id”属性,以便在将条目插入集合时,mongo 将其视为类型 "ObjectID" 而不是 "String"?目前,如果我提供自己的 _id,mongo 会将 _id 视为字符串而不是对象 ID。 Mongo 罗盘显示我插入的文档 ID 为:
而不是这个:
在将文档插入集合时,我无法找到获取生成的对象 ID 的方法,但我最终只是使用了一种解决方法。解决方法是在您的文档中有一个具有 UUID 的临时字段,然后使用该 uuid 再次查找该对象。之后,您可以获取 mongo 生成的 _id
并删除创建的临时字段。这是一个执行此操作的函数。
# an example of a collection
myTableCollection<- mongo("myTable", url = "mongodb://localhost:27017/myDatabase")
# This is a function to insert a dataframe into mongo collection as a document,
# and get back the ObjectID that was generated by mongo
storeIntoCollection <- function(document, collection){
# so create a temporary ID to find the entry in the database again
temp <- UUIDgenerate()
document$creationID <- temp
# insert the DB Object
returnValue = collection$insert(document)
# query string to look up object using temp id
id_string <- paste('{"creationID" : "' , temp , '"}', sep="")
# Get mongo DB object just inserted
insertedDocument = collection$find(id_string, field = '{}')
# delete the temporary 'creationID' field
update_string <- paste('{ "$unset" : {"creationID": ""} }', sep="")
collection$update(id_string, update_string)
# turn '_id' to 'id'
colnames(document)[colnames(document)=="_id"] <- "id"
return(insertedDocument$id)
}