3

QUERY

I'm trying to query a collection using the code below, as suggested here. It works fine for venueName but not for venueLocation - I'm guessing it must be something to do with the fact that it's a sub-schema, and I'm not writing the query properly.

    var query = {};
    if(Session.get('venueNameVar')) {
       query.venueName = Session.get('venueNameVar')
    }
    if(Session.get('venueLocationVar')){
       query.venueAddress = {
       neighbourhood : Session.get('venueLocationVar')
    }
    return Venues.find(query);

COLLECTIONS

My main schema and sub-schema work well across the app so far:

//MAIN SCHEMA
Schema.Venues = new SimpleSchema({
    venueAddress: {
        type: Schema.VenueAddress,
        optional: true
    }, [...]

//SUB-SCHEMA
Schema.VenueAddress = new SimpleSchema({
    neighbourhood: {
        type: String,
        max: 100,
        optional: true
    }, [...]

WHAT I'VE TRIED

  1. Using Schema.venueAddress.neighbourhood = Sesssion.get('venueLocationVar') - does not work
  2. Changing VenueAddress --> venueAddress - does not work
  3. Using square brackets, equals instead of colon, etc. - does not work
Community
  • 1
  • 1
Sekoul
  • 1,361
  • 2
  • 14
  • 30

1 Answers1

2

can you try to do query['venueAddress.neighbourhood'] = someVal im not positive that minimongo can search using objects

In other words you have to use dot notation when doing queries

  • You got it Patrick - I used your code and it works flawlessly. Could you clarify why you used `query['venue...` instead of `query.['venue...` I'm not that familiar with dot notation. – Sekoul Dec 14 '15 at 17:12
  • 1
    So an JSON `object` is kind of like an `[]` except your index has a alphanumeric value. therefor you can access `query.something` via `query['something']`. With mongodb you use what is refered to as `dot notation` to look up things in the DB that are embedded your can read more on it [https://docs.mongodb.org/v3.0/core/document/#dot-notation](here). To answer your question why not to use `query.['venue...` that is because the syntax is invalid. You can only look up values using `object.key` or `object['key']` – Patrick Mencias-lewis Dec 14 '15 at 17:18