JavaScript mongodb to return object from facet mongodb return object instead of array,mongodb return object,mongodb

Is it possible to have facet to return as an object instead of an array? It seems a bit counter intuitive to need to access result[0].total instead of just result.total

code (using mongoose):

        Model
            .aggregate()
            .match({
                "name": { "$regex": name },
                "user_id": ObjectId(req.session.user.id),
                "_id": { "$nin": except }
            })
            .facet({
                "results": [
                    { "$skip": start },
                    { "$limit": finish },
                    {
                        "$project": {
                            "map_levels": 0,
                            "template": 0
                        }
                    }
                ],
                "total": [
                    { "$count": "total" },
                ]
            })
            .exec() 
Answer:1

Each field you get using $facet represents separate aggregation pipeline and that's why you always get an array. You can use $addFields to overwrite existing total with single element. To get that first item you can use $arrayElemAt

Model
    .aggregate()
    .match({
        "name": { "$regex": name },
        "user_id": ObjectId(req.session.user.id),
        "_id": { "$nin": except }
    })
    .facet({
        "results": [
            { "$skip": start },
            { "$limit": finish },
            {
                "$project": {
                    "map_levels": 0,
                    "template": 0
                }
            }
        ],
        "total": [
            { "$count": "total" },
        ]
    })
    .addFields({
        "total": {
            $arrayElemAt: [ "$total", 0 ]
        }
    })
    .exec()
Answer:2

So i have a object like this: let obj = {'0': {x: 25, y: 12}, '1': {x:55, y:6} ,...} Now How can I push an other object like {x: 12, y: 15} in to this object?

So i have a object like this: let obj = {'0': {x: 25, y: 12}, '1': {x:55, y:6} ,...} Now How can I push an other object like {x: 12, y: 15} in to this object?

  1. push object into array
  2. push object into object
  3. push object into object javascript
  4. push object into array php
  5. push object into another object javascript
  6. push object into array typescript
  7. push object into array react
  8. push object into array javascript with key
  9. push object into array angular 6
  10. push object into array python
  11. push object into array javascript es6
  12. push object into array angular
  13. push object into array java
  14. push object into array mongodb
  15. push object into object php
  16. push object into array in javascript
  17. push object into json array
  18. push object into array mongoose
  19. push object into array at index
  20. push object into array es6

I have a small bug in my small coding playground and I just can't find any solution to my error after hours of struggling. I simply want to create an object of a Post class in a app.js file (required ...

I have a small bug in my small coding playground and I just can't find any solution to my error after hours of struggling. I simply want to create an object of a Post class in a app.js file (required ...

  1. node error class is not a constructor
  2. node js error class
  3. node custom error class
  4. node extend error class
  5. node js custom error class
  6. node js extend error class

I've a problem with my App, normally , ng-class displays a different background according to the time indicated in the database. It works fine on other views ( Home, and when creating ), but in the ...

I've a problem with my App, normally , ng-class displays a different background according to the time indicated in the database. It works fine on other views ( Home, and when creating ), but in the ...

  1. angularjs choose class

Let's say I have this string: <i id="1"></i><i id="2"></i><i id="3"></i> and this object: { "1": "Red", "2": "Green", "3": "Blue" } Then I want to obtain ...

Let's say I have this string: <i id="1"></i><i id="2"></i><i id="3"></i> and this object: { "1": "Red", "2": "Green", "3": "Blue" } Then I want to obtain ...

  1. string replace with regex
  2. string replace with regex python
  3. string replace with empty
  4. string replace with regex java
  5. string replace with regex javascript
  6. string replace with dictionary python
  7. string replace with python
  8. string replace with regex c#
  9. string replace with regex js
  10. string replace with empty char java
  11. string replace with empty char c#
  12. string replace with variable python
  13. string replace with multiple values
  14. string replace with space
  15. string replace with index in java
  16. string replace with java
  17. string replace with index
  18. string replace with variable javascript
  19. string replace with null java
  20. string replace with space java