Skip to content

Instantly share code, notes, and snippets.

@brianyang
Forked from lyhcode/mongodb.js
Created March 23, 2012 11:06
Show Gist options
  • Save brianyang/2169634 to your computer and use it in GitHub Desktop.
Save brianyang/2169634 to your computer and use it in GitHub Desktop.
MongoDB + Node.js samples
var mongodb = require('mongodb');
var server = new mongodb.Server("127.0.0.1", 27017, {});
new mongodb.Db('test', server, {}).open(function (error, client) {
if (error) throw error;
var collection = new mongodb.Collection(client, 'test_collection');
collection.find({}, {limit:10}).toArray(function(err, docs) {
console.dir(docs);
});
});
var Comments = new Schema({
title : String
, body : String
, date : Date
});
var BlogPost = new Schema({
author : ObjectId
, title : String
, body : String
, buf : Buffer
, date : Date
, comments : [Comments]
, meta : {
votes : Number
, favs : Number
}
});
var Post = mongoose.model('BlogPost', BlogPost);
var mongo = require('mongoskin');
mongo.db('localhost:27017/testdb').collection('blog').find().toArray(function(err, items){
console.dir(items);
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment