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

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

5 Comments

i tried splitting my collections and it result in this gist.github.com/4f33aaab970ccd0c7c09 maybe it's easier for making queries
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?
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
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?
I think i get how this mongodb collections work now, thx for your help
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

1 Comment

typing error, the res.json does content-type: application/json and send the body

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.