BSON_VALUE_OBJECTID function

The BSON_VALUE_OBJECTID function is a built-in SQL function that returns the ObjectId values in a BSON document as strings.

Syntax

BSON_VALUE_OBJECTID function

BSON_VALUE_OBJECTID ( bson_column , " _id " )
Element Description Restrictions Syntax
bson_column Name of a BSON column Must exist Identifier

Usage

The first field in BSON documents that you create with MongoDB commands is the _id field. The _id field contains a generated ObjectId value that is a unique identifier for the document. Use the BSON_VALUE_OBJECTID function to retrieve ObjectId values as strings.

Example

The following MongoDB command that is run through the wire listener inserts a BSON document into the products table:

"db.products.insert({item:"card", qty:15})"

The following statement returns the entire contents of the products table:

SELECT data::json FROM products;

(expression)
{"_id":ObjectID(54befb9eedbc233cd3a4b2fb"),"item":"card","qty":15.000000}
1 row(s) retrieved.

The following statement returns the ObjectID value from the products table:

SELECT BSON_VALUE_OBJECTID(data,"_id") FROM products;

(expression)    54befb9eedbc233cd3a4b2fb
1 row(s) retrieved.