Skip to content

Instantly share code, notes, and snippets.

@leodagdag
Last active December 18, 2015 03:28
Show Gist options
  • Save leodagdag/5718003 to your computer and use it in GitHub Desktop.
Save leodagdag/5718003 to your computer and use it in GitHub Desktop.
test nodejs - mongodb
var MongoClient = require('mongodb').MongoClient;
// Some docs for insertion
var docs = [{
type: 'switch',
duration:0
},{
type: 'switch',
duration:0
},{
type: 'switch',
duration:0
},{
type: 'switch',
duration:0
},{
type: 'switch',
duration:0
},{
type: 'switch',
duration:1
},{
type: 'switch',
duration:1
},{
type: 'switch',
duration:2
},{
type: 'switch',
duration:5
}];
MongoClient.connect("mongodb://localhost:27017/exampleDb", function(err, db) {
// Create a collection
db.createCollection('test', function(err, collection) {
collection.drop(function(err, result){
// Insert the docs
collection.insert(docs, {safe:true}, function(err, result) {
collection.count(function(err, results) {
console.log("count",results);
collection.group(
{type:1, duration:1},
{},
{"count":0},
function(obj, prev){
prev.count++;
},
function(err, result){
console.log("group",result);
db.close();
}
);
});
});
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment