-
-
Save brianyang/2169634 to your computer and use it in GitHub Desktop.
MongoDB + Node.js samples
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
}); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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