• No results found

Database References

In document MongoDB Docs (Page 188-191)

db.users.insert({_id:counter("users"), name:"Sarah C."}) // _id : 1 db.users.insert({_id:counter("users"), name:"Bob D."}) // _id : 2 //repeat

See also "Insert if Not Present" section of the Atomic Operations page for another method.

UUIDs

The _id field can be of any type; however, it must be unique. Thus you can use UUIDs in the _id field instead of BSON ObjectIds (BSON ObjectIds are slightly smaller; they need not be worldwide unique, just unique for a single db cluster). When using UUIDs, your application must generate the UUID itself. Ideally the UUID is then stored in the [DOCS:BSON] type for efficiency – however you can also insert it as a hex string if you know space and speed will not be an issue for the use case.

See Also

Optimizing Object IDs ObjectId

Redirection Notice

This page should redirect to Object IDs.

Database References

Simple Direct/Manual Linking DBRef

See Also

As MongoDB is non-relational (no joins), references ("foreign keys") between documents are generally resolved client-side by additional queries to the server ("linking").

These links are always resolved client-side. Doing this directly/manually can be quite easy and is recommended.

There is also a DBRef mechanism which many drivers support which abstracts the linking concept somewhat. Generally though direct/manual links are the recommended approach.

Embedding objects is an alternative to linking and is often more appropriate and superior. See the schema design page and also the schema design presentation videos.

Simple Direct/Manual Linking

Generally, manually coded link resolution works just fine and is easy. We simply store the value that is present in _id in some other document in the database and then query it later. For example:

> // grab a random blog post:

> p = db.postings.findOne();

{

: ObjectId( ),

"_id" "4b866f08234ae01d21d89604"

: ,

"author" "jim"

:

"title" "Brewing Methods"

}

> // get more info on author of post p. this is the "linking" step.

> a = db.users.findOne( { _id : p.author } ) { "_id" : "jim", "email" : "[email protected]" }

> // inverse: given an author, find all blog posts for the author

> db.postings.find( {author : a._id } )

DBRef

DBRef is a more formal specification for creating references between documents. DBRefs (generally) include a collection name as well as an object id. Most developers only use DBRefs if the collection can change from one document to the next. If your referenced collection will always be the same, the manual references outlined above are more efficient.

A DBRef is a reference from one document (object) to another within a database. A database reference is a standard embedded (JSON/BSON) object: we are defining a convention, not a special type. By having a standard way to represent, drivers and data frameworks can add helper methods which manipulate the references in standard ways.

DBRef's have the advantage of allowing optional automatic client-side dereferencing with some drivers. In many cases, you can just get away with storing the _id as a reference then dereferencing manually as detailed in the "Simple Manual References" section above.

Syntax for a DBRef reference value is

{ $ref : <collname>, $id : <idvalue>[, $db : <dbname>] }

where <collname> is the collection name referenced (without the database name), and <idvalue> is the value of the _id field for the object referenced. $db is optional (currently unsupported by many of the drivers) and allows for references to documents in other databases (specified by <dbname>).

The ordering for DBRefs does matter, fields must be in the order specified above.

The old BSON DBRef datatype is deprecated.

DBRef in Different Languages / Drivers C#

Use the DBRef class. It takes the collection name and _id as parameters to the constructor. Then you can use the FollowReference method on the Database class to get the referenced document.

C++

The C++ driver does not yet provide a facility for automatically traversing DBRefs. However one can do it manually of course.

Java

Java supports DB references using the DBRef class. Javascript (mongo shell)

Example:

> x = { name : 'Biology' } { "name" : "Biology" }

> db.courses.save(x)

> x

{ "name" : "Biology", "_id" : ObjectId("4b0552b0f0da7d1eb6f126a1") }

> stu = { name : 'Joe', classes : [ new DBRef('courses', x._id) ] } // or we could write:

// stu = { name : 'Joe', classes : [ {$ref:'courses',$id:x._id} ] }

> db.students.save(stu)

> stu {

"name" : "Joe", "classes" : [ {

"$ref" : "courses",

"$id" : ObjectId("4b0552b0f0da7d1eb6f126a1") }

],

"_id" : ObjectId("4b0552e4f0da7d1eb6f126a2") }

> stu.classes[0]

{ "$ref" : "courses", "$id" : ObjectId("4b0552b0f0da7d1eb6f126a1") }

> stu.classes[0].fetch()

{ "_id" : ObjectId("4b0552b0f0da7d1eb6f126a1"), "name" : "Biology" }

>

Perl

The Perl driver does not provide a facility for automatically traversing DBRefs, but there is a CPAN package for it: MongoDBx::AutoDeref. You can also traverse them manually, of course.

PHP

PHP supports DB references with the MongoDBRef class, as well as creation and deferencing methods at the database (MongoDB::createDBRef and MongoDB::getDBRef) and collection (MongoCollection::createDBRef and MongoCollection::getDBRef) levels.

Python

To create a DB reference in python use the bson.dbref.DBRef class. You can also use the dereference method on Database instances to make dereferencing easier.

Python also supports auto-ref and auto-deref - check out the auto_reference example. Ruby

Ruby also supports DB references using the DBRef class and a dereference method on DB instances. For example:

@db = Connection.new.db("blog")

@user = @db["users"].save({:name => "Smith"})

@post = @db["posts"].save({:title => "Hello World", :user_id => @user.id})

@ref = DBRef.new("users", @post.user_id) assert_equal @user, @db.dereference(@ref)

See Also

Schema Design

GridFS

GridFS is a specification for storing large files in MongoDB. All of the mongodb.org supported drivers implement the GridFS spec. Rationale

Implementation Language Support Command Line Tools See also

Rationale

The database supports native storage of binary data within BSON objects. However, BSON objects in MongoDB are limited in size (4MB older versions, 16MB in v1.7/1.8, higher limits in the future). The GridFS spec provides a mechanism for transparently dividing a large file among multiple documents. This allows us to efficiently store large objects, and in the case of especially large files, such as videos, permits range operations (e.g., fetching only the first N bytes of a file).

Implementation

To facilitate this, a standard is specified for the chunking of files. Each file has a metadata object in a files collection, and one or more chunk objects in a chunks collection. Details of how this is stored can be found in the GridFS Specification; however, you do not really need to read that, instead, just look at the GridFS API in each language's client driver or mongofiles tool.

Language Support

Most drivers include GridFS implementations; for languages not listed below, check the driver's API documentation. (If a language does not include support, see the GridFS specification -- implementing a handler is usually quite easy.)

Command Line Tools

Command line tools are available to write and read GridFS files from and to the local filesystem.

See also

C++

A PHP GridFS Blog Article

Choosing a Shard Key contains a section describing how best to shard GridFS.

In document MongoDB Docs (Page 188-191)