MongoDB(Mongoose)使数组成为模式的一部分

时间:2016-04-27 21:12:03

标签: javascript node.js mongodb mongoose-schema nosql

我正在使用MongoDB(mongoose)和Nodejs创建聊天应用程序。如下所示,我有一个mongoose允许的架构,但 storeSchema 中的用户需要是一个字符串数组(用户名)。这是正确的方法吗?

"use strict";
var express = require('express');
var mongoose = require('mongoose');

var userSchema = new mongoose.Schema({
    user: String
});

var messageSchema = new mongoose.Schema({
    msg: String
});

var storeSchema = new mongoose.Schema({
    users: [userSchema], // needs to be an array of users
    channels: {
        general: {
            messages: [messageSchema]
        },
        videogames: {
            messages: [messageSchema]
        },
        programming: {
            messages: [messageSchema]
        },
        other: {
            messages: [messageSchema]
        }
    }
});

var User = mongoose.model('User', userSchema);
var Message = mongoose.model('Message', messageSchema);
var Storage = mongoose.model('Storage', storeSchema);

module.exports = {
    User: User,
    Message: Message,
    Storage: Storage
}

1 个答案:

答案 0 :(得分:0)

如果您希望users成为用户名字符串数组,请在storeSchema中将其定义为:

users: [String]