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"
}
]
_idfield as a ID field for every document and generates it under the hood, you should use it instead of definedidfield.