Quantcast

Mongodb, Can you persist a 'sub collection' only in the parent objects?

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Mongodb, Can you persist a 'sub collection' only in the parent objects?

rniedzial
Given the following two domain objects you end up with two top level collections in mongodb:

class Address {
   String address1
   String address2
   
}

class User {
    String name
    List<Address> addresses = []

    static embedded = ['addresses']
}


When you persist the User object you actually end up w/ two separate collections in mongodb, one for User with the embedded addresses collection, and then another top level collection for Address. Like so (this is from my mongodb console):

> show collections
address
user
> db.address.findOne()
{ "_id" : ObjectId("4f1050f34206e33214f92cc5"), "address1" : "31 Spooner Street", "address2" : null  }
> db.user.findOne()
{
"_id" : ObjectId("4f1050f34206e33214f92cc6"),
"addresses" : [
{
"address1" : "31 Spooner Street",
"address2" : null,
}
],
        "name" : "Peter"
}
>

Is there a way when embedding objects to only end up w/ one persisted collection? I looked over the plugin docs several times and didn't see anything that would allow me to do it.

Rafal



Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Mongodb, Can you persist a 'sub collection' only in the parent objects?

Graeme Rocher-4
Administrator
Declare the "Address" class in the same file as the User class (ie User.groovy)

Cheers

On Fri, Jan 13, 2012 at 5:32 PM, Rafal Niedzialkowski
<[hidden email]> wrote:

> Given the following two domain objects you end up with two top level
> collections in mongodb:
>
> class Address {
>    String address1
>    String address2
>
> }
>
> class User {
>     String name
>     List<Address> addresses = []
>
>     static embedded = ['addresses']
> }
>
>
> When you persist the User object you actually end up w/ two separate
> collections in mongodb, one for User with the embedded addresses collection,
> and then another top level collection for Address. Like so (this is from my
> mongodb console):
>
>> show collections
> address
> user
>> db.address.findOne()
> { "_id" : ObjectId("4f1050f34206e33214f92cc5"), "address1" : "31 Spooner
> Street", "address2" : null  }
>> db.user.findOne()
> {
> "_id" : ObjectId("4f1050f34206e33214f92cc6"),
> "addresses" : [
> {
> "address1" : "31 Spooner Street",
> "address2" : null,
> }
> ],
>         "name" : "Peter"
> }
>>
>
> Is there a way when embedding objects to only end up w/ one persisted
> collection? I looked over the plugin docs several times and didn't see
> anything that would allow me to do it.
>
> Rafal
>
>
>



--
Graeme Rocher
Grails Project Lead
SpringSource - A Division of VMware
http://www.springsource.com

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Mongodb, Can you persist a 'sub collection' only in the parent objects?

rniedzial
Great! Could we perhaps add that to the documentation somewhere? Unless its already there and hiding in plain sight. I thought I saw a jira issue stating you couldn't persist objects if they were declared in the same file.


Rafal



I actually noticed that if you have a simple

On Fri, Jan 13, 2012 at 11:52 AM, Graeme Rocher <[hidden email]> wrote:
Declare the "Address" class in the same file as the User class (ie User.groovy)

Cheers

On Fri, Jan 13, 2012 at 5:32 PM, Rafal Niedzialkowski
<[hidden email]> wrote:
> Given the following two domain objects you end up with two top level
> collections in mongodb:
>
> class Address {
>    String address1
>    String address2
>
> }
>
> class User {
>     String name
>     List<Address> addresses = []
>
>     static embedded = ['addresses']
> }
>
>
> When you persist the User object you actually end up w/ two separate
> collections in mongodb, one for User with the embedded addresses collection,
> and then another top level collection for Address. Like so (this is from my
> mongodb console):
>
>> show collections
> address
> user
>> db.address.findOne()
> { "_id" : ObjectId("4f1050f34206e33214f92cc5"), "address1" : "31 Spooner
> Street", "address2" : null  }
>> db.user.findOne()
> {
> "_id" : ObjectId("4f1050f34206e33214f92cc6"),
> "addresses" : [
> {
> "address1" : "31 Spooner Street",
> "address2" : null,
> }
> ],
>         "name" : "Peter"
> }
>>
>
> Is there a way when embedding objects to only end up w/ one persisted
> collection? I looked over the plugin docs several times and didn't see
> anything that would allow me to do it.
>
> Rafal
>
>
>



--
Graeme Rocher
Grails Project Lead
SpringSource - A Division of VMware
http://www.springsource.com

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

   http://xircles.codehaus.org/manage_email



Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Mongodb, Can you persist a 'sub collection' only in the parent objects?

Graeme Rocher-4
Administrator
Sure. The documentation sources are here:

https://github.com/SpringSource/grails-data-mapping/tree/master/grails-documentation-mongo

pull requests / contributes welcome :-)

Cheers

On Fri, Jan 13, 2012 at 6:58 PM, Rafal Niedzialkowski
<[hidden email]> wrote:

> Great! Could we perhaps add that to the documentation somewhere? Unless its
> already there and hiding in plain sight. I thought I saw a jira issue
> stating you couldn't persist objects if they were declared in the same file.
>
>
> Rafal
>
>
>
> I actually noticed that if you have a simple
>
> On Fri, Jan 13, 2012 at 11:52 AM, Graeme Rocher <[hidden email]> wrote:
>>
>> Declare the "Address" class in the same file as the User class (ie
>> User.groovy)
>>
>> Cheers
>>
>> On Fri, Jan 13, 2012 at 5:32 PM, Rafal Niedzialkowski
>> <[hidden email]> wrote:
>> > Given the following two domain objects you end up with two top level
>> > collections in mongodb:
>> >
>> > class Address {
>> >    String address1
>> >    String address2
>> >
>> > }
>> >
>> > class User {
>> >     String name
>> >     List<Address> addresses = []
>> >
>> >     static embedded = ['addresses']
>> > }
>> >
>> >
>> > When you persist the User object you actually end up w/ two separate
>> > collections in mongodb, one for User with the embedded addresses
>> > collection,
>> > and then another top level collection for Address. Like so (this is from
>> > my
>> > mongodb console):
>> >
>> >> show collections
>> > address
>> > user
>> >> db.address.findOne()
>> > { "_id" : ObjectId("4f1050f34206e33214f92cc5"), "address1" : "31 Spooner
>> > Street", "address2" : null  }
>> >> db.user.findOne()
>> > {
>> > "_id" : ObjectId("4f1050f34206e33214f92cc6"),
>> > "addresses" : [
>> > {
>> > "address1" : "31 Spooner Street",
>> > "address2" : null,
>> > }
>> > ],
>> >         "name" : "Peter"
>> > }
>> >>
>> >
>> > Is there a way when embedding objects to only end up w/ one persisted
>> > collection? I looked over the plugin docs several times and didn't see
>> > anything that would allow me to do it.
>> >
>> > Rafal
>> >
>> >
>> >
>>
>>
>>
>> --
>> Graeme Rocher
>> Grails Project Lead
>> SpringSource - A Division of VMware
>> http://www.springsource.com
>>
>> ---------------------------------------------------------------------
>> To unsubscribe from this list, please visit:
>>
>>    http://xircles.codehaus.org/manage_email
>>
>>
>



--
Graeme Rocher
Grails Project Lead
SpringSource - A Division of VMware
http://www.springsource.com

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


Loading...