Saturday, 6 May 2017

Mongoose Select Limit 1 Data Subdocument

I have this kind of data.

{
    title : "Title",
    List : [
        {
            _id : '123',
            content : "Hello"
        },
        {
            _id : '234',
            content : "World"
        }
    ]
}

I Want to select by _id from sub document list '123' with only 1 data. I have doing It by

MyModel.find({'List._id' : 123}, {"List.$": 1}, function(err, doc){
    if (err){
        res.send(err);
    }
    else{
        // data here
    }
})

But, the result not like what I expect. They only return the List data like this?

{[
    List : [
        {
            _id : '123',
            content : "Hello"
        }
    ]
]}

How do select include the parent document so the result is just like this.

{
    title : "Title",
    List : [
        {
            _id : '123',
            content : "Hello"
        }
    ]
}



via Adi Sparta

No comments:

Post a Comment