Aggregate request with Mongoskin

I want to run the following query using mongoskin, but it seems that the aggregate is not supported

mongodb request

db.users.aggregate({
        $match : {
            _id : ObjectId("52ee0844177c86dc1d000001")
        }
    }, {
        $unwind : "$todos"
    }, {
        $unwind : "$todos.tags"
    }, {
        $group : {
            _id : "$todos.tags",
            count : {
                $sum : 1
            }
        }
    });

javascript mongoskin code NOT WORKING

var tags = db.collection('users').aggregate({
        $match : {
            _id : db.bson_serializer.ObjectID.createFromHexString(req.user._id.toString())
        }
    }, {
        $unwind : "$todos"
    }, {
        $unwind : "$todos.tags"
    }, {
        $group : {
            _id : "$todos.tags",
            count : {
                $sum : 1
            }
        }
    });

How to use the unit with mongoskin?

EDIT

When I call console.log("%j", tags);, I get

TypeError: Converting circular structure to JSON
    at Object.stringify (native)
    at K:\home\projects\todo\routes\tags.js:22:20
    at callbacks (K:\home\projects\todo\node_modules\express\lib\router\index.js:164:37)
    at param (K:\home\projects\todo\node_modules\express\lib\router\index.js:138:11)
    at pass (K:\home\projects\todo\node_modules\express\lib\router\index.js:145:5)
    at Router._dispatch (K:\home\projects\todo\node_modules\express\lib\router\index.js:173:5)
    at Object.router (K:\home\projects\todo\node_modules\express\lib\router\index.js:33:10)
    at next (K:\home\projects\todo\node_modules\express\node_modules\connect\lib\proto.js:193:15)
    at Object.ensureAuthenticated [as handle] (K:\home\projects\todo\services\middleware.js:13:11)
    at next (K:\home\projects\todo\node_modules\express\node_modules\connect\lib\proto.js:193:15)

events.js:72
        throw er; // Unhandled 'error' event
              ^
TypeError: object is not a function
    at K:\home\projects\todo\node_modules\mongoskin\node_modules\mongodb\lib\mongodb\collection.js:1611:7
    at K:\home\projects\todo\node_modules\mongoskin\node_modules\mongodb\lib\mongodb\db.js:940:5
    at Cursor.nextObject (K:\home\projects\todo\node_modules\mongoskin\node_modules\mongodb\lib\mongodb\cursor.js:678:5)
    at commandHandler (K:\home\projects\todo\node_modules\mongoskin\node_modules\mongodb\lib\mongodb\cursor.js:658:14)
    at K:\home\projects\todo\node_modules\mongoskin\node_modules\mongodb\lib\mongodb\db.js:1670:9
    at Server.Base._callHandler (K:\home\projects\todo\node_modules\mongoskin\node_modules\mongodb\lib\mongodb\connection\base.js:382:41)
    at K:\home\projects\todo\node_modules\mongoskin\node_modules\mongodb\lib\mongodb\connection\server.js:472:18
    at MongoReply.parseBody (K:\home\projects\todo\node_modules\mongoskin\node_modules\mongodb\lib\mongodb\responses\mongo_reply.js:68:5)
    at null.<anonymous> (K:\home\projects\todo\node_modules\mongoskin\node_modules\mongodb\lib\mongodb\connection\server.js:430:20)
    at EventEmitter.emit (events.js:95:17)
+3
source share
1 answer

Check out json structure! Units close immediately after the match. Instead, the aggregated argument as an array:

db.collection('users').aggregate([{
    $match : {
      _id : db.bson_serializer.ObjectID.createFromHexString(req.user._id.toString())
    }
}]);

Draw up documentation.

0
source

All Articles