mongodb - Mongo DB: cross collection query (join style) -
i have db 2 collections: users , pages.
in app each user can have 0 or more pages, each page belongs 1 or more users.
each user has pages
property consisting in array of objects id
(not default _id
field custom id
coming social network i'm taking page data from) of page , couple other page info.
pages have lot of props, 1 of is_synced
(boolean).
i want query users own synced pages (i want properties of page object, title
).
i tried javascript function in console foreach
ing on users query respective pages takes lifetime complete.
i have no indexes on collections except default ones on _id
fields (don't know how set/use them).
what approach suggest?
should make index on page custom id
field? should save needed page properties in users collection if not needed application logic administration purposes? should perform form of aggregation (map/reduce or similar)?
update
as suggested i'm adding simplified version of json models...
user
{ "_id" : objectid("555bd93562ed89ff43d792ce"), "facebook_data" : { profile: { id: "98765456789", displayname: "some name" }, ... "pages" : [ { "id" : "12345678909876", "title" : "my awesome page", ... "is_published" : true }, ... ], ... }, "ready" : true }
page
{ "_id" : objectid("556632b1cb44ccc10c59b82b"), "facebook_page_id" : "12345678909876", ... "is_synced" : true, ... "facebook_page_data" : { "id" : "12345678909876", "title" : "my awesome page", ... }, ... }
...and (rough) function i'm using retrieve administration data need...
db.users.find().foreach(function(user){ var pages = [] if(user.facebook_data.pages && user.facebook_data.pages.length) { var page_ids = user.facebook_data.pages.map(function (page) { return page.id }) pages = db.pages.find({'facebook_page_id': {$in:page_ids}, 'is_synced': true}) .map(function (page) { return page.facebook_page_data.title }) } print([ user.facebook_data.profile.displayname || '', user.facebook_data.profile.id, pages.join(',') ].join(';')) })
Comments
Post a Comment