Javascript MongoDB合并集合-不填充
尝试合并客户端和帐户集合 当我使用res.send(客户机)时,我只获得帐户id。我不知道如何填充字段以将帐户信息推送到客户端。我已经看到了一些一对多的解决方案,但无法解决这个双向问题 以下是我得到的:Javascript MongoDB合并集合-不填充,javascript,node.js,mongodb,express,Javascript,Node.js,Mongodb,Express,尝试合并客户端和帐户集合 当我使用res.send(客户机)时,我只获得帐户id。我不知道如何填充字段以将帐户信息推送到客户端。我已经看到了一些一对多的解决方案,但无法解决这个双向问题 以下是我得到的: {"accounts":["5fba8c2f8e5610374463caab","5fba8d49ae11d93c40ce08d9","5fba8dcfb4192a22d469eeef"],"_id&quo
{"accounts":["5fba8c2f8e5610374463caab","5fba8d49ae11d93c40ce08d9","5fba8dcfb4192a22d469eeef"],"_id":"5fba8bf1407032134c4c22b4","company":"Testing2","contact":"Testing2","phone":5555,"email":"Testing2","city":"Testing2","state":"Testing2","zip":5555555,"__v":3}
这是我正在使用的路线(基于一门似乎对他们很有效的课程(?):
以下是我正在使用的两种模型:
客户:
以及账户模式:
const mongoose = require('mongoose');
const { Schema } = mongoose;
const AccountSchema = new Schema({
name: String,
location: String,
client: [{
type: Schema.Types.ObjectId,
ref: 'Client'
}],
description: String,
contact: String,
tid: String,
loader: String,
surcharge: Number,
payout: Number,
net: Number,
})
module.exports = mongoose.model('Account', AccountSchema);
非常感谢您的帮助。提前谢谢
SonOfZappa如果您使用的是moongose,则可能就是您要寻找的产品。我认为这可能就是问题所在。我已经将填充视为一种解决方案,但我不确定在哪里实现它。模型里有吗?路由?等感谢您的回复。我将放置
const updatedClient=wait Client.findById(id)。填充('account')
在res.send(updatedClient)之前,但也许有更好的方法。
const mongoose = require('mongoose');
const { Schema } = mongoose;
const ClientSchema = new Schema({
company: String,
contact: String,
phone: Number,
email: String,
city: String,
state: String,
zip: Number,
accounts: [
{
type: Schema.Types.ObjectId,
ref: 'Account',
},
]
});
module.exports = mongoose.model('Client', ClientSchema);
const mongoose = require('mongoose');
const { Schema } = mongoose;
const AccountSchema = new Schema({
name: String,
location: String,
client: [{
type: Schema.Types.ObjectId,
ref: 'Client'
}],
description: String,
contact: String,
tid: String,
loader: String,
surcharge: Number,
payout: Number,
net: Number,
})
module.exports = mongoose.model('Account', AccountSchema);