Giter VIP home page Giter VIP logo

Comments (1)

pbatey avatar pbatey commented on July 20, 2024

My initial response was this:

Looks like the $size array query operator will do what you need. In query-to-mongo it works with a colon "things:size=3".

But after some thought, I decided that Wolciech was looking for an aggregation. Here's how I might do it:

var express = require('express')
var mongoskin = require('mongoskin')
var ObjectID = require('mongodb').ObjectID
var query2m = require('query-to-mongo')

// attach db to the router
var db = mongoskin.db('mongodb://localhost:27017/mydb', {safe: true})
var router = express.Router()
router.db = db
router.use(function (req, res, next) {
    req.db = router.db
    next()
})

// query objects in the collection
router.get('/my/query', function (req, res, next) {
    var query = query2m(req.query, { ignore: 'envelope' })
    var collection = req.db.collection('my')

    collection.count(query.criteria, function (e, count) {
        var links
        if (e) return next(e)
        res.append('X-Total-Count', count)
        var fullUrl = req.protocol + '://' + req.get('host') + req.originalUrl
        links = query.links(fullUrl, count)
        if (links) res.links(links)
        req.collection.aggregate([
            { $match: query.criteria },
            { $limit: query.options.limit },
            { $skip: query.options.skip },
            { $sort: query.options.sort },
            { $project: { item: 1, count: { $size:"$field" } }
        ]).toArray(function (e, results) {
            if (e) return next(e)
            res.locals.json = results
            next()
        })
    })
})

// attach the route to the app and serve
var app = express()
app.use('/api/formdata', router)
var server = app.listen(3000, function () {
    console.log('Listening on Port', server.address().port)
})

Note: I haven't run this code - let me know if it works or for you.

from query-to-mongo.

Related Issues (16)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.