MongoDB: How do I query for the number of elements in an array of documents/dictionaries?

Kyle Klockenbrink

I have a document that contains a list of dictionaries that show a user's login date and session id. How do I query for the number of total logins using MongoDB shell?

{

    "uname": "johndoe", 
    "loginActivity": [{}, {'date': '01/30/2018 15:52', 'SID': '02fe602a'}, {'date': '01/31/2018 20:32', 'SID': '2358eeea'}, {'date': '01/31/2018 20:06', 'SID': '3d720386'}, {'date': '01/25/2018 01:41', 'SID': '40b30ff2'}, {'date': '01/30/2018 15:55', 'SID': '4a3b9129'}, {'date': '01/31/2018 11:26', 'SID': '50fcead9'}, {'date': '01/29/2018 11:10', 'SID': '5f1f53c6'}, {'date': '01/25/2018 08:49', 'SID': 'a0123437'}, {'date': '01/26/2018 11:03', 'SID': 'c31daaf9'}, {'date': '01/31/2018 17:16', 'SID': 'd4073ae3'}, {'date': '01/25/2018 09:50', 'SID': 'd8cfe718'}, {'date': '01/25/2018 15:29', 'SID': 'e281c0cc'}, {'date': '01/26/2018 09:40', 'SID': 'e75e032a'}, {'date': '01/30/2018 10:26', 'SID': 'f3cc187f'}],
    "bytesIn": 18751,
    "bytesOut": 8343,
    "country": "US",
    "state": "California"

}

I have tried this query:

 db.user_information.aggregate( [
     { $match: {uname: "johndoe"}}, 
     { $unwind: "$login_activity"}, 
     { $group: {_id:0, total:{$sum:1}}} 
 ] )

But I get this result:

 { "_id" : 0, "total" : 1 }

I cannot figure out what I am doing wrong. The result should return 15 if I count the empty bracket in the beginning of the list.

mickl

You don't have to use $unwind here, all you need to do is to use $size operator

db.user_information.aggregate([
    { $match: {uname: "johndoe"}},
    { $project: { 
        uname: 1,
        total: { $size: "$loginActivity" }
      } 
    }
])

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How do we sum individual array elements in MongoDB aggregation query?

From Dev

How do I add an array of elements in MongoDB to an array in an existing document?

From Dev

How do I push elements to an existing array in MongoDB?

From Dev

How do I verify the number of elements and content of an array using ParameterMatchers?

From Dev

How do I find out the number of elements in a Javascript array?

From Dev

MongoDB query array elements

From Dev

query for number of array elements containing some field in mongodb

From Dev

How can I count the number of elements in an array?

From Java

How do I get the number of elements in a list?

From Dev

how do I print the number of elements

From Dev

how do I print the number of elements

From Dev

How I query array type via Mongodb

From Dev

Mongodb query on nested array elements

From Dev

How do I create every version of an array of a specified size with a specified number of elements?

From Dev

How do I describe the method header for a large number of input parameters which are the elements of array?

From Dev

Query for : How many elements of an array are matching within a string in mongoDb

From Dev

How do I add multiple elements to an array?

From Dev

How do I access the elements in this php array

From Dev

How can I modify my mongodb query so that the results do not contain specific values stored in an array?

From Dev

How do I transform my SQL query to a MongoDB query?

From Dev

How to query in MongoDB array

From Dev

How to query neighbor elements in MongoDB?

From Dev

How do I do a SQL query based on number of quantity

From Dev

How to do such query in MongoDB

From Dev

How do I number the nodes, if they are sometimes different elements

From Dev

How do I terminate variadic template recursion based on the number of elements?

From Dev

How do I count the number of elements in a table using a script on Azure?

From Dev

How do I know the number of elements in each cluster?

From Dev

How do i get the number of specific child elements in javascript

Related Related

  1. 1

    How do we sum individual array elements in MongoDB aggregation query?

  2. 2

    How do I add an array of elements in MongoDB to an array in an existing document?

  3. 3

    How do I push elements to an existing array in MongoDB?

  4. 4

    How do I verify the number of elements and content of an array using ParameterMatchers?

  5. 5

    How do I find out the number of elements in a Javascript array?

  6. 6

    MongoDB query array elements

  7. 7

    query for number of array elements containing some field in mongodb

  8. 8

    How can I count the number of elements in an array?

  9. 9

    How do I get the number of elements in a list?

  10. 10

    how do I print the number of elements

  11. 11

    how do I print the number of elements

  12. 12

    How I query array type via Mongodb

  13. 13

    Mongodb query on nested array elements

  14. 14

    How do I create every version of an array of a specified size with a specified number of elements?

  15. 15

    How do I describe the method header for a large number of input parameters which are the elements of array?

  16. 16

    Query for : How many elements of an array are matching within a string in mongoDb

  17. 17

    How do I add multiple elements to an array?

  18. 18

    How do I access the elements in this php array

  19. 19

    How can I modify my mongodb query so that the results do not contain specific values stored in an array?

  20. 20

    How do I transform my SQL query to a MongoDB query?

  21. 21

    How to query in MongoDB array

  22. 22

    How to query neighbor elements in MongoDB?

  23. 23

    How do I do a SQL query based on number of quantity

  24. 24

    How to do such query in MongoDB

  25. 25

    How do I number the nodes, if they are sometimes different elements

  26. 26

    How do I terminate variadic template recursion based on the number of elements?

  27. 27

    How do I count the number of elements in a table using a script on Azure?

  28. 28

    How do I know the number of elements in each cluster?

  29. 29

    How do i get the number of specific child elements in javascript

HotTag

Archive