ObjectId.toString()
Nesta página
ObjectId.toString()
Returns the string representation of the
ObjectId()
.
Compatibilidade
Você pode utilizar o ObjectId.toString()
para implantações hospedadas nos seguintes ambientes:
MongoDB Atlas: o serviço totalmente gerenciado para implantações do MongoDB na nuvem
MongoDB Enterprise: a versão autogerenciada e baseada em assinatura do MongoDB
MongoDB Community: uma versão com código disponível, de uso gratuito e autogerenciada do MongoDB
Exemplo
O seguinte exemplo:
Generates a new
ObjectId()
and stores it in the variablemyObjectId
.Creates a string representation of
myObjectId
using thetoString()
method.Stores the string representation in the variable
myObjectIdString
.
Run the following commands in mongosh
:
myObjectId = ObjectId("507c7f79bcf86cd7994f6c0e") myObjectIdString = myObjectId.toString()
The operation returns the following string:
507c7f79bcf86cd7994f6c0e
To confirm the type of myObjectIdString
, use the typeof
JavaScript operator:
typeof myObjectIdString
A operação retorna o seguinte:
string