我的主要问题是这个体系结构如何 - 如果用户在数据库中创建一个新列表并且有一个单独的集合用于存储列表,那么当他想访问一个他可以存储其联系人的特定列表时,我应该重定向他通过在数据库中为他分配一些集合,现在问题出现了,如果有特定用户的'n'列表通过根据他的需要分类到列表来保存他的联系人,那么我应该如何实现这个架构的任何建议?或者我到目前为止做了什么错误?任何更正?
我正在使用mongodb,nodejs with express framework
我到目前为止所做的是手动我能够创建一个新列表并为其分配一个集合,以便用户可以在该列表中添加他的联系人。 但是不可能为每个列表手动创建集合,因此我正在寻找一种可以根据需要自动创建新集合的方法。
是否有任何方法可以在mongodb中创建n个集合或n个子集合?
这是我在集合中添加联系人的js文件,我将这些联系人存储在两个单独的列表中,每个列表都有单独的集合。
var express = require('express');
var router = express.Router();
var $ = require("jquery");
var mongoose = require('mongoose');
/* GET New User page. in list 1 */
router.get('/mylist', function(req, res) {
res.render('mylist', { title: 'Go To My-List' });
});
router.post('/delcontact', function(req, res){
console.log('Using delcontact');
var db = req.db;
var collection = db.get('golists');
console.log('Got this : ' + JSON.stringify(req.body));
var delcontact = req.body;
var emails = Object.keys(delcontact).map(function(k) { return delcontact[k] });
for(var z=0; z<emails.length; z++){
//console.log('email: \n' + emails[z]);
collection.remove({email: emails[z]},
function(err, doc, z) {
if (err)
res.send('delete unsuccessfull');
else {
console.log('Selected contacts deleted');
}
});
res.send({redirect: '/userlist2'});
var collection = db.get('golists');
};
});
/* POST to Add User Service */
router.post('/addusers', function(req, res) {
// Set our internal DB variable
var db = req.db;
// Get our form values. These rely on the "name" attributes
var firstName = req.body.firstname;
var lastName = req.body.lastname
var userEmail = req.body.useremail;
// Set our collection
var collection = db.get('golists');
// Submit to the DB
collection.insert({
"firstname" : firstName,
"lastname" : lastName,
"email" : userEmail
}, function (err, doc) {
if (err) {
// If it failed, return error
res.send("There was a problem adding the information to the database.");
}
else {
// If it worked, set the header so the address bar doesn't still say /adduser
res.location("mailinglist");
// And forward to success page
res.redirect("mailinglist");
}
});
});
/* GET Userlist page. */
router.get('/mailinglist', function(req, res) {
var db = req.db;
var collection = db.get('golists');
collection.find({},{},function(e,docs){
res.render('userlist2', {
"userlist2" : docs
});
});
});
/* this is for list 2 */
router.get('/mylist2', function(req, res) {
res.render('mylist2', { title: 'Go To My-List' });
});
router.post('/addusers2', function(req,res){
//Set our internal DB variable
var db = req.db;
// Get our form values. These rely on the "name" attributes
var firstName = req.body.firstname;
var lastName = req.body.lastname
var userEmail = req.body.useremail;
// Set our collection
var collection = db.get('golists2');
// Submit to the DB
collection.insert({
"firstname" : firstName,
"lastname" : lastName,
"email" : userEmail
}, function (err, doc) {
if (err) {
// If it failed, return error
res.send("There was a problem adding the information to the database.");
}
else {
// If it worked, set the header so the address bar doesn't still say /adduser
res.location("mailinglist2");
// And forward to success page
res.redirect("mailinglist2");
}
});
});
/* GET Userlist page. */
router.get('/mailinglist2', function(req, res) {
var db = req.db;
var collection = db.get('golists2');
collection.find({},{},function(e,docs){
res.render('mailinglist2', {
"mailinglist2" : docs
});
});
});
router.post('/delcontact2', function(req, res){
console.log('Using delcontact');
var db = req.db;
var collection = db.get('golists2');
console.log('Got this : ' + JSON.stringify(req.body));
var delcontact = req.body;
var emails = Object.keys(delcontact).map(function(k) { return delcontact[k] });
for(var z=0; z<emails.length; z++){
//console.log('email: \n' + emails[z]);
collection.remove({email: emails[z]},
function(err, doc, z) {
if (err)
res.send('delete unsuccessfull');
else {
console.log('Selected contacts deleted');
}
});
res.send({redirect: '/mailinglist2'});
var collection = db.get('golists2');
};
});
这是我在某些集合中创建新列表的文件
router.get('/newlist',function(req,res){
res.render("newlist" ,{titile:'Add new list'}) ;
});
router.post('/addlist',function(req,res){
var db= req.db;
var listname=req.body.listname;
var collection=db.get("lists");
collection.insert({
"listname":listname
}, function (err,doc) {
if(err) {
res.send("There was a problem adding new list to database");
}
else {
res.location('lou');
res.redirect('lou');
}
});
});
router.get('/lou',function(req,res){
var db=req.db;
var collection=db.get("lists");
collection.find({},{},function(e,docs){
res.render('lou', {
"lou" : docs
});
});
});
router.get('/drop', function(req, res) {
res.render('drop', { title: 'Go To My-List' });
});
我的玉文件是
body
nav.navbar.navbar-default
.container-fluid
ul.nav.navbar-nav
li.active
a(href='/mailinglist')
| List 1
span.sr-only (current)
|
li
a(href='/mailinglist2')
| List 2
tbody
ul
p Import Contact From Your DB
a(href='/upload2') Go
table.table#stable
caption User Contact Details
|
thread
tr
th #
|
th FirstName
|
th LastName
|
th Email ID
|
each user, i in userlist2
tr(id=i)
td
input.contactID(type='checkbox')
td
| #{user.firstname}
td
| #{user.lastname}
td(id='email'+i)
| #{user.email}
button.btn.btn-danger#delete-button Delete Selected Contacts
答案 0 :(得分:0)
首先,这听起来不是一个好主意。如果所有用户的所有列表都有一个集合,那会好得多。拥有太多集合的问题是MongoDB没有提供任何工具来一次从多个集合中选择数据。因此,当您想要查询来自多个集合的数据时,您将不得不执行多个查询,这是非常不可靠的。
但是,当您决定坚持这种架构时,MongoDB会在您将数据插入到它不知道的集合名称时自动创建一个集合。