Saturday, 15 June 2013

java - How to calculate length of an array in mongo collection? -



java - How to calculate length of an array in mongo collection? -

i have next construction in mongo collection

[{ "_id" : objectid("543fa67e9672ec37ebe3d028"), "customerid" : "5429aed703646b52def71736", "tagname" : "abc", "tagmembers" : [ "111", "112", "123" ] }, { "_id" : objectid("543fa67e9672ec37ebe3d029"), "customerid" : "5429aed703646b52def71736", "tagname" : "pqr", "tagmembers" : [ "111", "112", "123" ] }]

i want calculate count of each tagmember.

how calculate using mongo ??

is possible count without using aggregate??

use aggregation framework , $size operator:

> db.test.aggregate([ { "$project" : { "_id" : 0, "tagmemberslength" : { "$size" : "$tagmembers" } } } ]) { "tagmemberslength" : 3 } { "tagmemberslength" : 3 }

java mongodb

No comments:

Post a Comment