Mongoose:填充嵌套的ID数组
我一直在网上寻找解决方案,但似乎找不到问题的答案。 但由于某些原因,我的文档在我的案例中仍然没有填充。 以下是模式: 客户模式:Mongoose:填充嵌套的ID数组,mongoose,mongoose-schema,mongoose-populate,Mongoose,Mongoose Schema,Mongoose Populate,我一直在网上寻找解决方案,但似乎找不到问题的答案。 但由于某些原因,我的文档在我的案例中仍然没有填充。 以下是模式: 客户模式: var mongoose = require('mongoose'); var Schema = mongoose.Schema; var customerSchema = new Schema({ _id: {type: Schema.Types.ObjectId}, auditTrail: {type: Object}, thirdParty: [{
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var customerSchema = new Schema({
_id: {type: Schema.Types.ObjectId},
auditTrail: {type: Object},
thirdParty: [{type: Schema.Types.ObjectId, ref: 'Thirdparty'}],
docRefs: {type: Object},
salesRep: {type: Array},
commentsArr: {type: Array}
});
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var contactSchema = new Schema({
_id: {type: Schema.Types.ObjectId},
title: {type: String},
role: {type: String},
firstName: {type: String},
lastName: {type: String},
phone: {type: String},
mobile: {type: String},
email: {type: String}
});
module.exports = mongoose.model('Contact', contactSchema);
module.exports=mongoose.model('Customer',customerSchema)
第三方:
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var thirdPartySchema = new Schema({
_id: {type: Schema.Types.ObjectId},
type: {type: String},
name: {type: String},
vat: {type: String},
corpoPhone: {type: String},
corpoMail: {type: String},
corpoWeb: {type: String},
activityNumber: {type: String},
addresses: {type: Array},
contacts: [{type: Schema.Types.ObjectId, ref: 'Contact'}], // <-- ARRAY OF ObjectIds /!\
});
module.exports = mongoose.model('Thirdparty', thirdPartySchema);
Customer
.find()
.populate({
path: 'thirdParty',
populate: { path: 'contacts', model: 'Contact' }
})
.then(
//... Do something
)
这是我的电话:
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var thirdPartySchema = new Schema({
_id: {type: Schema.Types.ObjectId},
type: {type: String},
name: {type: String},
vat: {type: String},
corpoPhone: {type: String},
corpoMail: {type: String},
corpoWeb: {type: String},
activityNumber: {type: String},
addresses: {type: Array},
contacts: [{type: Schema.Types.ObjectId, ref: 'Contact'}], // <-- ARRAY OF ObjectIds /!\
});
module.exports = mongoose.model('Thirdparty', thirdPartySchema);
Customer
.find()
.populate({
path: 'thirdParty',
populate: { path: 'contacts', model: 'Contact' }
})
.then(
//... Do something
)
如果第三方在客户中得到完美的填充,联系人绝对不会。。。
下面是“响应”的日志:
我做错了什么
提前谢谢 数组内部的数组不能直接工作。对于进一步的操作和两级
$group
,您需要$unwind
但这是实现您的需求的方法
以下是输出:
{
"_id" : ObjectId("5d3f0a70d4a630e6a5499d3a"),
"auditTrail" : {},
"docRefs" : {},
"salesRep" : [],
"commentsArr" : [],
"thirdParty" : [{
"_id" : ObjectId("5d3fe4ddd4a630e6a5499d3b"),
"type" : "type2",
"name" : "type2",
"vat" : "type2",
"corpoPhone" : "type2",
"corpoMail" : "type2",
"corpoWeb" : "type2",
"activityNumber" : "type2",
"contacts" : [{
"_id" : ObjectId("5d3f09edd4a630e6a5499d38"),
"title" : "title1",
"role" : "role1",
"firstName" : "firstname1",
"lastName" : "lastname1",
"phone" : "phone1",
"mobile" : "mobile1",
"email" : "email1"
}, {
"_id" : ObjectId("5d3fe547d4a630e6a5499d3d"),
"title" : "title2",
"role" : "role2",
"firstName" : "firstname2",
"lastName" : "lastname2",
"phone" : "phone2",
"mobile" : "mobile2",
"email" : "email2"
}]
}, {
"_id" : ObjectId("5d3f0a36d4a630e6a5499d39"),
"type" : "type1",
"name" : "type1",
"vat" : "type1",
"corpoPhone" : "type1",
"corpoMail" : "type1",
"corpoWeb" : "type1",
"activityNumber" : "type1",
"contacts" : [ {
"_id" : ObjectId("5d3f09edd4a630e6a5499d38"),
"title" : "title1",
"role" : "role1",
"firstName" : "firstname1",
"lastName" : "lastname1",
"phone" : "phone1",
"mobile" : "mobile1",
"email" : "email1"
}]
}]
}
明白了!我缺少模型中的导入语句:
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var Contact = require('./contact'); // <--- THIS LINE RIGHT HERE
var thirdPartySchema = new Schema({
_id: {type: Schema.Types.ObjectId},
type: {type: String},
name: {type: String},
vat: {type: String},
corpoPhone: {type: String},
corpoMail: {type: String},
corpoWeb: {type: String},
activityNumber: {type: String},
addresses: {type: Array},
contacts: [{type: Schema.Types.ObjectId, ref: 'Contact'}],
});
module.exports = mongoose.model('Thirdparty', thirdPartySchema);
现在,我的“对象”已正确填充:
{
"message": "Customer successfully fetched",
"customers": [
{
"thirdParty": [
{
"addresses": [
{
"street": "AVENIDA ESTADOS UNIDOS, 141",
"streetcomp": "",
"streetcomp2": "",
"city": "SAN BARTOLOME DE TIRAJANA ",
"cp": "35290",
"state": "PALMAS (LAS)",
"country": "spain",
"main": true
},
{
"street": "OTRA DIRECCION DUMMY",
"streetcomp": "",
"streetcomp2": "",
"city": "MADRID",
"state": "MADRID",
"country": "spain",
"main": false
}
],
"contacts": [
{
"_id": "5cf0f6f2a3e9cf847c5861af",
"title": "Mrs.",
"role": "CFO",
"firstName": "John",
"lastName": "Doe",
"phone": "912345654",
"mobile": "673369900",
"thirdParty_id": "5cf0f6d0a3e9cf847c5861aa",
"addresses": [
{
"street": "AVENIDA ESTADOS UNIDOS , 141",
"streetcomp1": "TUNTE",
"streetcomp2": "",
"cp": "35290",
"city": "SAN BARTOLOME DE TIRAJANA ",
"state": "PALMAS (LAS)"
}
],
"email": "jdoe@ketchup.com",
"auditTrail": {
"creation": {
"user_id": "1",
"creationDate": "1559213796974"
},
"modification": [
{
"user_id": "1",
"modifDate": "1559213833358"
}
]
}
}
],
"_id": "5cf0f6d0a3e9cf847c5861aa",
"type": "customer",
"name": "ketchup",
"vat": "B87451084",
"corpoPhone": "918388544",
"corpoMail": "obras@ferrometal.net",
"corpoWeb": "http://ferrometalcimentaciones.es/",
"activityNumber": "5630"
}
],
"salesRep": [
{
"user_id": "USER_ID to be defined",
"dateBegin": "1559212324146",
"active": true
},
{
"user_id": "USER_ID to be defined",
"dateBegin": "1559212324146",
"active": false
}
],
"commentsArr": [
{
"user_id": "USER_ID to be changed to name",
"comment": "this is a great customer!"
},
{
"user_id": "USER_ID to be changed to name",
"comment": "This is a test"
}
],
"_id": "5cf0f704a3e9cf847c5861b2",
"auditTrail": {
"modifications": [
{
"modificationDate": "1559211664284",
"user_id": "A123"
}
],
"creation": {
"creationDate": "1559211664284",
"user_id": "A123"
}
},
"docRefs": {
"fs": [
{
"name": "mod 200 2018",
"comment": "should be approved",
"url": "www.google.com",
"uploadDate": "1559212324146",
"originalName": "mod200-2018.pdf",
"mime": "pdf",
"type": "fs",
"typeName": "Financial Statements"
}
],
"id": [
{
"name": "Jose-Pedro",
"comment": "ID Valido",
"url": "/somehwere/else",
"uploadDate": "1559212324146",
"originalName": "id-jp.pdf",
"mime": "pdf",
"type": "id",
"typeName": "Identification Document"
}
],
"ad": [
],
"cd": [
],
"pd": [
],
"od": [
]
},
"active": true
},
{
"thirdParty": [
{
"addresses": [
{
"street": "CALLE MORGAN , 2 - BJ 2 B",
"streetcomp": "",
"streetcomp2": "",
"city": "BILBAO",
"cp": "48014",
"state": "BIZKAIA",
"country": "spain",
"main": true
}
],
"contacts": [
{
"_id": "5cf0f6f2a3e9cf847c5861af",
"title": "Mrs.",
"role": "CFO",
"firstName": "John",
"lastName": "Doe",
"phone": "912345654",
"mobile": "673369900",
"thirdParty_id": "5cf0f6d0a3e9cf847c5861aa",
"addresses": [
{
"street": "AVENIDA ESTADOS UNIDOS , 141",
"streetcomp1": "TUNTE",
"streetcomp2": "",
"cp": "35290",
"city": "SAN BARTOLOME DE TIRAJANA ",
"state": "PALMAS (LAS)"
}
],
"email": "jdoe@ketchup.com",
"auditTrail": {
"creation": {
"user_id": "1",
"creationDate": "1559213796974"
},
"modification": [
{
"user_id": "1",
"modifDate": "1559213833358"
}
]
}
}
],
"_id": "5cf629538c2d290f39a9b18b",
"type": "customer",
"name": "ginegorama",
"vat": "B95551776",
"activityNumber": "8690"
}
],
"salesRep": [
{
"user_id": "1",
"dateBegin": "1559212324146",
"active": true
},
{
"user_id": "2",
"dateBegin": "1559212324146",
"active": false
}
],
"commentsArr": [
{
"user_id": "USER_ID to be changed to name",
"comment": "this is a great customer!"
}
],
"_id": "5cf6296a8c2d290f39a9b18c",
"auditTrail": {
"modifications": [
{
"modificationDate": "1559211664284",
"user_id": "1"
}
],
"creation": {
"creationDate": "1559211664284",
"user_id": "1"
}
},
"docRefs": {
"fs": [
{
"name": "mod 200 2018",
"comment": "should be approved",
"url": "/somewhere",
"uploadDate": "1559212324146",
"originalName": "mod200-2018.pdf",
"mime": "pdf",
"type": "fs",
"typeName": "Financial Statements"
}
],
"id": [
],
"ad": [
],
"cd": [
],
"pd": [
],
"od": [
]
},
"active": false
}
]
}
哈迪克,谢谢你的回答。但是,尽管Mongoose中有一个聚合函数,但我感觉您使用的语法直接来自MongoDB的API,而不是Mongoose,对吗?对,您可以在使用Mongoose聚合函数获取记录时使用此聚合。好的。。。让我试试。正如您所说,它似乎有点“冗长”,Mongoose的文档似乎暗示有一个解决方案……您的解决方案没有错误,但我得到了一个完全空的文档数组:
{“message”:“Customer successfully fetched”,“customers”:[]}
用Mongoose获得了它!我将发布解决方案。无论如何,谢谢!:)