【发布时间】:2019-10-04 16:26:08
【问题描述】:
我有一个特定的项目集合并执行交易。在我的架构中,我将 userId 与每个项目相关联。我希望能够将用户拥有的所有项目显示为列表。
在这里,我设法将每个项目的所有尺寸加起来,但我无法找出如何为每个用户获取总和的方法
{
id: Number,
x: Number,
y: Number,
xSize: String,
ySize: String,
imageSource: String,
user: { type: mongoose.Schema.ObjectId, ref: 'User' }
},
{ timestamps: true }
);
const UserSchema = new Schema(
{
id: Number,
name: String,
website: String,
},
{ timestamps: true }
);
Item.find({}, function (err, items) {
var itemMap = {};
items.forEach(function (item) {
itemMap[item._id] = item;
});
var countedNames = items.reduce(function (allNames, name) {
if (name.xSize in allNames) {
allNames[name.xSize]++;
}
else {
allNames[name.xSize] = 1;
}
return allNames;
}, {});
基本上我想得到一个列表,基本上说 {名称:“戴夫”,网站:“www.google.com,项目:[item1,item2]}
其中 item1 和 item2 与项目架构相关
【问题讨论】: