how to write mongodb query for 'counting elements in sub-document' -
i have collection this...
{ { id: 123, name: "abc", data: { type1: 0.78, type2: 0.89, type3: 0.67 } purpose: "commercial" }, { id: 124, name: "pqrs", data: { type1: 0.34, type2: 0.78, } purpose: "open" }, { id: 125, name: "xtec", data: { type1: 0.48, type2: 0.56, type3: 0.67 type4: 0.56, type5: 0.67 } purpose: "some other" } }
i count number of elements in "data" sub-document. 1 post me mongodb query?
or display documents "data" sub-document length = 3
the below query doesn't work
db.test.find({$where : 'data.length = 3'});
any appreciated. thank you!!
regards, raj
try this, put condition inside count method, can use condition use in find method.
db.test.count({...});
Comments
Post a Comment