Comments (1)
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)
- Support for $or function HOT 3
- Support comparison operators with a=b query format HOT 6
- Fields generated have field name = true of false but 1 or 0 required HOT 5
- query by default _id HOT 3
- Support existence check encoded into the value HOT 1
- Filtering "not contains" string HOT 2
- URL encoded multiple negations not working as expected HOT 1
- Commas in regex HOT 2
- There is not any `offset` on query to mongo callback HOT 2
- Sometimes 'Infinity' is returned in the converted result HOT 4
- $and, $or HOT 1
- search support HOT 1
- Getting defaultKeywords is not defined HOT 1
- Dates not working in New Zealand HOT 2
- Update npm HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from query-to-mongo.