Ottoman queries for referenced objects


#1

Hi,

I’m trying to follow an example for querying ‘sub references’ to models based on http://ottomanjs.com/#toc12

So using something like the following should work?

const CampaignElementModel = ottoman.model('CampaignElement', {
    campaign: {
        ref: 'Campaign'
    },
    element: {
        ref: 'Element'
    },
    rules: [{
        property: 'string',
        values: ['string'],
        rule: 'string'
    }]
}, {
    index: {
        findById: {
            by: '_id',
            type: 'refdoc'
        }
    }
}, {
    queries: {
        getAllCampaigns: {
            of: 'Campaign',
            by: '_id'
        }
    }
});

CampaignElementModel.findById(myId, function(err, myCampaignElement) {
    if(err) {
        throw err;
    }
    myCampaignElement.getAllCampaigns(function(err, campaigns) {
        console.log('campaigns ', campaigns)
    })
})

It doesn’t seem to be behaving at the moment - am I doing something wrong here?

Thanks

Glen


#2

Hey @glenswoop,

Can you describe the behaviour you are seeing that is incorrect?

Cheers, Brett


#3

Hi,

Sorry for delay. The example above is not “real code”, but what we were seeing was probably not wrong - we were expecting to see a “reverse” lookup of referenced objects and misinterpreting the doc’s:

ottoman.model('Post', {
  user: {ref:'User'},
  title: 'string',
  body: 'string'
}, {
  queries: {
    myUsers: {
      of: 'User',
      by: 'user'
    }
  }
});

ottoman.model('User', {
  name: 'string'
});

We’d tried something like the above to get “child” documents, but it hadn’t worked, so we used a different approach in the end.

Thanks

Glen