Express: Embed document in the existing document - node.js

I am developing an application in Express, Node and Mongo being the database. I have a collection users, and user can have mutiple registered-IDs. It like a one-to-many relationship. I m trying to embed a document in the user collection like this:
post(function (req, res, next) {
var pid=req.body.pid;
var sid=req.body.sid;
var rfname=req.body.rfname;
var des=req.body.des;
var brand=req.body.brand;
var model=req.body.model;
var serial=req.body.serial;
var location=req.body.location;
var arr={pid: 'pid', sid: 'sid', rfname: 'rfname' ,des: 'des', brand: 'brand', model: 'model' ,serial: 'serial', location: 'location'};
mongoose.model('User').findOne({'pemail': req.session.email}, function (err, user){
if(err){
} else {
user.registeredId = arr;
user.save(function(err){
if(err){
} else {
res.render('user/register', {'success': 'dfhlaksdhfh'});
}
})
}
});
}
My user schema is like this:
var mongoose = require('mongoose');
var userSchema = new mongoose.Schema({
email: String,
password: String,
fname: String,
lname: String,
plang: String,
slang: String,
country: String,
state: String,
city: String,
postalcode: String,
address1: String,
address2: String,
pemail: String,
semail: String,
age: String,
gender: String,
pphone: String,
sphone: String,
q1: String,
a1: String,
q2: String,
a2: String,
cfname: String,
clname: String,
cemail: String
});
mongoose.model('User', userSchema);
Guide me, what am i doing wrong, because it does not embed document in the existing document. Do I need to define that in schema, if so, then how?

In your schema definition, the field registeredId is not defined and by default through the strict option, Mongoose ensures that values passed to your model constructor that were not specified in our schema do not get saved to the db, hence it is not creating the modified document.
You can either explicitly define the field in your schema or set the strict option to false in your schema definition:
// set to false..
var userSchema = new Schema({..}, { strict: false });
and then implement one of the findAndModify() methods like findOneAndUpdate() to update your user document by pushing the new object to the new array field registeredId. So you could re-write your post function as:
post(function (req, res, next) {
var User = mongoose.model('User'),
pid=req.body.pid,
sid=req.body.sid,
rfname=req.body.rfname,
des=req.body.des,
brand=req.body.brand,
model=req.body.model,
serial=req.body.serial,
location=req.body.location,
arr = {
'pid': pid,
'sid': sid,
'rfname': rfname,
'des': des,
'brand': brand,
'model': model,
'serial': serial,
'location': location
},
condition = { 'pemail': req.session.email },
update = {
"$push": { 'registeredId': arr }
};
User.findOneAndUpdate(
condition,
update,
function (err, doc){
if(err){}
else {
// doc contains the modified document
res.render('user/register', {'success': 'dfhlaksdhfh'});
}
}
);
});

Related

Mongoose findByIdAndUpdate

I trying to edit and update a form using mongoose. The code seems fine to me, but it doesn't work. I have tried so many ways but the updated version is still the same, I uses a put route to send the form, when I output req.body.studentInfo to the console, it is correct, but the update remains the same. Please help
This is my schema
var mongoose = require("mongoose");
var uniqueValidator = require('mongoose-unique-validator');
var passportLocalMongoose = require("passport-local-mongoose");
var mongoose = require("mongoose");
var UserSchema = new mongoose.Schema({
studentInfo: {
first_name: String,
middle_name: String,
last_name: String,
street: String,
town: String,
city: String,
region: String,
country: String,
studentId: String,
day: Number,
month: String,
year: Number,
},
username: {type: String, required:true, unique:true},
passport: String
});
UserSchema.plugin(uniqueValidator);
UserSchema.plugin(passportLocalMongoose);
module.exports = mongoose.model("StudentInfo", UserSchema);
This is my App.js
app.put('/:id', function(req,res){
StudentInfo.findByIdAndUpdate(req.params.id, {$set: req.body.studentInfo}, function(err, updated){
console.log(req.params.id);
console.log(req.body.studentInfo);
if(err) {
console.log(err);
}
else {
res.redirect('/' + req.params.id);
}
});
});
The studentInfo is an object that contains the names of each variables in my form which I name was studentInfo[name of variable]. Please help
It should be specified that mongoose should return the updated document - by default it returns the original (this is also the behavior of mongodb). I think that if the code gets changed to this:
StudentInfo.findByIdAndUpdate(req.params.id, {$set: req.body.studentInfo}, { new: true }, function(err, updated){
...
});
you will receive the updated document in the callback.
As #Denny mentioned in his answer, mongoose will not return the updated document in the callback until you pass {new : true } option.
For Details and available options check findByIdAndUpdate Docs

Autoincrement with Mongoose

I'm trying to implement an autoicremental user_key field. Looking on this site I came across two questions relevant for my problem but I don't clearly understand what I should do. This is the main one
I have two Mongoose models, this is my ProductsCounterModel.js
var mongoose = require('mongoose'),
Schema = mongoose.Schema;
var Counter = new Schema({
_id: {type: String, required: true},
sequence_value: {type: Number, default: 0}
});
module.exports = mongoose.model('products_counter', Counter);
and this is the Mongoose model where I try to implement the auto-increment field:
var mongoose = require('mongoose'),
Schema = mongoose.Schema;
var products_counter = require('./ProductsCounterModel.js');
var HistoricalProduct = new Schema({
product_key: { type: Number },
class: { type: String },
brand: { type: String },
model: { type: String },
description: { type: String }
});
HistoricalProduct.pre("save", function (next) {
console.log("first console log:",products_counter);
var doc = this;
products_counter.findOneAndUpdate(
{ "_id": "product_key" },
{ "$inc": { "sequence_value": 1 } },
function(error, products_counter) {
if(error) return next(error);
console.log("second console log",products_counter);
doc.product_key = products_counter.sequence_value;
next();
});
});
module.exports = mongoose.model('HistoricalProduct', HistoricalProduct);
Following the steps provided in the above SO answer I created the collection products_counter and inserted one document.
The thing is that I'm getting this error when I try to insert a new product:
"TypeError: Cannot read property 'sequence_value' of null"
This are the outputs of the above console logs.
first console log output:
function model (doc, fields, skipId) {
if (!(this instanceof model))
return new model(doc, fields, skipId);
Model.call(this, doc, fields, skipId);
}
second console log:
Null
can you see what I'm doing wrong?
You can run following line in your middleware:
console.log(products_counter.collection.collectionName);
that line will print products_counters while you expect that your code will hit products_counter. According to the docs:
Mongoose by default produces a collection name by passing the model name to the utils.toCollectionName method. This method pluralizes the name. Set this option if you need a different name for your collection.
So you should either rename collection products_counter to products_counters or explicitly configure collection name in your schema definition:
var Counter = new Schema({
_id: {type: String, required: true},
sequence_value: {type: Number, default: 0}
}, { collection: "products_counter" });

Mongoose remove document with references

I have two Schemas, eventSchema and personSchema as shown below:
var mongoose = require('mongoose')
, Schema = mongoose.Schema
var eventSchema = Schema({
title : String,
location : String,
startDate : Date,
endDate : Date
});
var personSchema = Schema({
firstname: String,
lastname: String,
email: String,
dob: Date,
city: String,
eventsAttended: [{ type: Schema.Types.ObjectId, ref: 'Event' }]
});
var Event = mongoose.model('Event', eventSchema);
var Person = mongoose.model('Person', personSchema);
How can I remove all eventsAttended from a deleted Person?
For example, if I remove a Person then I expect that all events assigned to that Person will be removed.
This is my code:
Person.findOneAndRemove({_id: req.body._id}, (err, response) => {
// remove the events assigned to this person
})
With mongoose you can use pre and post middleware on your schemas:
personSchema.post('remove', removeLinkedDocuments);
Then in the removeLinkedDocuments callback, you can remove all linked documents:
function removeLinkedDocuments(doc) {
// doc will be the removed Person document
Event.remove({_id: { $in: doc.eventsAttended }})
}
Note the middleware is only called for the following methods (refer to the linked documentation for details):
count
find
findOne
findOneAndRemove
findOneAndUpdate
update
To remove the documents 'manually' in your callback, you might do
Person.findOneAndRemove({_id: req.body._id}, (err, response) => {
// note that if you have populated the Event documents to
// the person documents, you have to extract the id from the
// req.body.eventsAttended object
Event.remove({_id: { $in: req.body.eventsAttended }}, (err, res) => {
...
})
})

Using a value of an document as _id when importing this document to MongoDB using NodeJS

I'm trying to import an Object into Mongo. But when I try to use a value of the Object as _id it fails. The error i.e: "[CastError: Cast to ObjectID failed for value "11563195" at path "_id"]" and later "[Error: document must have an _id before saving]"
What am I doing wrong ?
// Read and import the CSV file.
csv.fromPath(filePath, {
objectMode: true,
headers: keys
})
.on('data', function (data) {
setTimeout(function(){
var Obj = new models[fileName](data);
Obj._id = Obj.SOME_ID;
Obj.save(function (err, importedObj) {
if (err) {
console.log(err);
} else {
console.log('Import: OK');
}
});
}, 500);
})
Here is the used Schema:
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var SomeSchema = new Schema(
{
SOME_ID: String,
FIELD01: String,
FIELD02: String,
FIELD03: String,
FIELD04: String,
FIELD05: String,
FIELD06: String,
FIELD07: String,
FIELD08: String,
FIELD09: String,
FIELD10: String,
FIELD11: String,
FIELD12: String,
FIELD13: String
},
{
collection: 'SomeCollection'
});
module.exports = mongoose.model('SomeCollection', SomeSchema);
Many thanks for your time and help.
By default mongoose is validating that the _id field is a MongoId. If you want to store something other than a MongoId in the _id field you will need to give the _id field a different type.
var SomeSchema = new Schema({
_id: { type: String, required: true }
}

Mongoose object id is null

I'm creating an object Registration (a Mongoose Schema) and I need to set its Id in User with this line: registrationId: registration._id});
However, the Id is still null, even though it's the callback function? When I check the database the Registration has and Id of course, but not in the callback. How can I set the Id of the Registration in User?
Edit2: changed to minimal example. This prints out two times null.
exports.create = function(req, res) {
Registration.create(req.body, function(err, registration) {
if(err) { return handleError(res, err); }
console.log(registration._id);
console.log(registration.id);
return res.json(201, registration);
});
};
Edit: this is the schema (I left out some fields that are not required):
'use strict';
var mongoose = require('mongoose'),
Schema = mongoose.Schema;
var RegistrationSchema = new Schema({
isReservation: Boolean,
//Id of the trip
tripId: String,
//socialMutuality
codeGerechtige: String,
socialMutualityNumberParent1: String,
socialMutualityNumberParent2: String,
//contact
userId: String,
firstnameContact: String,
lastnameContact: String,
emailContact: String,
streetContact: String,
streetNumberContact: String,
zipcodeContact: String,
busContact: String,
cityContact: String,
phoneContact: String,
gsmContact: String,
socialSecurityNumberContact: String,
//coordinats of person that pays
//child information
//emergency contacts
emergencyContacts: [{
firstName: String,
lastName: String,
phone: String
}],
extraInfo: String
});
module.exports = mongoose.model('Registration', RegistrationSchema);
Problem and solution: the problem was the client sending an attribute _id = null, and that's why MongoDB/Mongoose didn't update the id.
removing _id from req.body fixed my issue.
if(req.body._id === null) {
delete req.body._id;
}
There must be something else going on in your code which is effecting this. This example works as expected for me:
'use strict';
var mongoose = require('mongoose'),
Schema = mongoose.Schema;
mongoose.connect('mongodb://localhost/createid');
var RegistrationSchema = new Schema({
isReservation: Boolean,
//Id of the trip
tripId: String,
//socialMutuality
codeGerechtige: String,
socialMutualityNumberParent1: String,
socialMutualityNumberParent2: String,
//contact
userId: String,
firstnameContact: String,
lastnameContact: String,
emailContact: String,
streetContact: String,
streetNumberContact: String,
zipcodeContact: String,
busContact: String,
cityContact: String,
phoneContact: String,
gsmContact: String,
socialSecurityNumberContact: String,
//coordinats of person that pays
//child information
//emergency contacts
emergencyContacts: [{
firstName: String,
lastName: String,
phone: String
}],
extraInfo: String
});
var Registration = mongoose.model('Registration', RegistrationSchema);
var reg = {
userId: '1234',
tripId: '2345',
firstnameContact: 'Timothy',
lastnameContact: 'Strimple',
emailContact: 'tim#tstrimple.com'
};
Registration.create(reg, function(err, registration) {
if(err) { throw err; }
console.log(registration._id);
});
I get a valid id written out to the console.

Resources