Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- {
- "_id" : ObjectId("5b453cc7799fb211dc44a1e8"),
- "id" : 1.0,
- "nestedDoc" : {
- "nestedArray" : [
- {
- "field1" : "value1",
- "field2" : "value2",
- "field3" : "value3"
- },
- {
- "field1" : "value11",
- "field2" : "value21",
- "field3" : "value31"
- },
- {
- "field1" : "value12",
- "field2" : "value22",
- "field3" : "value32"
- }
- ]
- }
- }
- {
- "_id" : ObjectId("5b453d23799fb211dc44a1e9"),
- "id" : 2.0,
- "nestedDoc" : {
- "nestedArray" : [
- {
- "field1" : "value1",
- "field2" : "value29",
- "field3" : "value39"
- },
- {
- "field1" : "value118",
- "field2" : "value281",
- "field3" : "value381",
- "field4" : "value281"
- },
- {
- "field1" : "value172",
- "field2" : "value272",
- "field3" : "value372"
- }
- ]
- }
- }
- db.test1.aggregate(
- [
- // Stage 1 : filter documents
- {
- $match: {
- id:1
- }
- },
- // Stage 2 : count array size and return only it.
- {
- $project: {
- arraySize:{$size:"$nestedDoc.nestedArray"},
- _id:0
- }
- },
- ]
- );
- db.test1.aggregate(
- [
- // Stage 1 count array size for each document
- {
- $project: {
- _id:0,
- arraySize:{$size:"$nestedDoc.nestedArray"}
- }
- },
- // Stage 2 group all documents and sum arraySize
- {
- $group: {
- _id:null,
- totalArraysSize:{$sum:"$arraySize"}
- }
- },
- ]
- );
Add Comment
Please, Sign In to add comment