BSON::Code - BSON type wrapper for Javascript code
| Use Case | Command | Description |
|---|---|---|
| ✨ Create basic code | bson_code("alert('hi');") | Wraps plain JavaScript as BSON code |
| 🔗 Create code with scope | bson_code("return x;", {x => 1}) | Code with variables in closure |
| 📏 Get code length | $code->length | Returns length of the code string |
| 🔄 Convert to extended JSON | $code->TO_JSON (with BSON_EXTJSON) | Produces MongoDB extended JSON doc |
use BSON::Types ':all';
$code = bson_code( $javascript );
$code = bson_code( $javascript, $scope );
See also: BSON::Types
This module provides a BSON type wrapper for the "Javascript code" type and the "Javascript with Scope" BSON types.
A string containing Javascript code. Defaults to the empty string.
An optional hash reference containing variables in the scope of code. Defaults to undef.
Returns the length of the code attribute.
If the BSON_EXTJSON option is true, returns a hashref compatible with MongoDB's extended JSON <https://github.com/mongodb/specifications/blob/master/source/extended-json.rst> format, which represents it as a document as follows:
{"$code" : "<code>"}
{"$code" : "<code>", "$scope" : { ... } }
If the BSON_EXTJSON option is false, an error is thrown, as this value can't otherwise be represented in JSON.
This software is Copyright (c) 2020 by Stefan G. and MongoDB, Inc.
This is free software, licensed under:
The Apache License, Version 2.0, January 2004
Generated by phpman v4.9.25-4-g0d844aa · Markdown · JSON · MCP Author: Che Dong Under GNU General Public License
2026-07-07 09:04 @216.73.217.93
CrawledBy Mozilla/5.0 AppleWebKit/537.36 (KHTML, like Gecko; compatible; ClaudeBot/1.0; +claudebot@anthropic.com)
Enhanced by LLM: deepseek-v4-pro / taotoken.net / www.chedong.com - original format