0

This is my document.

{ 
    "_id" : "exportadores", 
    "instancia" : ISODate("2018-03-23T11:36:32.296+0000"), 
    "dwFatPDFs" : {
            "regis" : [
                {
                    "cod" : "A1", 
                    "idFat" : NumberLong(15772), 
                    "data" : ISODate("2018-04-13T12:33:44.149+0000"), 
                    "ipPm" : "192.168.0.1"
                }, 
                {
                    "cod" : "A2", 
                    "idFat" : NumberLong(15772), 
                    "data" : ISODate("2018-04-13T13:54:26.477+0000"), 
                    "ipPm" : "192.168.0.1"
                }
            ]
    }
}

I'd like to get one result from array(regis) by the 'cod'.

For example. If search "cod" like "A1" I want to get something like this:

{ 
    "_id" : "exportadores",
    "dwFatPDFs" : {
            "regis" : [
                {
                    "cod" : "A1", 
                    "idFat" : NumberLong(15772), 
                    "data" : ISODate("2018-04-13T12:33:44.149+0000"), 
                    "ipPm" : "192.168.0.1"
                }             
            ]
    }
}

I'm sorry for my english.

Can someone help me?

Thanks

Myller M
  • 1
  • 1

1 Answers1

0

Use the dotted notation + the $ identifier to return the first sub documents that match the condition :

Collection.find({
    "dwFatPDFs.regs.cod": your_id
}, {
    "dwFatPdfs.regs.$.cod": 1
});

I think this query will do the job

Nicolas
  • 421
  • 4
  • 14