猫鼬独特的类型 - 不起作用

时间:2016-08-09 19:48:53

标签: node.js mongodb mongoose mongoose-schema database

为什么猫鼬独特的类型不起作用? 每次我在datebase中都有重复项。

   'use strict';
    const mongoose = require('mongoose');
    const Schema = mongoose.Schema;

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

    var matchSchema = new Schema({
        url: { type: String, required: true, unique: true },
        league: String,
        league_url: String,
        team_left: String,
        team_left_url: String,
        team_right: String,
        team_right_url: String,
        start: String
    });

    let Match = mongoose.model('Match', matchSchema);

    let test = new Match({
        url: '/match/Q7u11iDN/',
        league: '123',
        league_url: '123',
        team_left: '123',
        team_left_url: '123',
        team_right: '123',
        team_right_url: '123',
        start: '09.08.2016 22:00'
    });

    test.save((err, test, affected) => {
        mongoose.connection.close();
    });

    Match.find({}, (err, test) => {
        console.log(test);
        console.log(test[0].league);
    });

是的,我重新启动mongodb,并删除数据库。 P.S: '每次为特定连接/数据库编译模型时,都会通过ensureIndex定义索引。这意味着只有在应用的生命周期内才能确保索引一次。'

我该如何使用它?

0 个答案:

没有答案
相关问题