Reputation: 829
I have a document like this.
{
'_id': xxx,
'name': xxx,
'background': [],
'songs': [
{
'title': xxx,
'background': []
}
]
}
First, how could i just return song document not all. I want something like this.
function(err, doc) {
//the doc is {'title': xxx, 'background': []}
}
Second, how could i push a data in background array that doc is in songs.
collection.update(
{'_id': xxx, songs: {$elemMatch:{'title': xxx}},
{
$push: {'background': xxx}
}
)
This sample code always push the data to background of root doc not song doc.
Upvotes: 0
Views: 42
Reputation: 4363
You can limit fields returned from a Mongo query through a projection. This might look like
collection.find( { /*some criteria*/ }, { song: 1, _id:0 } )
To modify an element in an array within a document, you will need to use the positional $ operator
collection.update( {'_id': xxx, songs: {$elemMatch:{'title': xxx}}, { $push: {'songs.$.background': xxx} }, callback )
Upvotes: 1