How do i find the total number of subjectsthat has no prerequisites using agregation?

Jerwayne Hoo

I have tried several codes but it didn't work. Example from the database, one has a prerequisite and one does not have prerequisites and I would like to find the total number of the subject with no prerequisites :

db.Subject.insert(
{ 
    "_id":ObjectId(),
    "subject":{ 
        "subCode":"CSCI321",
        "subTitle":"Final Year Project",
        "credit":6,
        "type":"Core",
        "assessments": [
                { "assessNum": 1,
                  "weight":30,
                  "assessType":"Presentation",
                  "description":"Prototype demonstration" },
                { "assignNum": 2,
                  "weight":70,
                  "assessType":"Implementation and Presentation",
                  "description":"Final product Presentation and assessment of product implementation by panel of project supervisors" }
            ]
  }
}
)
db.Subject.insert(
{ 
    "_id":ObjectId(),
    "subject":{ 
        "subCode":"CSCI203",
        "subTitle":"Algorithm and Data Structures",
        "credit":3,
        "type":"Core",
        "prerequisite": ["csci103"]
}})

one of the few codes that I tried using :

db.Subject.aggregate({$group:{"prerequisite":{"$exists": null}, count:{$sum:1}}});

Results :

_getErrorWithCode@src/mongo/shell/utils.js:25:13
doassert@src/mongo/shell/assert.js:18:14
_assertCommandWorked@src/mongo/shell/assert.js:534:17
assert.commandWorked@src/mongo/shell/assert.js:618:16
DB.prototype._runAggregate@src/mongo/shell/db.js:260:9
DBCollection.prototype.aggregate@src/mongo/shell/collection.js:1062:12
@(shell):1:1
varman

You can use $match to eliminate unwanted documents and $group to calculate sum

db.collection.aggregate([
  {
    $match: {
      "subject.prerequisite": {
        "$exists": false
      }
    }
  },
  {
    $group: {
      _id: null,
      total: {
        $sum: 1
      }
    }
  }
])

Working Mongo playground

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

How do I check if a number has a remainder?

分類Dev

How do I find out where a program has been installed?

分類Dev

How do I find the max in an Array using only math algorithms, specifically I am trying to knock out the lowest number each time

分類Dev

How do I find the number of different possible matrices?

分類Dev

How can I find out the number of times an RSK transaction has been confirmed on the RSK blockchain?

分類Dev

how do I display the total of the whole cart

分類Dev

How do i count number of referals using aggregation in mongoDB

分類Dev

How do I find which program is using port 80 in Windows?

分類Dev

In Jython, How do I print a sentence with a float, which has a percentage symbol without a space between the number and symbol?

分類Dev

How do I find out which random port Docker has chosen?

分類Dev

How can I recursively find all files and display the full path and line number using cmd?

分類Dev

How to find the maximum number using Generics?

分類Dev

How do I store total points after user input in Javascript?

分類Dev

How do I detect total loss of precision with Doubles?

分類Dev

How do I get the total size of everything in a directory in one line?

分類Dev

How do I count the sum of specified sum in $total

分類Dev

How do I know which delimiter has occurred first using awk in bash?

分類Dev

How do I change the font color using SCSS when it has an imported stylesheet in Angular 8?

分類Dev

How can I get the total of records using awk - Solaris

分類Dev

Websphere MQ- How to find the total number of messages that pass througha queue manager

分類Dev

How do I split strings into number and the remaining string using stringr in r?

分類Dev

How can I do this C++ question by using XOR operator for 2 or more digits number

分類Dev

How do I customize the colours in the bars using custom number set in matplotlib?

分類Dev

How do I replace the port number in JavaScript?

分類Dev

How do I specify a DCG for a valid number?

分類Dev

How can I do this, but with a changeable number of dice?

分類Dev

How do i select a number in a matrix in c?

分類Dev

How do I check if an ID is a number?

分類Dev

How do I find add-ons for packages when using the command line?

Related 関連記事

  1. 1

    How do I check if a number has a remainder?

  2. 2

    How do I find out where a program has been installed?

  3. 3

    How do I find the max in an Array using only math algorithms, specifically I am trying to knock out the lowest number each time

  4. 4

    How do I find the number of different possible matrices?

  5. 5

    How can I find out the number of times an RSK transaction has been confirmed on the RSK blockchain?

  6. 6

    how do I display the total of the whole cart

  7. 7

    How do i count number of referals using aggregation in mongoDB

  8. 8

    How do I find which program is using port 80 in Windows?

  9. 9

    In Jython, How do I print a sentence with a float, which has a percentage symbol without a space between the number and symbol?

  10. 10

    How do I find out which random port Docker has chosen?

  11. 11

    How can I recursively find all files and display the full path and line number using cmd?

  12. 12

    How to find the maximum number using Generics?

  13. 13

    How do I store total points after user input in Javascript?

  14. 14

    How do I detect total loss of precision with Doubles?

  15. 15

    How do I get the total size of everything in a directory in one line?

  16. 16

    How do I count the sum of specified sum in $total

  17. 17

    How do I know which delimiter has occurred first using awk in bash?

  18. 18

    How do I change the font color using SCSS when it has an imported stylesheet in Angular 8?

  19. 19

    How can I get the total of records using awk - Solaris

  20. 20

    Websphere MQ- How to find the total number of messages that pass througha queue manager

  21. 21

    How do I split strings into number and the remaining string using stringr in r?

  22. 22

    How can I do this C++ question by using XOR operator for 2 or more digits number

  23. 23

    How do I customize the colours in the bars using custom number set in matplotlib?

  24. 24

    How do I replace the port number in JavaScript?

  25. 25

    How do I specify a DCG for a valid number?

  26. 26

    How can I do this, but with a changeable number of dice?

  27. 27

    How do i select a number in a matrix in c?

  28. 28

    How do I check if an ID is a number?

  29. 29

    How do I find add-ons for packages when using the command line?

ホットタグ

アーカイブ