简体   繁体   English

mongodb node.js

[英]mongodb node.js

I have a problem with this collection name: 我对此集合名称有疑问:

module.exports = function() {

    var mongoose = require('mongoose');
    var db = mongoose.createConnection('localhost', 'race');
    db.on('error', console.error.bind(console, 'connection error:'));
    db.once('open', function () {});

    var collection = 'test';
    var Schema = mongoose.Schema;
    var ObjectId = Schema.ObjectId;

    var schema = new Schema({
        author: ObjectId,
        name: String,
        date: Date
    });

    this.model = db.model(collection, schema);

    var silence = new this.model({ name: 'Silence' })
    console.log(silence.name);
    silence.save();

    this.model.find(function (err, log) {
        console.log(err)
        console.log(log)
    })

    return this;
};  

I already have a test collection but console.log(log) just return Silence, 我已经有一个测试集合但是console.log(log)只返回Silence,

in fact Silence is register in "tests" collection and no "test" 事实上,沉默是在“测试”集合中注册而没有“测试”

can you explain me why, I set 我能解释一下为什么吗?

var collection = 'test';

Try this 尝试这个

var schema = new Schema({
        author: ObjectId,
        name: String,
        date: Date
    }, { collection: collection });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM