e.k
e.k

Reputation: 1363

Node js return array element in mongoose

Please consider this as a part of my schema.

newSchema({
   'product':{}
   'productPayment':[{}]
});

productPayment has 'n' number of elements each with unique Ids. I want to return only that array element which matches my id. I queried like schema.findOne({'_id':variables._id,'productPayment._id':variables.productPaymentId}) but it returns whole document, but I want that particular element only. How can I possibly get that? Any help will be appreciated. Thanks in advance.

Upvotes: 0

Views: 49

Answers (1)

Gaurav joshi
Gaurav joshi

Reputation: 1799

Take help from projections, something like this:-

 schema
.findOne({'_id':variables._id,'productPayment._id':variables.productPaymentId})
.select({ productPayment: {$elemMatch: {_id: variables.productPaymentId}}})

*this is just a sample code please modify according to your needs

Upvotes: 1

Related Questions