Construyendo una consulta dinámica de mongo para meteorito

I'm building an app that has clickable 'filters'; I'm creating a list of objects(?) that I want to pass to a mongo 'find', so that I can pull out listings if selected attributes match a certain score.

My data is structured like this (a snippet):

    name: 'Entry One',
    location: {
      type: 'Point',
      coordinates: [-5.654182,50.045414]
    dogs: {
      score: '1',
      when: 'seasonal',
      desc: 'Dogs allowed from October to April'
    lifeguard: {
      score: '1',
      when: 'seasonal',
      desc: 'A lifeguard hut is manned between April and October',
      times: ''
    cafe: {
      score: '1',
      name:'Lovely cafe',
    }, ...

My search variable is a list of objects (I think?) that I assign to a session variable. If I output this session var ('searchString') via JSON.stringify, it looks like this:


I'd like to pass this to my mongo find so that it only lists entries that match these scores on these attributes, but it's returning zero results. Do I need to somehow make this an $and ¿consulta?

Actualmente tiene este aspecto:


Unfortunately as soon as I drop searchString into the find, I get zero results even if it's empty {}. (When it's just a find() the entries list fine, so the data itself is ok)

What am I doing wrong? I'm relatively new to mongo/meteor, so I apologise in advance if it's something stupidly obvious!

preguntado el 28 de mayo de 14 a las 11:05

¿Resolviste esto? -

I think this is the answer you were looking for: -

1 Respuestas

Don't stringify the query. Flatten the object instead. Example:

  "cafe.score": 1,
  "dogs.score": 1,

contestado el 28 de mayo de 14 a las 12:05

Thanks Hubert, your solution works in the console! Should I just build an array instead of objects for my searchString? join(), rather than build an object to flatten it? - BellamyStudio

Actually, your example looks like it's working, but it returns a null result set. I thought it might be because my data was storing the scores as strings and your example was looking for numbers, but no dice. I'll dig deeper. - BellamyStudio

I've tried building an array and joining it into a string to pass into the find, and also passing the query object straight into the find as well, but neither work! My query object looks correct, and when I type the equivalent query in to the console I get the result I expect, but as soon as I pass in my stored object I get nothing. Extremely frustrating! - BellamyStudio

¿Podría usted console.log the object you're passing as a query and post the result? - Huberto OG

Sure, I have been all along, I should have posted it! OK, so the one I'm currently trying to get working looks like this: Object {'cafe.score': 1} - BellamyStudio

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.