Unable to insert populated document w/ mongoose? - node.js

I'm trying to populate a document field with referenced document with the right data and saving it to the database. However, I'm unable to insert a document correctly in the database, even though I had successfully saved the document.
**getting-started.js**
...
var personSchema = mongoose.Schema({
_id : Number,
name : String,
age : Number,
stories : [{ type: Schema.Types.ObjectId, ref: 'Story' }]
});
var storySchema = mongoose.Schema({
_creator : { type: Number, ref: 'Person' },
title : String,
fans : [{ type: Number, ref: 'Person' }]
});
var Story = mongoose.model('Story', storySchema);
var Person = mongoose.model('Person', personSchema);
var Johns = new Person({ _id: 15, name: 'Johns', age: 154});
Johns.save(function (err) {
if (err) {
console.log('Person Save error! %s', err);
}
var story1 = new Story({
title: "Once upon a timex??",
_creator: Johns._id // assign the _id from the person
});
story1.save(function (err) {
if (err) return handleError(err);
console.log('successful story save! %d', story1._creator);
});
});
Story
.find({ title: 'Once upon a timex######' })
.populate('_creator')
.exec(function (err, story) {
if (err) return handleError(err);
console.log("callback story: ", story);
story.save(function (err, story) {
if (err) return console.error(err);
console.log('successful save new story w/ populated _creator');
console.log('The story: creators name: %s', story._creator.name);
console.log('The story: creator field: %s', story._creator);
})
});
First time executing:
>>node getting-started.js
Following output:
>>opened conn to db
successful story save! 15
opened conn to db
{ fans: [],
__v: 0,
_creator: { stories: [], __v: 0, age: 154, name: 'Johns', _id: 15 },
title: 'Once upon a timex######',
_id: 56244cf796f73380353e803a }
successful save new story w/ populated _creator
The story: creators name: Johns
The story: creator field: { stories: [], __v: 0, age: 154, name: 'Johns', _id: 15 }
However, after commenting out the following code:
var Johns = new Person({ _id: 15, name: 'Johns', age: 154});
Johns.save(function (err) {
if (err) {
console.log('Person Save error! %s', err);
}
var story1 = new Story({
title: "Once upon a timex??",
_creator: Johns._id // assign the _id from the person
});
story1.save(function (err) {
if (err) return handleError(err);
console.log('successful story save! %d', story1._creator);
});
});
the next time I'm executing
Story
.find({ title: 'Once upon a timex######' })
.exec(function (err, story) {
if (err) return handleError(err);
console.log("callback story: ", story);
});
I'm unable to see my saved Story with the populated field of _creator, it still only has the person _id and not the person object:
{ fans: [],
__v: 0,
_creator: 15,
title: 'Once upon a timex######'
_id: 56244cf796f73380353e803a }

Related

Populate in Mongoose not working

I am trying to perform associations by referencing method. There are 2 models:
1. User
2. Product
I have established one-to-one relationship of 1 user can have multiple products. User creation is successful
Product creation is successful
Code Inputs
var mongoose = require("mongoose");
mongoose.connect("mongodb://localhost/product_demo_x9");
Product Schema
var productSchema = new mongoose.Schema({
category : String,
Brand: String
});
var Product = mongoose.model("product", productSchema);
User Schema
var userSchema = new mongoose.Schema({
email: String,
name: String,
products: [
{
type: mongoose.Schema.Types.ObjectId,
ref: "Product"
}
]
});`
var User = mongoose.model("user", userSchema);
User Creation
User.create({
email: "madhur#google.com",
name: "Maddy"
},function(err,newUser){
if(err){
console.log(err);
}
else {
console.log(newUser);
}
});
Product Creation
Product.create({
category: "Smartwatches",
Brand: "Samsung and Google"
},
function(err,product){
console.log(product);
User.findOne({name : "Maddy"},function(err,foundUser){
if(err) {
console.log(err);
}
else {
foundUser.products.push(product);
foundUser.save(function(err,updatedUser){
if(err){
console.log(err);
}
else {
console.log(updatedUser);
}
});
}
});
});
Display of associated Data on the console
User.find({email: "madhur#google.com"}).
populate("products").
exec(function(err,user){
if(err){
console.log(err);
}
else {
console.log(user);
}
});
Code Outputs
User Creation (Success)
[{
products: [],
_id: 5a47acb0317d4e3c2081b8ce,
email: 'madhur#google.com',
name: 'Maddy',
__v: 0
}]
Product Creation and associating (Success)
{
_id: 5a47acd53c771123b4018ff1,
category: 'Smartwatches_2',
Brand: 'Samsung and Google',
__v: 0
}
{
products: [ 5a47acd53c771123b4018ff1 ],
_id: 5a47acb0317d4e3c2081b8ce,
email: 'madhur#google.com',
name: 'Maddy',
__v: 1
}
Display of embedded data using populate - Failure!!
{ MissingSchemaError: Schema hasn't been registered for model "products".
Use mongoose.model(name, schema)
at new MissingSchemaError
Can anyone please explain me how to do it correctly?
Thanks in Advance
Model Name is Case-sensitive
'Product' is not equal to 'product'
and when u create a model as 'product' (singular) it converts it into plural, i.e. 'products', this is default mongoose behavior, can be overridden.
so change the following:
var userSchema = new mongoose.Schema({
email: String,
name: String,
products: [
{
type: mongoose.Schema.Types.ObjectId,
ref: "products" //<---- changed 'Product' to 'products'
}
]
});`
var User = mongoose.model("user", userSchema);
Try this
var mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/product_demo_x9');
var productSchema = new mongoose.Schema({
category: String,
Brand: String
});
var Product = mongoose.model('Product', productSchema);
var userSchema = new mongoose.Schema({
email: String,
name: String,
products: [
{
type: mongoose.Schema.Types.ObjectId,
ref: 'Product'
}
]
});
var User = mongoose.model('User', userSchema);
User.create({
email: 'madhur#google.com',
name: 'Maddy'
}, function(err, newUser) {
if (err) {
console.log(err);
} else {
console.log(newUser);
}
});
Product.create({
category: 'Smartwatches',
Brand: 'Samsung and Google'
},
function(err, product) {
console.log(product);
User.findOne({name: 'Maddy'}, function(err, foundUser) {
if (err) {
console.log(err);
} else {
foundUser.products.push(product);
foundUser.save(function(err, updatedUser) {
if (err) {
console.log(err);
} else {
console.log(updatedUser);
}
});
}
});
});
User.find({email: 'madhur#google.com'})
.populate('products')
.exec(function(err, user) {
if (err) {
console.log(err);
} else {
console.log(user);
}
});
Solved
Did the following
Downgraded my Mongoose version from 5.00x to 4.10.8 using the following command npm remove mongoose then npm install mongoose#4.10.8 --save
Made the following change in app.js file
var userSchema = new mongoose.Schema({
email: String,
name: String,
products: [
{
type: mongoose.Schema.Types.ObjectId,
ref: "product" //<---- changed 'Product' to 'product'
}
]
});`
var User = mongoose.model("user", userSchema);
Thanks to the Stack community for giving a try!

node js mongoose delete a from an id in document array

I've two collection of reference both collection together. One of the collection is user and the other collection is project.
So, a user can add project to the projects collection, then one of the user type called supervisor can add staff to the project and the project id saved to the user collection which referred to staff document on the project collection.
So actually i need to do when admin deletes a supervisor from the user collection it deletes all the projects created by supervisor users's id that equal to addedBy documents which deleted from the users collection.
So my problems is when i do this process i need to delete all the project id is equal to the users collection projectId. it's an array and I tried to do this to many times but i couldn't find a solution. I'll provide all of the source code. That i created for this project.
Users collection
const userSchema = new Schema({
firstName: {
type: String
},
lastName: {
type: String
},
email: {
type: String
},
username: {
type: String
},
password: {
type: String
},
usertype: {
type: Schema.ObjectId,
ref: 'usertypes'
},
projectId: [{
type: Schema.ObjectId,
ref: 'projects'
}]
});
Project collection
const proSchema = new Schema({
projectName: {
type: String
},
description: {
type: String
},
addedBy: {
type: Schema.ObjectId,
ref: 'users'
},
staff: [{
type: Schema.ObjectId,
ref: 'users'
}]
});
Here is the query that i tried to do the process that i mentioned in above
Users
.findByIdAndRemove({
_id: req.params.id
})
.then(function(){
Projects
.remove({
userId: req.params.id
})
.then(function(err, project){
if(err) throw err;
console.log(project.id)
Users
.update({}, {
$pull: {
projectId: project.id
}
}, function(){
res.json({
success: true,
message: "Deleted"
});
});
});
});
I think the problems are
(1) Model.findByIdAndRemove only expects the ID (not the condition) i.e. Users.findByIdAndRemove(req.params.id) instead of Users.findByIdAndRemove({ _id: req.params.id })
(2) Model.remove's callback does not have a second argument in Projects.remove({ userId: req.params.id }).then(function (err, project) {. As well, you don't have a userId field in your ProjectSchema.
I would do
// delete user
Users.findByIdAndRemove(req.params.id, function (err, user) {
console.log('deleting user', user);
if (err)
throw err;
// delete user's projects
Projects.remove({ addedBy: user._id }, function (err) {
console.log('deleting projects');
if (err)
throw err;
// delete project references
Users.update({}, { $pull: { projectId: { $in: user.projectId }}}, function (err) {
console.log('deleting project references');
if (err)
throw err;
res.json({ success: true, message: "Deleted" });
});
});
});
(3) user.projectId is an array of ObjectIDs, so you need to use $in (see first example).
Aside: projects is a better name than projectId. The latter is ambiguous because a user has multiple projects not projectIds.
User.findByIdAndUpdate(req.params.idRec,
{ $pull: { comments: { _id: comm._id } } },
function (err, doc) {
if (!err) {
res.status(200).send()
} else {
res.render('error', { error: err })
}
})

Mongoose Subdocument array pushing

My scenario is if person1 accepting person2 deal means..the person1_id will save inside that person2 particular deal field accepted,i have tried the code it was working perfectly if a accepted user(person2) has one deal but in case of more than one deal it was updating but deleting other deals (i.e,the suppose the person2 having 3 deals means if person1 accepting 3rd deal the accepted user id was updating in 3rd deal and the 1st and 2nd deal was deleted).Anyone please help me how to save only the updated deal array
var incomingUser = req.user;//accepting user accesstoken in header(person1)
if(req.params.id){
var id = req.params.id;//deal id
console.log("DealId:"+id + "Acceptinguser:"+incomingUser.name);
User.findOne(
{
"deals": {
$elemMatch: {
_id: id
}
}
},
function(err, data){
console.log("Dealer:" +data.name);
console.log("deal:"+ data.deals);
if(err){
console.log("User not found");
res.send(new restify.ResourceNotFoundError('failed','Deal not found'));
return next();
}
var dealObj = _.filter(data.deals, { id: id })[0];
console.log("Deal Obj" + dealObj);
var acceptingUser = incomingUser;
console.log("accepting user:" +acceptingUser._id);
dealObj.accepted = acceptingUser._id;
console.log("accept id: "+ dealObj.accepted);
data.deals = dealObj;
console.log("data:"+ data.deals);
data.save(function (err, result){
console.log("Result:" + result);
if(err){
console.log("Internal error");
res.send(new restifyc.InternalError('failed','Error accepting'));
return next();
}
console.log("saved");
res.send(200,{user: result});
return next();
});
});
}
}
And my schema is
var dealSchema = new mongoose.Schema({
shopName: {type: String,required: true},
deal: {type: String,required: true},
price:{type: Number,required: true},
start:{type: Date,default: Date.now},
end:{type: Date},
expiry:{type: Date},
comments:{type: String},
accepted: {type:mongoose.Schema.Types.ObjectId, ref:'user'},//person1 _id
rejected: {type:mongoose.Schema.Types.ObjectId, ref: 'user'}
});
var userSchema = new mongoose.Schema({
name: { type: String,required: true},
phone: { type: Number, required: true,unique: true},
email:{type: String},
password: {type: String},
deals:[dealSchema]
}, {collection: 'user'});
mongoose.model('Deal', dealSchema);
mongoose.model('user', userSchema);
Yep in order to update specifically what you need you can use the <array>.$ for the specified position of the element:
User.update(
"deals": {
$elemMatch: {
_id: id
}
}, {
"$set": {
"deals.$" : {/*your deal data*/}
}
}, function(err, doc) {
});
More details on how to use the $ wildcard https://docs.mongodb.org/manual/reference/operator/update/positional/

Mongoose one-to-many - not quite sure how to implement it

I'm relatively new to Mongoose (2 days at it) and I want to make a one-to-many relationship, as in one person can come from one country, one country has many people.
So, this is what I've got:
var userSchema = new Schema({
name: String,
username: {
type: String,
required: true,
unique: true
},
password: {
type: String,
required: true
},
country: {
type: Schema.Types.ObjectId,
ref: 'Country'
}
});
var User = mongoose.model('Person', userSchema);
var countrySchema = new Schema({
name: {
type: String,
required: true,
unique: true
},
created_at: Date,
updated_at: Date,
people: [{
type: Number,
ref: 'User'
}]
});
var Country = mongoose.model('Country', countrySchema);
var UK = new Country({
name: 'UK'
});
usa.save(function(err) {
var user = new User({
username: 'James',
password: 'Bond',
country: UK._id
});
user.save(function(err) {
});
});
Now I have two questions: 1) I've seen that ref can sometimes be an ObjectId or just a number - what's the differences? 2) when saving the data, in my case, I saved country to a person (by _id), how do I save a person to a country? Should I update the instance of the model?
Thanks
UPDATE:
since this question has been marked as a duplicate, let me rephrase the question: consider the official example in this link: http://mongoosejs.com/docs/populate.html
The idea is that one person has many stories, and one story has one author (person). So, the saving would be as follows:
var aaron = new Person({ _id: 0, name: 'Aaron', age: 100 });
aaron.save(function (err) {
if (err) return handleError(err);
var story1 = new Story({
title: "Once upon a timex.",
_creator: aaron._id // assign the _id from the person
});
story1.save(function (err) {
if (err) return handleError(err);
// thats it!
});
});
That's from the official documentation - my question is, where or how do we save story1 to the Author? Author is created before the Story, so, shouldn't the Author be updated with story1._id???
UPDATE 2:
I figured out that if I use only type: Schema.Types.ObjectId and never type: Number, that I can do just this:
var aaron = new Person({ _id: 0, name: 'Aaron', age: 100 });
var story1 = new Story({
title: "Once upon a timex.",
_creator: aaron._id // assign the _id from the person
});
aaron.stories.push(story1._id);
aaron.save(function (err) {
if (err) return handleError(err);
});
story1.save(function (err) {
if (err) return handleError(err);
// thats it!
});
This actually works in a dummy example... are there any problems if there were too many posts in a request that IDs could have get lost/duplicated? What is the shortcoming of this approach?
1) I've seen that ref can sometimes be an ObjectId or just a number - what's the differences?
Please refer to this question Why do they use an ObjectId and a Number in the Mongoose Population example?
where or how do we save story1 to the Author
aaron.save(function (err) {
if (err) return handleError(err);
var story1 = new Story({
title: "Once upon a timex.",
_creator: aaron._id // assign the _id from the person
});
story1.save(function (err) {
if (err) return handleError(err);
// save id of story1 into person here, also you should use `update` operation with `$push` operator.
aaron.stories.push(story1._id);
aaron.save(function(err){
if (err)
handleError(err);
else
console.log('save person successfully...');
})
});
});
The results
> db.stories.find()
{ "_id" : ObjectId("56f72f633cf1e6f00159d5e7"), "title" : "Once upon a timex.", "_creator" : 0, "fans" : [ ], "__v" : 0 }
> db.people.find()
{ "_id" : 0, "name" : "Aaron", "age" : 100, "stories" : [ ObjectId("56f72f633cf1e6f00159d5e7") ], "__v" : 1 }

Mongoose population ref

Hye!
This is an example of http://mongoosejs.com/docs
var personSchema = Schema({
_id : Number,
name : String,
age : Number,
stories : [{ type: Schema.Types.ObjectId, ref: 'Story' }]
});
var storySchema = Schema({
_creator : { type: Number, ref: 'Person' },
title : String,
fans : [{ type: Number, ref: 'Person' }]
});
var Story = mongoose.model('Story', storySchema);
var Person = mongoose.model('Person', personSchema);
Story
.findOne({ title: 'Once upon a timex.' })
.populate('_creator')
.exec(function (err, story) {
if (err) return handleError(err);
console.log('The creator is %s', story._creator.name);
// prints "The creator is Aaron"
})
And that is my codeMy '_creator' is not an array but an array of objects :
var storySchema = Schema({
_creator : [{
_id : { type: Number, ref: 'Person' },
quantity : Number
}],
...
});
And my request is :
Story
.findOne({ title: 'Once upon a timex.' })
.populate('_creator._id')
.exec(function (err, story) {
if (err) return handleError(err);
console.log('The creator is %s', story._creator.name);
// prints "The creator is Aaron"
})
But this solution 'populate('_creator._id')' doesn't work.
Have you got an idea?
Thank you!

Resources