0

i have a mongo collection like this

{"stores": [{"name": "foo",
             "songs": [ {"id": "", "name": "", "artist": "", "category": "", "tags": []} ],
             "songsSchedule": [
                               {
                                "song_id": "",
                                "date": ,
                                "user": "",
                                "help": ,
                                "partners": [{"user": ""}],
                                "likes":
                               }
                             ]
 }]}

and i want to get the songs name and artist from the songsSchedule song_id, i've tried this but it's not working

var query = { _id: fn.generateID(req.params.store_id), songsSchedule: { $exists: true } };
var select = { songsSchedule:1 };
var array = [];

client("stores", function(err, collection) {
    if (err) 
       return res.json(fn.status("30"));

  collection.findOne(query, select, function(err, store) {
        if (err || !store) 
           return res.json(fn.status("31"));

    for (var i in store.songsSchedule) {
      var song = store.songsSchedule[i];
      array.push(song.song_id);
    }

    collection.find({ _id: fn.generateID(req.params.store_id), "songs._id": { $in: array } }, function(err, songs) {

      res.json(songs);
    });
    });
});

and i dont really know if it's the best way of doing it

Subham
  • 1,414
  • 4
  • 17
  • 34
jtomasrl
  • 1,430
  • 3
  • 13
  • 22

2 Answers2

1

I'm not entirely clear what you mean by "get the songs name and artist from the songsSchedule song_id" but it looks like that query will be messy.

If it were me, I'd consider splitting out songs and songSchedule into their own collections for easier querying.

cirrus
  • 5,624
  • 8
  • 44
  • 62
  • i tried splitting my collections and it result in this https://gist.github.com/4f33aaab970ccd0c7c09 maybe it's easier for making queries – jtomasrl Nov 09 '12 at 15:36
  • 1
    OK, that looks OK, but what do you want to do with it? I could write an AF query to join stores to songs (name and artist for song_ids in a store) but I'm wondering if the most efficient way would be for songSchedule to contain name and artist, belong in it's own collection and tagged with the store ID. What exactly are the queries that you want to do? And what's the idea behind songSchedule? Is that likely to repeat song_ids a lot? – cirrus Nov 09 '12 at 22:48
  • it will repeat song_ids a lot as long as the users ask for that song. for example for songSchedule i'll want to grab the song title, artist, the user that ask for it, etc and i also need a place to store each song. Same with menu_items and items inside tables – jtomasrl Nov 10 '12 at 06:42
  • 1
    OK, so you have the list of song_ids, and now you can easily fetch all the song data for those songs. Do you still have a question about that? If so, can you share some more sample data and explain what outputs you're looking for? – cirrus Nov 12 '12 at 11:29
  • I think i get how this mongodb collections work now, thx for your help – jtomasrl Nov 12 '12 at 12:03
0

from your document example, the "songs" field contains documents that do not contain an "_id" field.

    "songs": [ {"name": "", "artist": "", "category": "", "tags": []} ]

But, your find() query is querying on the "songs._id" field.

Also, I'm not too familiar with the json() method, but does it handle cursors?

Regards,

Kay

Kay
  • 2,942
  • 18
  • 13