Post data to Subdocument - Mongoose Express - node.js

After googling!...
Using Mongoose/Express to save data I have issues :
Cannot post data to subdocument or nested document
Here is my Mongoose model :
const Users = new Schema({
firstname : {
type : String
},
password : {type : String},
phone : {type : Number},
city : {type : String},
country : {type : String},
experience : [
{
title : {type : String},
company : {type : String},
description : {type : String},
date_bg : {type : Date},
date_end : {type : Date},
}
],
education : [
{
degree : {type : String},
school : {type : String},
description : {type : String},
date_bg : {type : Date},
date_end : {type : Date},
}
],
// ...
})
And here is post router :
const addUser = async (req, res) => {
// my data from fronted is like this :
// req.body.experience is like this (same as education):
// [{title: "title experince 1", company: "title experince", date_bg: "2021-02-11", date_end: "2021-02-02", description: "title experince"}
{title: "title experince 2", company: "title experince 2", date_bg: "2021-02-10", date_end: "2021-02-08", description: "tstset"}]
const user = new Users(req.body) // This cause cast error for the *experience* and *education*
try {
await user.save()
return res.status(200).json({message : "Signin up successfully, Your account is now active!"})
} catch (error) {
return res.json({error : "Something went wrong, please try again later!", message : error})
}
}
My question is :
How to post my data to subdocument? (My problem is in the experience and education model)

in the req.body.experience of request there is a 1 before the second experience so change the format of body like this:
[
{
title: "title experince 1",
company: "title experince",
date_bg: "2021-02-11",
date_end: "2021-02-02",
description: "title experince"
},
{
title: "title experince 2",
company: "title experince 2",
date_bg: "2021-02-10",
date_end: "2021-02-08",
description: "tstset"
}
]

This is my recieve data but actually cannot saved :
{
email: 'manana04#gmail.com',
password: '$2a$10$DSpWahC5QCDEWwr1VljOXeok3awr/vVzDRcGazQsevu3H1ZgNY.pm',
firstname: 'dkfsdgk',
lastname: 'kvhdvgdjk',
job_title: 'cbhckvk5',
description: 'sdfvbilfgad',
hourly_rate: '454',
city: 'sdkvhdfvik',
country: 'hsvhdo',
education: [],
experience: [
{
title: 'Title experincev v',
company: 'dfvbhdfighfi',
date_bg: '2021-02-04',
date_end: '2021-02-17',
description: 'sdfsfsdfs'
},
{
title: 'tititititititititititititit',
company: 'dfldhkfjshdkfhsdkfshk',
date_bg: '2021-02-04',
date_end: '2021-02-16',
description: 'sdfshfdslfsdhfhs'
}
]
}

Related

how to update specific subdocument in array with mongoose

postSchema
let postSchema = new mongoose.Schema({
data : Buffer,
contentType : String,
caption : String,
likes : [mongoose.ObjectId],
likesCount : {type :Number, default : 0},
comment :[{userId :mongoose.ObjectId, cmnt : String, reply :[{}]}],
commentCount : {type :Number, default : 0},
date : {type : Date, default: Date.now}
})
userSchema
let userSchema = new mongoose.Schema({
qr : {
data : Buffer,
contentType : String
},
profile_pic:{
data:Buffer,
contentType : String
},
first_name:String,
last_name:String,
email:{
type:String,
unique:true
},
mobile_number:Number,
DOB : Date,
gender:{
type : String,
enum : ["Male","Female","Other"]
},
password : {
type : String
},
address:{
city : String,
state : String
},
followers : {
type : [mongoose.ObjectId]
},
followersCount : {
type : Number,
default : 0
},
following : {
type : [mongoose.ObjectId]
},
followingCount : {
type : Number,
default : 0
},
//this is my post schema
posts : [postSchema],
postsCount :{type :Number, default : 0},
reset_password_token : {
OTP:Number,
is_varify_password_token : {
type:Boolean,
default : false
},
time : {
type : Date,
}
}
})
** 1) i want to find specific user
2) after finding user i want to find and update specific post (caption) in array of posts **
i have tried but not working
await USERMODEL.findOneAndUpdate({_id:req.user._id,posts:{$elemMatch:{_id: req.params.postId}}},{"posts.$.caption ":caption})mongodb document image
PLEASE HELP I AM NEW TO MONGODB
**by mistake I have pasted some code out of block please do not mind **
It looks like your post is mostly code; please add some more details

How to specify and update an object within array in MongoDB

I'm trying to update the values of an object placed inside the array.
The MongoDB schema is as follows:
const ServiceStatus = new Schema({
CustomerName : { type : String},
CustomerNumber : {type : String},
ServiceName : {type : String},
Details : {type: String},
Date : {type: String},
Status : {type : String},
Comments : {type : String},
ServiceId : {type: Schema.Types.ObjectId},
CustomerId : {type: Schema.Types.ObjectId},
ServiceProvider : {type: Schema.Types.ObjectId},
message : [{
sender : {type: String},
content : {type : String},
time : {type : String}
}]
})
and the snippet of the route is
router.put('/message/customer/:id',(req, res) => {
const id=req.params.id;
Status.findByIdAndUpdate({_id : id},
{
$push : {
"message.$.sender": "Customer" ,
"message.$.content": req.body,
"message.$.date" : new Date()
}
}
,{ useFindAndModify: false } )
.exec((err, status) => res.json(status))
})
When I tried running it on postman it doesn't get updated but I don't get any error. I want to know how I should change the router to make it work.
I think it should be like this one:
db.ServiceStatus.insertOne(
{
CustomerName: "the name",
message: [{ sender: "Customer", content: "some content", time: new Date() }]
}
);
db.ServiceStatus.updateOne(
{ CustomerName: "the name" },
{
$push: {
message: { sender: "Another customer", content: "more content", time: new Date() }
}
}
)
db.ServiceStatus.find()
{
"_id" : ObjectId("5fdc4f14d1d3de1f92780a0b"),
"CustomerName" : "the name",
"message" : [
{
"sender" : "Customer",
"content" : "some content",
"time" : ISODate("2020-12-18T07:41:24.166+0100")
},
{
"sender" : "Another customer",
"content" : "more content",
"time" : ISODate("2020-12-18T07:41:26.328+0100")
}
]
}

Setting default values to array of objects in mongoose

var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var patientSchema = new Schema({
resourceType : {type :String, default : 'Patient' },
id : {type : String, default : 'example'},
text : [{
status : {type : String, default : 'generated'},
div :{type : String, default :'<div> Something </div>'}
}],
active : {type : String, default : 'true'},
identifier : [{
use : {type : String, default : 'official'},
system : {type : String, default : 'urn:oid:1.2.36.146.595.217.0.1'},
assinger :[{
display : {type : String, default : 'Acme Healthcare'},
}]
}],
name: [{
use : {type : String, default : 'official'},
first_name : {type : String, default : ''},
second_name : {type : String, default : ''}
}],
gender :{type : String, default : ''},
birthDate :{type : String, default : ''},
telecom : [{
system : {type : String, default : ''},
value : {type : String, default : ''}
}],
address : [{
use : {type : String, default : 'official'},
text : {type : String, default : ''},
city : {type : String, default : ''},
district : {type : String, default : ''},
state : {type : String, default : ''},
postalcode :{type : String, default : ''}
}]
});
var patients = mongoose.model('Patients',patientSchema);
module.exports = patients;
This is my model class, i'm sending values through post-man tool,
The default values inside the array of fields eg.
text : [{
status : {type : String, default : 'generated'},
div :{type : String, default :'<div> Something </div>'}
}],
the status and div are not storing the default values
i need to store the values of status and div as default!
You could use a sub-scheme/document instead!
var patientTextSchema = new Schema({
status : {type : String, default : 'generated'},
div :{type : String, default :'<div> Something </div>'}
});
... ommited for clarity
var patientSchema = new Schema({
text: [patientTextSchema]
})
This way you you can do patient.text.push({}) for adding a default patientTextSchema, or patient.text.push({status: "another_status"}) for a (partially) filled scheme.
Source: http://mongoosejs.com/docs/subdocs.html
You can use the following way to create Array of Objects with a default in mongoose:
const organisationSchema = new mongoose.Schema({
name: {
type: String,
required: true
},
brands: {
type: [
{
type: mongoose.Schema.Types.ObjectId,
ref: 'brand'
}
],
default: []
},
users: {
type: [
{
type: mongoose.Schema.Types.ObjectId,
ref: 'user'
}
],
default: []
}
}, { timestamps: true });

Inserting products to subschema taking long time?

I have mongoose schema:
var productSchema = new Schema({
"productId" : {type : String},
"productName" : {type : String},
"mrp" : {type : String}
});
var shopSchema = new Schema({
"ShopId" : {type : Number},
"Name" : {type : String},
"contact" : {type : Number},
"products" : [productSchema]
},{collection:"shopProductDetails"});
module.exports.Shops = mongoose.model('Shops',shopSchema);
module.exports.Products = mongoose.model('Products',productSchema);
I inserted data into the shop successfully .Next I have to insert data into the sub schema that is productSchema
Insertion Data
{
"ShopId":"439",
"products": [{
"productId": "1234",
"productName": "non veg",
"mrp": "38",
}, {
"productId": "5678",
"productName": "veg",
"mrp": "38 "
},{...}]
}
Products Insertion Code:
model.Shops.findOne({"ShopId":439},function(err, doc){
if(doc == null){
res.json({'success':'0','result':{},'errorMessage':'Credentials not matched'});
}
else{
async.each(products,function(item){
var insertProducts={productId: item.productId, productName: item.productName, mrp: item.mrp};
doc.products.push(insertProducts)
});
doc.save(function (err, data) {
if (err) {
res.json({'errorMessage':err});
}
else {
res.json({'success':'1','result':{'message':'Products Inserted Successfully'},'errorMessage':'No'});
}
});
}
});
Here the products are inserted successfully, the problem is that it is taking a large time when there is a bulk data. I am using async here,i think its wrong.Can any one suggest a better approach
I would suggest you only having the reference id for the product in the shop. It will save you time when making the query (now you just have to insert an id instead of looking up for another document in the database).

Mongoose- How to use discriminators in subdocument

I want the output to be like this
var FeedSchema = new Schema({
timestamp : {type : String, required : true},
feed_type_code : {type : Number, required : true},
gender : {type : String, required : true},
feed_item : Article || Tip || etc.
}
Hence, I understand I have to use discriminators. I followed the following SO answer
Here is what I did:
var feed_type = {discriminatorKey : 'feed_item'};
var FeedSchema = new Schema({
timestamp : {type : String, required : true},
feed_type_code : {type : Number, required : true},
gender : {type : String, required : true}
}, feed_type);
var TipSchema = new Schema({
tip : {type : String, required : true},
subtip : {type : String, required : true},
display_count : {type : Number},
likes : {type : Number}
}, feed_type);
var ArticleSchema = new Schema({
title : {type : String, required : true},
english_title : {type : String, required : true},
image_url : {type : String, required : true},
article_text : {type : String, required : true},
english_article_text : {type : String},
author : {type : String},
english_author : {type : String}
}, feed_type);
Here is how I am saving the document:
var article = new Article({
title : req.body.title,
english_title : req.body.english_title,
image_url : req.body.image_url,
article_text : req.body.article_text,
english_article_text : req.body.english_article_text,
author : req.body.author,
english_author : req.body.english_author
});
var feed = new Feed({
gender : req.body.gender,
timestamp : moment().valueOf(),
feed_type_code : 9002,
feed_item : article
});
feed.save(function(err, doc){
if(err){
res.json({success : -1});
return;
}
res.json({success : 1, feed : doc});
});
I am not getting the article output for this:
{
"success": 1,
"feed": {
"__v": 0,
"gender": "female",
"timestamp": "1481460218959",
"feed_type_code": 9002,
"_id": "584d49faa6ff3a23bc868ab3"
}
}
I am new to Nodejs. I would appreciate if you can point out the error.
in the model file add these lines
var Feed = mongoose.model('Feed', FeedSchema, 'feed');
var Article = Feed.discriminator('Article', ArticleSchema);
var Tip = Feed.discriminator('Tip', TipSchema);
module.exports = {
Feed : Feed,
Article : Article,
Tip : Tip
}
This is how you save the document. You don't need to create a separate Feed object.
var article = new Article({
gender : req.body.gender,
timestamp : moment().valueOf(),
feed_type_code : 9002,
title : req.body.title,
english_title : req.body.english_title,
image_url : req.body.image_url,
article_text : req.body.article_text,
english_article_text : req.body.english_article_text,
author : req.body.author,
english_author : req.body.english_author
});
article.save(function(err, doc){
if(err){
console.log(err);
res.json({success : -1});
return;
}
res.json({success : 1, feed : doc});
});

Resources