开发者

What's the most simple couple lines of code to connect to mongo and insert a post?

I'm using express.js and node.js.

What's t开发者_如何转开发he simplest, few lines of code that you use to connect to mongo and insert a post into the database?


var Db = require('mongodb').Db;
var db = new Db('node-mongo-examples', 
  new Server('localhost', '27017', {}), 
  {native_parser:true});

db.open(function(err, db) {
  db.collection('test', function(err, collection) {
    collection.insert({'hello':'world'})
  });
});


Here's a primitive save using mongoose.js:

var mongoose = require('mongoose'),
    Schema = mongoose.Schema;

mongoose.connect('mongodb://localhost/test');

mongoose.model('Post', new Schema({ title: String }));
var Post = mongoose.model('Post');

new Post({ title: 'Yeah' }).save();


var Mongolian = require("mongolian"),
    db = new Mongolian("mongo://user:pass@mongo.example.com:12345/dbname"),
    posts = db.collection("posts");

posts.insert({ title: "Hello", body: "Lorem ipsum..." });
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜