0

I have data in mongodb like this:

{
    "_id" : ObjectId("55a12bf6ea1956ef37fe4247"),
    "tempat_lahir" : "Paris",
    "tanggal_lahir" : ISODate("1985-07-10T17:00:00.000Z"),
    "gender" : true,
    "family" : [ 
        {
            "nama" : "Robert Deniro",
            "tempat_lahir" : "Bandung",
            "tanggal_lahir" : ISODate("2015-07-09T17:00:00.000Z"),
            "pekerjaan" : "IRT",
            "hubungan" : "XXX",
            "tanggungan" : false,
            "_id" : ObjectId("55a180f398c9925299cb6e90"),
            "meta" : {
                "created_at" : ISODate("2015-07-11T20:59:25.242Z"),
                "created_ip" : "127.0.0.1",
                "modified_at" : ISODate("2015-07-12T15:54:39.682Z"),
                "modified_ip" : "127.0.0.1"
            }
        }, 
        {
            "nama" : "Josh Groban",
            "tempat_lahir" : "Jakarta",
            "tanggal_lahir" : ISODate("2015-06-30T17:00:00.000Z"),
            "pekerjaan" : "Balita",
            "hubungan" : "Lain-Lain",
            "tanggungan" : true,
            "_id" : ObjectId("55a29293c65b144716ca65b2"),
            "meta" : {
                "created_at" : ISODate("2015-07-12T16:15:15.675Z"),
                "created_ip" : "127.0.0.1"
            }
        }
    ]
}

when i try to find data in sub-document, with this code:

person.findOne({ _id: req.params.person, {'family.nama': new RegExp('robert', 'gi') }}, function(err, data){
  // render code here
});

It show all data in Family Data, Can we fetch or display a data only match with criteria/keyword, for example only "Robert Deniro" row

Thank You

Rampak
  • 115
  • 1
  • 11
  • Are you looking for this [Retrieve only the queried element in an object array in MongoDB collection](http://stackoverflow.com/questions/3985214/retrieve-only-the-queried-element-in-an-object-array-in-mongodb-collection)? – hassansin Jul 12 '15 at 16:43

2 Answers2

5

In 'regular' MongoDB, you can use the $ operator for that. I'm not sure if it works with Mongoose, but it's worth a try:

person.findOne({ 
  _id           : req.params.person,
  'family.nama' : new RegExp('robert', 'gi')
}, {
  // Only include the subdocument(s) that matched the query.
  'family.$'    : 1
}, function(err, data){
  // render code here
});

If you need any of the properties from the parent document (tempat_lahir, tanggal_lahir or gender; _id will always be included), you need to add them to the projection object explicitly.

One caveat: the $ operator will only return the first matching document from the array. If you need it to return multiple documents, you can't use this method and (AFAIK) have to postprocess the results after they are returned from the database.

robertklep
  • 198,204
  • 35
  • 394
  • 381
0

It solved with this code:

var options = {
  family: {
    $elemMatch: { nama: req.query.keyword }
  },
};

person.findOne({ _id: req.params.person, 'family.nama': keyword }, options, function(err, data){
  //render code here
});

Thanks to @hassansin & @robertklep

Rampak
  • 115
  • 1
  • 11