0
我有一個集合,它有一個引用另一個集合的objectId的屬性。Mongoose populate()返回對象數組而不是單個對象
placeSchema = mongoose.Schema({
name: String,
category: [{ type: Schema.Types.ObjectId, ref: 'categories' }],
facilities: [{ type: Schema.Types.ObjectId, ref: 'facilities' }],
});
的category
REF爲categories
集合的OBJECTID。 facilities
ref是facilities
集合的objectId的數組。
當我填充屬性,成爲facilities
和陣列facilities
objecrts的但category
也變得之一單category
陣列。
Place.find(query).populate('category').populate('facilities').limit(limit).skip(offset).exec(function(err, data){
if (err) return handleError(err);
res.json(data);
});
下面是上面查詢的例子:
{
"_id": "52ff59be70e8f0dfc8358cb4",
"address": {
"address_1": "Chambers Street",
"city": "Edinburgh",
"postcode": "EH1 1JF"
},
"description": "The National Museum of Scotland brings together our rich Scottish heritage with exciting international collections.",
"loc": {
"lon": -3.188384,
"lat": 55.94772
},
"name": "National Museum of Scotland",
"facilities": [
{
"_id": "53012d5b65b5e9a35efbb214",
"name": "Facility One"
},
{
"_id": "53012d6965b5e9a35efbb215",
"name": "Facility Two"
}
],
"category": [
{
"_id": "52ffbf33605d0c81f36d98e0",
"name": "Museums",
"slug": "museums"
}
]
}
這裏的文件,因爲它是在蒙戈:
{
"_id" : ObjectId("52ff59be70e8f0dfc8358cb4"),
"address" : {
"address_1" : "Chambers Street",
"city" : "Edinburgh",
"postcode" : "EH1 1JF"
},
"category" : ObjectId("52ffbf33605d0c81f36d98e0"),
"description" : "The National Museum of Scotland brings together our rich Scottish heritage with exciting international collections.",
"facilities" : [
ObjectId("53012d5b65b5e9a35efbb214"),
ObjectId("53012d6965b5e9a35efbb215")
],
"loc" : {
"lon" : -3.188384,
"lat" : 55.94772
},
"name" : "National Museum of Scotland"
}
如何填充我的單category
裁判,而不是一個數組?
我只是認爲它需要一個數組,我並沒有真正認爲這決定了結果。衛生署!謝謝安德烈亞斯! – iamjonesy