用 mongoose 打包数据
Posted
技术标签:
【中文标题】用 mongoose 打包数据【英文标题】:Packing data with mongoose 【发布时间】:2015-11-16 04:43:27 【问题描述】:我有 3 个具有不同架构的文件。用户有很多笔记本,笔记本也有很多笔记。模式示例:
用户架构:
var mongoose = require('mongoose'),
Schema = mongoose.Schema;
var User = new Schema(
username: type: String, require: true, index: unique: true , trim: true,
password: type: String, require: true, select: true ,
age: type: Number, min: 0 ,
firstname: String,
secondname: String,
token: String,
role: String,
city: String,
rememberMe: Boolean
);
module.exports = mongoose.model('User', User);
笔记本架构:
var mongoose = require('mongoose'),
Schema = mongoose.Schema,
Note = require('./note'),
User = require('./user');
var NoteBook = new Schema(
creator: type:Schema.ObjectId, ref:"User",
name: String,
description: String
);
NoteBook.methods.getAllNotes = function(cb)
Note.find(notebook: this, function(err, noteList)
cb(noteList);
);
;
module.exports = mongoose.model('NoteBook', NoteBook);
NoteSchema:
var mongoose = require('mongoose'), Schema = mongoose.Schema;
var NoteSchema = new Schema(
notebook: type: Schema.Types.ObjectId, ref: 'NoteBook',
name: String,
description: String,
content: String
);
module.exports = mongoose.model('Note', NoteSchema);
我输入了请求 userId,我需要将所有这行数据打包成 json。具有笔记本的用户和带有注释的笔记本在一个 json 中。我尝试过这样的事情:
函数 getTree(req, res)
var data = [];
User.findOne(_id: req.body.userId, function(err, user)
NoteBook.find(creator: user._id, function(err, notebookList)
for (var idx in notebookList)
Note.find(notebok: notebookList[idx]._id, function(err, noteList)
var children = [];
for (var noteIdx in noteList)
children.push(
'text': noteList[idx].name,
'a_attr' :
'data-node-type': 'note',
'data-node-id': noteList[idx]._id,
,
);
data.push(
'text': notebookList[idx].name,
'a_attr' :
'data-node-type': 'notebook',
'data-node-id': notebookList[idx]._id,
,
'children': children
);
);
res.json( tree: data );
);
);
但它不起作用。
【问题讨论】:
你想达到什么目的? json 示例:["text": "first_notebook", "a_attr" : "data-node-type": "notebook", "data-node-id": 2, "孩子”:[“文本”:“first_note”,“a_attr”:“数据节点类型”:“笔记”,“数据节点ID”:5],“文本”:“ second_notebook”,“a_attr”:“data-node-type”:“notebook”,“data-node-id”:2,“children”:[“text”:“second_note”,“a_attr”: "data-node-type": "note", "data-node-id": 5]] 【参考方案1】:var async = require("async");
function getTree(req, res)
var data = [];
User.findOne(_id: req.body.userId, function(err, user)
NoteBook.find(creator: user._id, function(err, notebookList)
async.forEach(notebookList, function(notebook, callback)
Note.find(notebok: notebook._id, function(err, noteList)
var children = [];
for (var noteIdx in noteList)
children.push(
'text': noteList[idx].name,
'a_attr' :
'data-node-type': 'note',
'data-node-id': noteList[idx]._id,
,
);
data.push(
'text': notebookList[idx].name,
'a_attr' :
'data-node-type': 'notebook',
'data-node-id': notebook._id,
,
'children': children
);
);
, function(err)
res.json( tree: data );
);
);
);
【讨论】:
您已经使用了 for 循环,并且在其中您有异步调用。因此,在您从 DB 获得笔记之前可能会发生这种情况。它将执行 res.json( tree: data ); 这行中的回调是什么意思 async.forEach(notebookList, function(notebook, callback) ? 如果我们有空笔记本,那么在这段代码中他也不会在数据中以上是关于用 mongoose 打包数据的主要内容,如果未能解决你的问题,请参考以下文章