mongoose文档
博客专区 > cyper 的博客 > 博客详情
mongoose文档
cyper 发表于2年前
mongoose文档
  • 发表于 2年前
  • 阅读 87
  • 收藏 0
  • 点赞 0
  • 评论 0

标题:腾讯云 新注册用户域名抢购1元起>>>   

详见:http://mongoosejs.com/docs/guide.html

Schema 》Model 》Instance

安装
node install mongoose --save

简单的例子:

var mongoose = require('mongoose');
var Schema = mongoose.Schema;
//mongoose.connect('mongodb://localhost/test');
mongoose.connect('localhost', 'test');

var db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error:'));
db.on('open', function (callback) {
    console.log('yay!');

    // schema
    var kittySchema = new Schema({
        name: String
    });
    kittySchema.methods.speak = function () {
        var greeting = this.name ? 'My name is ' + this.name : "I don't have a name";
        console.log(greeting);
    };

    // model
    // 记住第一个参数+s就是表/collection的名字。
    var Kitten = mongoose.model('Kitten', kittySchema);

    // instance/property
    var xx = new Kitten({name: 'xxx'});
    console.log(xx.name);

    // call method
    var yy = new Kitten({name: 'yy'});
    yy.speak();

    // save
    yy.save(function (err, yy) {
        if (err) {
            console.log(err);
            return;
        }

        console.log(yy);
    });

    // find all
    Kitten.find(function (err, kittens) {
        if (err) {
            console.log(err);
            return;
        }

        console.log(kittens);
    });

    // find by (name starts with 'y')
    Kitten.find({name: /^y/}, function (err, kittens) {
        if (err) {
            console.log(err);
            return;
        }

        console.log(kittens);
    });

});

自己写得稍微复杂一点的例子:

var mongoose = require('mongoose');
var PlatformTask = require('./PlatformTask');
var Receiver = require('./Receiver');
var Message = require('./Message');
var BlueConfig = require('./BlueConfig');
var _ = require('lodash');


mongoose.connect('mongodb://localhost/bluelotus');
var db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error:'));
db.on('open', function () {
    console.log('yay!');


    PlatformTask.findOne({"taskStatus": "开放招募中"}, function (err, task) {
        var requiredSkills = task.get('taskSkills');


        // status = active, order by vip level desc.
        var messages = [];
        Receiver.find({active: true})
            .sort('-vip.freelevel')
            .exec(function (err, users) {
                if (err) return console.log(err);

                console.log('required skills:', requiredSkills);

                _.each(users, function (user) {
                    var skills = user.get('skill');
                    var skillNames = _.pluck(skills, 'name');
                    console.log("user skills:", skillNames);

                    var common = _.intersection(requiredSkills, skillNames);
                    console.log(common);

                    // match skills
                    if (common.length > 0) {
                        messages.push({
                            user_phone: user.telephone.length > 0 ? users.telephone[0]: '',
                            content: _.template("亲爱的<%=user%>, 去看看新任务吧:任务名称:<%=task%> ")({
                                user: user.get('nickname'),
                                task: task.get('taskName')
                            })
                        });
                    }

                    // max users number
                    if(messages.length === BlueConfig.max_users){
                        return false;
                    }
                });

                console.log('messages: ', messages);


                // insert messages
                Message.create(messages, function(err){
                    if(err) return console.log('save message error');

                    console.log("messages saved");
                });


            });
    });


});




我只能说mongodb太好用了。。。数据库和表不用事先存在。 只管用就行。



共有 人打赏支持
cyper
粉丝 56
博文 619
码字总数 143069
×
cyper
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
* 金额(元)
¥1 ¥5 ¥10 ¥20 其他金额
打赏人
留言
* 支付类型
微信扫码支付
打赏金额:
已支付成功
打赏金额: