Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Web Technology by (120 points)
recategorized by

this is my code to update the doc

UserAddress.findOneAndUpdate({ "_id": _userAddress._id, "address._id": address._id }, {
                    "$set": {
                        "address.$": {
                            "name": address.name,
                            "mobileNo": address.mobileNo,
                            "pincode": address.pincode,
                            "landmark": address.landmark,
                            "address": address.address,
                            "area": address.area,
                            "state": address.state,
                            "alternatePhoneNo": address.alternatePhoneNo,
                            "addressType": address.addressType
                        }
                    }
                })
                .exec((error, data) => {
                    if(error) return res.status(400).json({ error })
                    if(data) return res.status(201).json({ data })
                    console.log(error)
                    console.log(data)
                })
//Here is my Schema
const addressSchema = mongoose.Schema({
    name: {
        type: String,
        required: true,
        trim: true,
        min: 2,
        max: 40
    },
    mobileNo: {
        type: String,
        required: true,
        trim: true 
    },
    pincode: {
        type: String,
        required: true,
        trim: true
    },
    landmark: {
        type: String,
        required: true,
        trim: true
    },
    address: {
        type: String,
        required: true,
        trim: true
    },
    area: {
        type: String,
        required: true,
        trim: true
    },
    state: {
        type: String,
        required: true,
        trim: true
    },
    alternatePhoneNo: {
        type: String
    },
    addressType: {
        type: String,
        required: true,
        enum: [ 'home', 'work' ]
    }
})
const userAddressSchema = mongoose.Schema({
    user: {
        type: mongoose.Schema.Types.ObjectId,
        required: true,
        ref: 'User'
    },
    address: [addressSchema]
}, { timestamps: true })
Please log in to answer this question.

Related questions

0 votes
1 answer
0 votes
2 answers
asked Oct 16, 2019 in Web Technology by Sammy (47.6k points)
0 votes
2 answers
0 votes
2 answers

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...