Description
Ankur Pathak opened DATAMONGO-1878 and commented
@Document(collection = "users")
class User {
@Id
String id;
@DBRef
Organization organization;
String name;
}
@Document(collection = "organizations")
class Organization {
@Id
String id;
}
User
will be stored in MongoDb like:
{
"_id": "1",
"organization": {
"$ref": "organizations",
"$id": 1
},
"name" : "Ankur Pathak"
}
But if we try to use DBRef
in aggreagation framework of MongoDb like below we will get error:
db.users.aggregate({ $group: {
_id: { name: "$name", orgId: "$organization.$id"}
}})
Their is a ugly way to use them in aggregation framework by converting them to array and then into object something like this:
db.foo.aggregate([{$project: {x: {$objectToArray: "$$ROOT.ref"}}}])
Check this url for reference and more detail: SERVER-14466
It will be great if we have some different or pluggable way for serialiazation and deserialization of
DBRef
s. Something like this:
@Document(collection = "users")
class User {
@Id
String id;
@DBRef(embedded=true) or @EmbeddedDBRef
Organization organization;
String name;
}
User will be stored in MongoDb like:
{
"_id": "1",
"organization": {
"namespace": "organizations",
"id": 1
},
"name" : "Ankur Pathak"
}
Now I can easily use use DBref in aggregation frmaework like this:
db.users.aggregate({ $group: {
_id: { name: "$name", orgId: "$organization.id"}
}})
Storing dbref like this will make them easily useable in aggregation framework
and for left joins in aggreagation framework($lookup).
Reference URL: https://jira.mongodb.org/browse/SERVER-14466