java - Retrieving count of specific attribute in MongoDB collection -
i have mongodb collection lot of data.
part of json documant looks this:
{ "_id":{ "$oid":"5364e0867a2690e2a2be13ff" }, "share_name":"test123", "data_objects":[ { "user":"the flying pirate.", "location":"devon, uk.", "share_id":462568869077716992 }, { "user":"the dragon.", "location":"london, uk.", "share_id":462568869077716992 }, { "user":"lozzien", "location":"miami, usa.", "share_id":462568869077716992 } ] }
as can see, single document can have list of data_objects. above document has 3 data objects. need total count of data objects in collection. how can total count?
what after "summing up" total length of array elements contained in document collection.
with mongodb 2.6 quite simple $size
operator:
db.collection.aggregate([ { "$group": { "_id": null, "totalsize": { "$sum": { "$size": "data_objects" } } }} ])
in prior versions going need other methods array "size". notably th $unwind
operator:
db.collection.aggregate([ { "$unwind": "$data_objects" }, { "$group": { "_id": null, "totalsize": { "$sum": 1 } }} ])
and since array content "de-normalized" it's matter of counting total entries. $sum
1 in case each expanded document in collection.
writing java driver not hard:
dbobject group = new basicdbobject( "$group", new basicdbobject( "_id",null ).append( "totalsize", new basicdbobject( "$sum", new basicdbobject( "$size", "data_objects" ) ) ) ); db.getcollection("collection").aggregate(arrays.aslist(group));
ideally though should consider "storing" array length in document. useful "query" purposes , negates need calculate "on fly". handle additions $inc
operator alongside $push
, $pull
:
for adding data array:
db.collection.update( { "_id": objectid("5364e0867a2690e2a2be13ff") }, { "$push": { "data_objects": newobject }, "$inc": { "data_count": 1 } } )
and removing:
db.collection.update( { "_id": objectid("5364e0867a2690e2a2be13ff") }, { "$pull": { "data_objects": removereqery }, "$inc": { "data_count": -1 } } )
then performing same aggregation of time:
db.collection.aggregate([ { "$group": { "_id": null, "totalsize": { "$sum": "$data_count" } }} ])
Comments
Post a Comment