Mongoose doesn't create model object with array - node.js

I'm working on a project where I have the following mongoose schema:
module.exports = mongoose.model('Env', {
name : {type : String, default: ''},
services: [ServiceAddress]
});
module.exports = mongoose.model('ServiceAddress', {
serviceId : {type : ObjectId},
address: {type: String}
});
I'm trying to update an existing 'Env' document which has an empty array of 'services'.
router.put('/api/env', function(req, res, next) {
var env = new Environment(req.body);
env.save(function(err, updated){
if (err){
res.send(err);
}
res.json(updated);
});
});
The req.body content is:
{
_id: "56dd26a6618c1b983d5bada6",
name: "Test Drive"
services: [{_id: "56dc6e385fb0b038241d3399", address: "11"}, {_id: "56dc6e595fb0b038241d339a", address: ""}]
}
But the result in the DB is (only the IDs are pushed):
"services" : [ { "_id" : ObjectId("56dc6e385fb0b038241d3399") }, { "_id" : ObjectId("56dc6e595fb0b038241d339a") }
Any idea?
Thanks

Please try out with following snippet
var serviceSchema = {
serviceId : {type : ObjectId,required: false},
address: {type: String,required: false}
};
// added so that new _id field is not added when object is pushed in array
serviceSchema = 'new Schema('+serviceSchema +',{_id:false})';
module.exports = mongoose.model('Env', {
name : {type : String, default: ''},
services: [serviceSchema], default:[],
});
Thanks

Related

not able to save document with ref mongoose

I can't save my insert
MY MODELS :
Action and Type_intervention
var mongoose = require("mongoose"),
Schema = mongoose.Schema;
var actionSchema = new Schema({
action: {
type: String,
required: true,
unique: true
},
}); //Exporter le model
module.exports = mongoose.model('Action', actionSchema);
/*-----------------------------------------*/
var mongoose = require("mongoose"),
Schema = mongoose.Schema;
var type_interventionSchema = new Schema({
type_name_intervention : {type : String},
libelle : {type : String},
Standart : {type : String},
libelle_crt : {type : String},
action : {type: Schema.ObjectId, ref: 'Action'},
});
//Exporter le model
module.exports = mongoose.model('Type_intervention',type_interventionSchema);
/*--------------------------------------*/
MY CONTROLLER:
var new_type_intervention = new Type_intervention({
type_name_intervention: req.body.type_name_intervention,
libelle: req.body.libelle,
Standart: req.body.Standart,
libelle_crt: req.body.libelle_crt,
action: req.body.action,
})
new_type_intervention.save((err, newinter) => {
if (err) {
return res.send({
message: 'Error when try to save',
'intervention': new_type_intervention,
'req.action': req.body.action,
'new_type_intervention_action': new_type_intervention.action
});
}
return res.send({
message: 'Add with succes',
'intervention': new_type_intervention
})
})
POSTMAN RUN : The error is catched (new_intervention.action not apear and there type is undefined !? )
I think this is a probleme
{ "type_name_intervention":"f",
"libelle":"f",
"Standart":"f",
"libelle_crt":"f",
"action":"test"}
//Results:
{
"message": "Error when try to save",
"intervention": {
"type_name_intervention": "f",
"libelle": "f",
"Standart": "f",
"libelle_crt": "f",
"_id": "591eb2ccd4325d0e40b2d038"
},
"req.body.action": "test",
"type of new_type_intervention_action": "undefined"
}
I found the solution :
Before save : i try to find the action_id
Action.findOne({
'action': req.body.action,
})
.exec(function (err, found_action) {
if (found_action) {
new_type_intervention.action=found_action._id; // This is GOOD practice
new_type_intervention.save();
return res.send({
'type': typeof req.body.action,
message: 'type intervention ajoutee avec succes '
})
}
})

Deleting subdocuments using mongoose returning error?

I want to delete all the sub-documents of my collection.
mongoose schema :
//productSchema
var pdtSchema = new Schema({
"productId" : {type : String},
"product" : {type : String},
"item no" : {type : String},
});
var shopSchema = new Schema({
"providerId" : {type : String},
"provider" : {type : String},
"products" : [pdtSchema]
}, { collection:"shopdetails" });
module.exports.Shops = mongoose.model('Shops',shopSchema);
module.exports.Products = mongoose.model('Products',pdtSchema);
I have stored a bulk of data inside the collection and I need to delete all the products(that is the whole pdtSchema data).
code:
router.post('/delete',function (req,res) {
var providerId = req.body.providerId;
model.Shops.findById({"providerId" : providerId},function(err, doc) {
console.log(doc.products) // returns whole products here...
doc.products.remove();
doc.save(function(err,data){
res.json({"msg":"deleted"});
});
});
});
error:
(node:16351) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): ValidationError: CastError: Cast to ObjectID failed for value "[Function]" at path "_id"
Use the $unset operator which deletes the products field with the findOneAndUpdate() method. Using the traditional approach of first
retrieving the document with findById() only works with a valid ObjectId, in your case you are only providing a non-ObjectId string, hence the error.
router.post('/delete',function (req,res) {
var providerId = req.body.providerId;
model.Shops.findOneAndUpdate(
{ "providerId": providerId },
{ "$unset": { "products": "" } },
{ "new": true }
function(err, doc) {
console.log(doc) // returns modified doc here...
res.json({"msg": "Field deleted"});
}
);
});
If you want to keep the array field but remove all its elements, use $set as
router.post('/delete',function (req,res) {
var providerId = req.body.providerId;
model.Shops.findOneAndUpdate(
{ "providerId": providerId },
{ "$set": { "products": [] } },
{ "new": true }
function(err, doc) {
console.log(doc) // returns doc with empty products here...
res.json({"msg": "Products deleted"});
}
);
});
This is because you are saving "providerId" in shopSchema as type String, even though it is a mongoose object.
So, comparing a string type against a mognoose ObjectId type gives a cast error.
Instead do this,
var shopSchema = new Schema({
"providerId" : {
type : Schema.ObjectId
ref : schema which they are a reference to},
"provider" : {type : String},
"products" : [pdtSchema]
}, { collection:"shopdetails" });
But, I think if providerId refers to a shop Id, then it should be _id only.
model.findById() works with _id

Mongoose + Expressjs - E11000 duplicate key error index?

I don't understand why I got this error below after adding the first entry:
E11000 duplicate key error index: mydb.datasets.$id_1 dup key: { : null }
I don't have any null value in my first entry:
{
"index" : "9IPZMW7IL",
"name" : "Tweets",
"owner_name" : "xxx",
"read_key" : "fb6f9125f4ca15c33fea89416c3351d1",
"write_key" : "d8a6c7e5fc73b5a91aa7a533565ed1f1",
"data" : {
"var1" : {
"name" : "particles"
}
},
"_id" : ObjectId("57729dc20cb70952424cdbb4"),
"created_at" : ISODate("2016-06-28T15:54:42.576Z"),
"entries_number" : 0,
"public" : true,
"__v" : 0
}
Below is my code:
// CRUD API:
// POST/ Create new dataset request
router.post("/", helper.authenticate, function(req, res) {
// Used to set the dataset owner
var sessionUser = req.session.user.name;
// Get values from the post request
var name = req.body.name;
var isPublic = req.body.public != undefined ? true:false;
// Delete the values from the request body so that we only keep information about the variables
delete req.body.name;
delete req.body.public;
// This is so that we can loop through the object in reverse order
// We do that so that the fields are saved in the right order on the db
// (this way it will appear in the right order on the 'edit' view)
var propertiesList = [];
for (var property in req.body) {
if (req.body.hasOwnProperty(property)) {
propertiesList.push(property);
}
}
propertiesList.reverse();
var variablesFields = {};
for (var i in propertiesList) {
console.log(propertiesList[i])
variablesFields[propertiesList[i]] = {name:req.body[propertiesList[i]],
values: Array};
}
// Create dataset
Dataset.create({
index: helper.uniqueIndex(),
name: name,
owner_name: sessionUser,
read_key: hat(),
write_key: hat(),
public: isPublic,
data: variablesFields
}, function(err, dataset) {
if (err) {
console.log("Error creating the dataset: " + err);
req.session.error = "A problem occured when creating the dataset. Please try again.";
} else {
console.log("New dataset created with id: " + dataset._id);
req.session.success = "Dataset " + name + " created successfully.";
}
res.redirect("/index");
});
});
Error:
Error creating the dataset:
WriteError({"code":11000,"index":0,"errmsg":"E11000 duplicate key
error index: mydb.datasets.$id_1 dup key: { : null
}","op":{"index":"2IPZMWHGI","name":"PM
2","owner_name":"xxx","read_key":"fc31c152aa86070252c70c0304e4ca5c","write_key":"238110753c8762ce4a547fa02100a299","data":{"var1":{"name":"particles"}},"_id":"57729dcf0cb70952424cdbb5","created_at":"2016-06-28T15:54:55.459Z","entries_number":0,"public":true,"__v":0}})
Model:
var datasetSchema = new mongoose.Schema({
index: {type: String, required: true, index: {unique: true}},
name: {type: String, required: true},
owner_name: {type: String, required: true},
read_key: {type: String},
write_key: {type: String},
public: {type: Boolean, default: false},
data: {type: Object},
entries_number: {type: Number, default: 0},
created_at: {type: Date, default: Date.now},
last_entry_at: {type: Date}
});
Any idea why and how I can fix this?
I solved it by removing a "id" key that I initially declared as
id: { type: String, unique: true, required: true},
I removed this line and deleted the initial collection and that solved the issue.

mongoose populate not populating an array

I am facing an issue where mongoose query is not populating an array type.
Here is institute schema
'use strict';
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var InstituteSchema = new Schema({
name: String,
address: String,
city: String,
country: String,
zip: String,
owner: { type: mongoose.Schema.ObjectId, ref: 'User' },
teachers: [{type: mongoose.Schema.ObjectId, ref: 'Teacher'}],
categories: [String],
created : { type : Date, default : Date.now }
});
module.exports = mongoose.model('Institute', InstituteSchema);
And here is teacher Schema
'use strict';
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var TeacherSchema = new Schema({
education: [{degree: String, instituteName: String}],
dob: Date,
photoUrl: String,
phoneNumber: String,
owner: {type: mongoose.Schema.ObjectId, ref: 'User'},
institutes: [{type: mongoose.Schema.ObjectId, ref: 'Institute'}],
subjects: [{type: mongoose.Schema.ObjectId, ref: 'Subject'}],
created : { type : Date, default : Date.now }
})
module.exports = mongoose.model('Teacher', TeacherSchema);
Here is a method which queries the institute by owner id
exports.mine = function (req, res, next) {
var ObjectId = mongoose.Types.ObjectId;
var userId = new ObjectId(req.user._id);
Institute.find({
owner: userId
}).populate('teachers').exec(function (err, institute) {
if (err) return next(err);
if (!institute) return res.json(401);
res.json(institute);
});
};
I can see from the db that institute has teacher added
db.institutes.find();
{
"_id" : ObjectId("554719a9f5be11c6d4369264"),
"owner" : ObjectId("5547199bf5be11c6d4369263"),
"country" : "USA",
"name" : "Raghvendra Singh",
"address" : "38589 Royal Ann Cmn",
"city" : "Fremont",
"zip" : "94536",
"created" : ISODate("2015-05-04T07:03:05.569Z"),
"categories" : [ "IIT", "Medical" ],
"teachers" : [ ObjectId("55471965f5be11c6d436925f") ],
"__v" : 3
}
But somehow the query method doesn't populate the teachers collection. The weird thing is that i don't even get the collection with object ids and it returns and institute with empty teacher array.
And when i remove the .populate('teachers') from the method call it indeed returns the teacher array with object ids.
I looked at the documentation and i can't see what am i doing wrong.
First you need to change your Model slightly as mention for teachers feild.
teachers: [ { teacher: { type: Schema.ObjectId, ref: "Teacher" } } ]
exports.mine = function (req, res, next) {
var ObjectId = mongoose.Types.ObjectId;
var userId = new ObjectId(req.user._id);
Institute.find({
owner: userId
}).populate('**teachers.teacher**').exec(function (err, institute) {
if (err) return next(err);
if (!institute) return res.json(401);
res.json(institute);
});
};
Then, change your populate parameter to teachers.teacher . It will work

update object inside array that is inside collection

I need to update field "rightAnswer" in object inside array that is inside collection.
How can I change boolean of specific comment to true for right question.
In Angular I pass id of question and id of comment to backend. But on the backend I don't know how to update that field. I use MEAN stack.
Question schema:
var questionSchema = new mongoose.Schema({
title: {type : String, default : '', trim : true},
text: {type : String, default : '', trim : true},
publisher_id: {type : mongoose.Schema.ObjectId, ref : 'User'},
answers: [{
body: { type : String, default : '' },
user: { type : mongoose.Schema.ObjectId, ref : 'User' },
rightAnswer: { type : Boolean, default: false },
createdAt: { type : Date, default : Date.now }
}],
date : {type : Date, default : Date.now}
});
My route in express:
Question.update({_id: req.body.question._id}, {"answers._id": req.body.answer._id}, {$set: {"answers.$.rightAnswer": true} }, function(req, data){
res.json(data);
});
Solution:
Question.update({_id: req.body.question._id,
"answers": {$elemMatch: {_id: req.body.answer._id}}},
{$set: {"answers.$.rightAnswer": true}},
function(req, res){});
Question.update({_id: req.body.question._id,
"answers": {$elemMatch: {_id: req.body.answer._id}}},
{$set: {"answers.$.rightAnswer": true}},
function(req, res){});

Resources