Skip to content

Instantly share code, notes, and snippets.

@lehtu
Last active November 10, 2016 11:16
Show Gist options
  • Select an option

  • Save lehtu/70c046713ec93dff100481633169b2e9 to your computer and use it in GitHub Desktop.

Select an option

Save lehtu/70c046713ec93dff100481633169b2e9 to your computer and use it in GitHub Desktop.

Counts how many items exists in db.orders.products array.

Inside aggregate query you only need to chance $products to be the property which contains an array

db.orders.aggregate([{$project: {item: 1, bookings: {$size:"$products"}}}])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment