Search code examples
mongodbmongoskinco-monk

mongodb aggregation (co-monk) undefined function


Following code does not seem to work, I receive undefined function. Using co-monk which is based on mongoskin which is based on the mongodb native node module.

Document:

{
    "_id" : ObjectId("560c24b853b558856ef193a4"),
    "name" : "ирина",
    "pic" : "",
    "language" : ObjectId("560c24b853b558856ef193a2"),
    "cell" : 1,
    "local" : {
        "email" : "ирина@mail.com",
        "password" : "12345"
    },
    "sessions" : [
        {
            "id" : ObjectId("560c24b853b558856ef193a5")
        }
    ]
}

Query:

            var sessionSeen =
                yield users.aggregate([
                    {
                        $match: {
                            _id: myVarIdHere
                        }
                    },
                    {
                        $project: {
                            _id: 0,
                            data: {
                                $map: {
                                    input: '$sessions',
                                    as: 'sess',
                                    in : '$$sess.seen'
                                }
                            }
                        }
                    }
                ]);

seen is supposed to return null as it hasen't been set.


Solution

  • Monks documentation is retarded. An internet hero wrote that .col accesses the native mongodb driver, there I can do shit. I however have to wrap it in a promise again, no probs though.

    var sessionSeen =
                yield new Promise(function (resolve, reject) {
    
                    users.col.aggregate([
                            {
                                $match: {
                                    '_id': new ObjectID.createFromHexString(socket._id)
                                }
                            },
                            {
                                $project: {
                                    _id: 0,
                                    data: {
                                        $map: {
                                            input: '$sessions',
                                            as: 'sess',
                                            in : '$$sess.seen'
                                        }
                                    }
                                }
                            }
                        ],
                        function (err, res) {
                            console.log('err ' + err);
                            console.log('res ' + JSON.stringify(res));
                            if (err === null)
                                resolve(res);
                            reject(err);
                        });
                });