Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Storing DBRef as Embedded Document for Easy Use In Aggregation Framework or Extension point for Serialization and Deserialization of DBRef [DATAMONGO-1878] #2780

Closed
spring-projects-issues opened this issue Feb 15, 2018 · 1 comment
Assignees
Labels
in: repository Repositories abstraction type: enhancement A general enhancement

Comments

@spring-projects-issues
Copy link

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

@christophstrobl
Copy link
Member

support added via #3602

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: repository Repositories abstraction type: enhancement A general enhancement
Projects
None yet
Development

No branches or pull requests

2 participants