1

I want to save my details as {userId:status} into list but I'm not able to able to achieve can you help where I'm doing wrong?

if(campResp){
    let userId = await req.body.userId
    let status = req.body.status
    let userResp = await User.findOne({'id':req.body.userId}).exec()
    var data = {}
    data[userId] = status

    if(userResp){
        campResp.views = {}
        let visiterResp = await Campaign.find({'views':data})
        if(visiterResp.length>0){
            res.status(400).json({'status':false,'message':'Already watched'})
        }else{
            let pushResp = await campResp.views.push(data)
            if(pushResp){
                await campResp.save(async(err,response)=>{
                    if(response){
                        res.status(200).json({'status':true,'message':'campaign finished'})
                    }
                })
            }
        }
    }

Schema is like, but still it's not working.

const mongoose = require('mongoose')

const campaignSchema = new mongoose.Schema({
    id: mongoose.Schema.Types.ObjectId,
    imageUrl:{
        required:true,
        type:String
    },
    link:{
        required:true,
        type:String
    },
    ownerId:{
        required:true,
        type:String
    },
    setting:{
        types:{
            type:String
        },
        bill:{
            type:String
        },
        dateTime:{
            type:String
        },
        expected_time:{
            type:String
        },
        expected_views:{
            type:String
        },
        expected_subs:{
            type:String
        },
        expected_likes:{
            type:String
        }
    },
    views:[{
        userId:Boolean
    }]
},{versionKey: false},{_id:false})

module.exports = mongoose.model('Campaign',campaignSchema)

and in response what it's saving how can i save into my db like userId:status. Ex "6789786722hgdvasnbad":true how to achieve the same.

"views": [
            {
                "_id": "609b60a82445e32e8060b0c2"
            },
            {
                "_id": "609b60a82445e32e8060b0c4"
            }
        ]
2
  • LIttle comment regarding database model, MongoDB uses _id field as a ID field for every document and generates it under the hood, you should use it instead of defined id field. Commented May 12, 2021 at 5:55
  • ohh, Thanks @Ayzrian, sure will remove my own id.. Commented May 12, 2021 at 5:58

1 Answer 1

1

You may want to use mongoose.Schema.Types.Mixed

views:[{
    type: mongoose.Schema.Types.Mixed
}]

An "anything goes" SchemaType. Mongoose will not do any casting on mixed paths. You can define a mixed path using Schema.Types.Mixed or by passing an empty object literal. The following are equivalent.

Sign up to request clarification or add additional context in comments.

2 Comments

Wow, @Someone Special it's working perfect can you little explain it please.. why mine schema didn't work?
Because what you are using is a dynamic key. I don't think mongoose allow that. You can however use mongoose.Schema.Types.Mixed which allows any data type.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.