Skip to content

Instantly share code, notes, and snippets.

@focusj
Created June 21, 2014 04:08
Show Gist options
  • Save focusj/022485b82e8362c949ef to your computer and use it in GitHub Desktop.
Save focusj/022485b82e8362c949ef to your computer and use it in GitHub Desktop.
mongoose 官方文档学习
/**
* Created by focusj on 14-6-20.
*/
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
mongoose.connect('mongodb://10.0.1.39:30000/TestGeo');
var db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', function callback() {
console.log("mongodb connected");
});
var kittySchema = Schema({
name: String
});
//methods
kittySchema.methods.speak = function () {
var greeting = this.name
? "Meow name is " + this.name
: "I don't have a name";
console.log(greeting);
}
//statics
kittySchema.statics.findByName = function (name, callback) {
this.find({name: name}, callback);
}
//index 有量中级别:
//1.fields level
//2.schema level
//
//默认的是启动系统的时候创建索引, 创建索引的过程是非常消耗资源的,
//会造成数据库系统暂时的性能颠簸, 这边给出了两种关闭的方式.
//
//animalSchema.set('autoIndex', false);
//new Schema({..}, { autoIndex: false });
var personSchema = Schema({
id: {type: String, index: true},
name: String,
score: Number,
address: String
});
personSchema.index({score: 1, score: -1});
//Virtual
personSchema.virtual("toString").get(function(name) {
});
var Kitten = mongoose.model('Kitten', kittySchema);
//var fluffy = new Kitten({ name: 'Silence' });
//fluffy.save(function (err, fluffy) {
// if (err) return console.error(err);
// fluffy.speak();
//});
Kitten.findByName("Silence", function (err, data) {
if (!err)
console.log(data)
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment