简体   繁体   English

更新嵌套数组并在此之前用猫鼬填充

[英]Update nested array and populate before that with mongoose

I would like to add a photo into a country in mongoose. 我想在猫鼬里添加一张照片到一个国家。 But country is an array and photo too. 但是国家也是一张照片。 Here is my user schema : 这是我的用户架构:

const UserSchema = new mongoose.Schema({
    name: {
        type: String,
        required: true
    },
    firstName: {
        type: String,
        required: true
    },
    email: {
        type: String,
        required: true,
        unique: true
    },
    password: {
        type: String,
        required: true
    },
    birthDate: {
        type: Date,
        required: true
    },
    sex: {
        type: String,
        required: true
    },
    countries: [
        {
            type: mongoose.Schema.Types.ObjectId,
            ref: 'Country',
            photos: [
                {
                    base64: {
                        type: String,
                        required: true       
                    },
                    title: String,
                    description: String
                }
            ]
        }
    ],
    admin: {
        type: Number,
        required: true
    }
});

Here is what I got as data into mongoDB : 这是我作为数据输入mongoDB的结果:

在此处输入图片说明

The problem is that I only got the id of countries. 问题是我只有国家/地区的ID。 And I would like to use another field of the document country. 我想使用文档国家/地区的另一个字段。 Populate works well when I want to get data, but how to populate and then use the fields to update with mongoDB? 当我想要获取数据时,Populate工作得很好,但是如何填充然后使用字段使用mongoDB更新?

Moreover, I don't know how to update data into nested array, I tried : 而且,我不知道如何将数据更新到嵌套数组中,我尝试过:

User.findOneAndUpdate(
    {
        "name": "CHARLAT",
        "countries": "5d2d847b06f2f94118a36518"
    },
    { $push : { "countries.photos" : {
        base64: "bla"
    } }}
)

As you can see, I use a hand written id for country... I could do a find query before on country but can we use populate here? 如您所见,我为国家/地区使用手写的ID ...我可以在国家/地区之前进行查找查询,但是我们可以在此处使用填充吗?

And I got this in Postman : 我在邮递员那里得到了这个:

在此处输入图片说明

Thank you in advance for your help ! 预先感谢您的帮助 !

If the type is ObjectId , it can't have a photos field, since it's just an _id . 如果类型为ObjectId ,则它不能具有photos字段,因为它只是一个_id It is a reference to another collection. 它是对另一个集合的引用。

Updated answer after comments : 评论后更新答案:

The best thing to do IMO is to create a Photo model which would have the file path and the country's _id. IMO最好的做法是创建一个Photo模型,该模型具有文件路径和国家/地区的_id。 The User model would only store a list of Photos [_id]. 用户模型将仅存储照片[_id]的列表。

UserSchema : UserSchema:

{
   .....
   photos : [{
            type: mongoose.Schema.Types.ObjectId,
            ref: 'Photo'
    }],
   .....
}

PhotoSchema : PhotoSchema:

{
    country : {
            type: mongoose.Schema.Types.ObjectId,
            ref: 'Country'
    },
    path : String
}

Then, query your Users this way, by populating the photos, and inside each photo, populating the countries : 然后,通过填充照片并在每张照片中填充国家/地区,以这种方式查询用户:

UserModel
        .find(conditions)
        .populate({ 
             path: 'photos',
             model: 'Photo'
             populate: {
                 path: 'country',
                 model: 'Country'
            } 
         })
         .lean() // Faster and lighter for read-only, simply returns an object

So you should get a User object like this : 因此,您应该获得一个User对象,如下所示:

{
    .....
    name : "John",
    photos : [{
         country : {
              name : "Country 1",
              code : "C1" // or whatever field you have in your Country model
         },
         path: "path/to/photo1.jpg"
    },
    {
         country : {
              name : "Country 2",
              code : "C2"
         },
         path: "path/to/photo2.jpg"
    }]
    .....
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM