0
試圖找出如何拉斷此查詢MongoDB中訪問不同的屬性:)查詢MongoDB中的一個集合;但需要對架構對象
CategorySchema
const CategorySchema = new Schema({
// Declare a unique name for each category.
name: {
required: true,
type: String,
unique: true
},
// This is the object that contains the 3 responsive image collections.
photos: {
mobile: [],
tablet: [],
desktop: []
}
});
addPhoto(
/**
* addPhoto(arg, arg2, arg3)
* - @param {String} name
* - @param {Object} image
* - @param {String} type
* - @return {Promise}
* - NOTE: Schema.statics allows for creating methods
* - on the Schema class.
*/
CategorySchema.statics.addPhoto = async function (name, image, type) {
// Find category 'name' & push the imageUrl to the key 'photos' array.
const list = await this.findOneAndUpdate({ name }, { $push: { photos: image.url } });
// Return the updated category.
return list;
};
最初addPhoto()
在將photos
更改爲包含3個數組的對象之前工作良好。現在我有addPhoto
接受第三個參數type
,它將成爲photos
對象中3個鍵之一。
問題我遇到了如何拉這個查詢。我按名稱查詢集合,因爲這對每個集合都是唯一的。 photos
存在於每個集合中,所以我無法查詢;但最終需要能夠訪問photos[type]
來執行$push
。關於我如何能夠實現這一點的任何想法?我一直在玩幾個不同的查詢操作符,但無濟於事。