node with mongodb 查询的语法
发布于 12 年前 作者 sipgear 5049 次浏览 最后一次编辑是 8 年前
var express = require('express');
var MongoClient = require('mongodb').MongoClient;
var app = express();
app.use(express.bodyParser());

// Connect to the db
MongoClient.connect("mongodb://user:passwd[@ds033087](/user/ds033087).mongolab.com:33087/database", function(err, db) {
  if(!err) {
app.get('/data', function(req, res) {
 var collectdb = db.collection('gts');
   collectdb.find({}, {limit:10}).toArray(function(err, docs) {
       // console.dir(docs);

      var json = JSON.stringify(docs);
      res.send(json);
	});

});
app.listen(3000);
console.log('Listening notenizer on port 3000...');  
// end of db    

  }
});

collectdb.find({}, {limit:10}).toArray 可以查到10条记录,但看不明白其他的查询条件, 现在要查不同uniqueID最新一条的记录。

[{“uniqueID”:“7101216048”,“lat”:22.535061666666667,“lng”:113.47493,“statusCode”:“62144”,“speed”:“000”,“head”:“161”,“date”:“2011-02-24”,“time”:“03:29:02”,"_id":“51527d7af703af9125000004”},{“uniqueID”:“7101216048”,“lat”:22.535061666666667,“lng”:113.47493,“statusCode”:“62144”,“speed”:“000”,“head”:“161”,“date”:“2011-02-24”,“time”:“03:29:02”,"_id":“51527d9ef703af9125000005”},{“uniqueID”:“7201216048”,“lat”:22.535061666666667,“lng”:113.47493,“statusCode”:“62144”,“speed”:“000”,“head”:“161”,“date”:“2011-02-24”,“time”:“03:29:02”,"_id":“51527da9f703af9125000006”},{“uniqueID”:“7301216048”,“lat”:22.535061666666667,“lng”:113.47493,“statusCode”:“62144”,“speed”:“000”,“head”:“161”,“date”:“2011-02-24”,“time”:“03:29:02”,"_id":“51527daef703af9125000007”},{“uniqueID”:“7401216048”,“lat”:22.535061666666667,“lng”:113.47493,“statusCode”:“62144”,“speed”:“000”,“head”:“161”,“date”:“2011-02-24”,“time”:“03:29:02”,"_id":“51527db3f703af9125000008”},{“uniqueID”:“3101216048”,“lat”:22.535061666666667,“lng”:113.47493,“statusCode”:“62144”,“speed”:“000”,“head”:“161”,“date”:“2011-02-24”,“time”:“03:29:02”,"_id":“5152933c371ce22026000001”},{“uniqueID”:“7101216048”,“lat”:22.535061666666667,“lng”:113.47493,“statusCode”:“62144”,“speed”:“000”,“head”:“161”,“date”:“2011-02-24”,“time”:“03:29:02”,"_id":“51529351371ce22026000002”},{“uniqueID”:“c101216048”,“lat”:22.535061666666667,“lng”:113.47493,“statusCode”:“62144”,“speed”:“000”,“head”:“161”,“date”:“2011-02-24”,“time”:“03:29:02”,"_id":“515296472e4cec3d26000001”},{“uniqueID”:“7101216048”,“lat”:22.535061666666667,“lng”:113.47493,“statusCode”:“62144”,“speed”:“000”,“head”:“161”,“date”:“2011-02-24”,“time”:“03:29:02”,"_id":“5152966c2e4cec3d26000002”},{“uniqueID”:“7101216012”,“lat”:22.535061666666667,“lng”:113.47493,“statusCode”:“62144”,“speed”:“000”,“head”:“163”,“date”:“2011-02-26”,“time”:“05:29:02”,"_id":“5152f3dbb52ac79228000001”}]

1 回复

实际上就是SQL的groupby查询,google关键字:mongodb groupby stackoverflow,相信有你需要的答案。

回到顶部