BSON::DBRef(3pm)

πŸ“¦
version 1.12.2 Β· 5 versions
, in debian bookworm, ubuntu jammy, ubuntu noble

πŸ’– BSON::DBRef πŸ’–

okay so bestie, welcome to the BSON::DBRef page β€” it's part of the User Contributed Perl Documentation, and it's literally the whole vibe for wrapping MongoDB DBRefs in BSON. sit down, grab a boba, let's go πŸ§‹βœ¨


✨ VERSION ✨

version v1.12.2 β€” yeah she's on v1.12.2, iconic, no notes πŸ’…


πŸ¦‹ SYNOPSIS πŸ¦‹

the way you summon her:

use BSON::Types ':all';

my $dbref = bson_dbref( $oid, $collection_name );

that's it. that's the whole ritual. πŸŽ€


🌸 DESCRIPTION 🌸

so this module hands you a BSON type wrapper for MongoDB Database References β€” you can peep the official docs over at http://docs.mongodb.org/manual/reference/database-references/ if you want the lore.

a DBRef is a special document format that points at another document in the database. and like β€” important β€” DBRefs are NOT the same as foreign keys. they do not give you any referential integrity, no constraint checking, nothing. zero. so a DBRef can absolutely point at a document that no longer exists. or one that <em>never</em> existed. it's giving "ghosted" 😭

and here's the tea: use of DBRefs is discouraged, so this module only exists for backwards compatibility. what you actually want are "Manual references" (https://docs.mongodb.com/manual/reference/database-references/#document-references) whenever you need to reference other documents. periodt. πŸ‘‘


πŸ’… ATTRIBUTES πŸ’…

πŸ“ id

Required. this is the "_id" value of the document you're referencing. and heads up: if that "_id" is an ObjectID, then bestie you must use a BSON::OID object. no shortcuts. πŸ™ˆ

🧚 ref

Required. the name of the collection where the referenced document lives. you can hand her either a MongoDB::Collection object or a plain string with the collection name. if it's an object, she'll coerce it to string form. slay ✨

fun fact: you can also pass this in the constructor as '$ref'.

πŸ’œ db

Optional. the database where the referenced document lives. again, either a MongoDB::Database object or a string with the database name β€” object gets coerced to string form.

but ngl, not all other language drivers support the $db field, so using this field is not recommended. just putting that out there 🫢

you can also pass this in the constructor as '$db'.

🌈 extra

Optional. a hash reference of extra fields in the DBRef document. not all MongoDB drivers support this feature and you should not rely on it. this attribute literally exists for one reason only: so DBRefs generated by drivers that do allow extra fields will round-trip correctly. that's it.

🚨 USE OF THIS FIELD FOR NEW DBREFS IS NOT RECOMMENDED. 🚨 like, at all. don't.

πŸ”₯ METHODS πŸ”₯

⭐ TO_JSON

so if the "BSON_EXTJSON" option is true, this returns a hashref compatible with MongoDB's extended JSON format (https://github.com/mongodb/specifications/blob/master/source/extended-json.rst), and it represents it as a document like this:

{ "$ref": "<collection name>", "$id": "<id>" }

and if the "BSON_EXTJSON" option is false? an error is thrown, because this value literally cannot be represented in JSON any other way. it's giving "no thoughts, head empty" but for JSON 😭


πŸ‘‘ AUTHORS πŸ‘‘

the legends behind this:

  • David Golden <david@mongodb.com>
  • Stefan G. <minimalist@lavabit.com>

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

and that's the whole page, bestie!! πŸ¦‹ this was written for perl v5.30.3 back on 2020-08-14 β€” yeah she's a little vintage but she still serves πŸ’– mwah, go forth and reference responsibly (or don't, apparently, since DBRefs are discouraged 😭) 🫢✨

πŸ“¦ where this page comes from