2

I have an an array of objects which contain an artist, a song title and some other data, for example:

var obj = {  
  artist: 'Of Monsters and Men',  
  title: 'Little Talks',
  data: 'important thing'  
}

var array = [<obj1>, <obj2>, <obj3>];

I have also a mongoDB collection with contain songs. I need to query this collection and return all the songs which have the artist AND the title contained in the previous array. I also need to access the data field of the matching object.
I used to loop in the array and perform a query for each objects but it consumes too much resources, as there can be hundreds/thousands of objects in the array. Is there a way to achieve it?

2
  • you might as well accept an answer now... The people who have put their efforts in answering your question deserve that :) Commented Feb 8, 2014 at 18:04
  • I needed to fix another issue before testing that ;) Commented Feb 8, 2014 at 18:29

2 Answers 2

3

As @vmr suggested, you could change your schema, but in case you don't want to, here are a few options:

You could use the aggregation framework.

`db.collection.aggregate( { $group: {
    _id:{artist:"$artist", title:"$title"},
    data:{$push:"$data"}
}, function(err, result){})`

** Remember ** The resultant document must be less than 16MB.

Or, you could open a cursor like so

db.collections.find({ artist: 'some artist', title: 'some title'}, function(err, cursor){ //don't call .toArray() 
    function handleItem(err, item){ 
        if(item == null) return;
        //do stuff...
        cursor.nextObject(handleItem);
    }
    cursor.nextObject(handleItem);
})

This would most certainly cut down your iterator, and since each document is already less than 16 MB you won't have to worry about an aggregated document being too large

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

Comments

2

The ideal way to solve this problem would be to re-design the schema: The document schema should look something like this:

{ artist: 'Of Monsters and Men',
title: 'Little Talks',
data: 'important thing'
songs: [{song1 details},{song2 details},....] }

This way retrieval will be much faster as you need to access a single document for an artist.

2 Comments

Do you mean regrouping the songs by artist? If so I guess the title/data attributes should be moved to the songs details.
Yes you can do that as well, the current schema of yours results in redundant data. This can result in update anomalies unless you link the documents.

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.