nodejs mongoose many-to-many relation on MongoDB - node.js

I'm trying to make a many-to-many relation between two documents in mongoose. Just can't make it work.
I've been trying to utilize the mongoose populate method, but with no success. Does anyone know any good tutorials or example on how to take on the matter?
Update:
Got schemas
var EventSchema = new Schema({
users: [{
type: Schema.Types.ObjectId,
ref: 'User'
}]
});
and
var UserSchema = new Schema({
events: [{
type: Schema.Types.ObjectId,
ref: 'Event'
}]
});
In my tests I pused to event model user like so
event.users.push(user);
event.save(function(err, doc) {
user.events[0].should.equal(event._id);
done();
});
firstly I need to push somehow saved event to added user to event. Then by using populate I should can 'dress up' every object in events array and users array. Preferably in post save callback, if I understood populate correctly.
This test pases
it('creates', function(done) {
event.users.append(user);
event.save(function(err, ev) {
user.save(function(err, doc) {
doc.events[0].should.equal(ev._id);
doc.populate('events', function(err, d) {
console.log(d);
done();
});
});
});
});
So I know the ids are stored correctly, but when I run doc.populate() the returned document has events array empty. Just don't get it.

Made it work... used mongo-relation package to add the needed ids for me, but docs for this plugin made me change the schema form
var UserSchema = new Schema({
events: [{
type: Schema.ObjectId,
ref: 'Event'
}]
});
to
var UserSchema = new Schema({
events: [Schema.ObjectId]
});
once corrected, it worked.

Related

Nodejs- combine two diffrent lists into one

I've got two models in mongoose:
const user = mongoose.Schema({
name: String,
...
});
and
const moderator = mongoose.Schema({
name: String,
...
});
When I access these collections with:
user.find()
I get in response list of users and moderators separetly.
What I want to achive is to join these two list together to get a list based on this model:
const myDataSchema = mongoose.Schema({
user: {type: Schema.Types.ObjectId, ref: 'user'},
moderator: {type: Schema.Types.ObjectId, ref: 'moderator'}
});
Where one of attributes (either user or moderator) will be set.
I solved my problem. Instead of using the mongoose schema I created my "own object". My code:
let list= [];
users.forEach( function (user)
{
list.push({user: user});
});
moderators.forEach( function (moderator)
{
list.push({moderator: moderator});
});
It's probaly not the most efficient way but it works.

Saving ALL nested documents on parent update Mongoose

My parent model looks like this:
var OrderSchema = new mongoose.Schema({
serviceNotes: {type: mongoose.Schema.Types.ObjectId, ref: 'Service'},
vehicle: {type: mongoose.Schema.Types.ObjectId, ref: 'Vehicle'}
});
The children look like this:
var VehicleSchema = new mongoose.Schema({
name: String
});
var ServiceSchema = new mongoose.Schema({
baseCost: Number
});
I am trying to find an easy solution to updating all of these documents at once. The problem is, when I call an update on an order, it does not update the nested documents. Please see the following:
exports.updateOrder = function (req, res) {
var order = req.body.order,
update = {
$set: order
};
Order.findOneAndUpdate({_id: order._id}, update, {new: true}, function (err, order) {
if (err) handleError(res);
if (!order) return res.status(404).send('Order not found.');
return res.json({order: order});
});
}
An example of req.body in this case may look like this:
{
order: {
_id: 829198218932shdbn,
serviceNotes: {
_id: 8932838nsd2sdnbd,
baseCost: 1
},
vehicle: {
_id: iu283823872378bd,
name: 'Honda'
}
}
}
The order update should also update the serviceNotes with the updated information, and the vehicle with the updated information.
The only way I have been able to update the nested documents is by calling a findOneAndUpdate on the children and updating them one by one. I am looking for a solution to just call update on the parent (order) and have the children update as well.

mongoose schema, is it better to have one or have several for different tasks?

so I'm been making a site that has comments section, messaging, profile and shopping for the user. I been wondering about when making a schema for those functions, is it better to have all in one schema like
userSchema {
name: String,
....
....
}
or have them seperate like
userSchema {
}
commentSchema {
}
gallerySchema {
}
No one can give you clear answer for this, everyone has different views.
Basically, It depends on your project's scalability
As I see your requirement for this project
You can create a single schema and use it as embedded form, but it's not a very good idea if you are scaling the app.
My recommendation is to create the separate schema for all the tasks which will be easy to debug,scaling the app and in readable form.
Edit
If you are creating separate schema and want to connect them then you can use populate on the basis of ObjectId
See the docs to populate collections
Example
var mongoose = require('mongoose')
, Schema = mongoose.Schema
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);
Population
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"
});

Deep populate self referencing schema in mongoose

I have an self referencing employee schema in mongoose.
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var Employee = new Schema({
name: String,
description: {
type: String,
default: 'No description'
},
manager: {
type: Schema.Types.ObjectId,
ref: 'Employee',
default: null
},
reportee: [{
type: Schema.Types.ObjectId,
ref: 'Employee'
}]
});
An employee can a manager & can have several reportee. If manager is null then the employee is treated as top level employee.
I need to created hierarchy based on this model. I am struggling to generate desired output.
So far I have tried to use popluate() & mongoose-deep-populate module but I am unable to get the desired output. I wonder its becuase I have a self referencing model. Or may be I am not using these two options properly.
This is what I have tried with deep-populate module. It seems to be populating reportee model but not repotree of reportee model. In short its only populating 1 level of records.
Employee.deepPopulate(employees, 'reportee.reportee.reportee.reportee.reportee', function (err, _employee) {
employees.forEach(function (employee) {
});
});
Please suggest how can I retrive all the employee hierarchy ?
To answer my own question, I am using mongoose-deep-populate library.
To use it we need to install the module:
npm install mongoose-deep-populate
//Register the plugin
var deepPopulate = require('mongoose-deep-populate');
Employee.plugin(deepPopulate);
And then use this code:
Employee.deepPopulate(employees, 'reportee.reportee.reportee.reportee.reportee', function (err, _employee) {
employees.forEach(function (employee) {
});
});
This will load 5 levels of reportees as we have mentioned reportee.reportee.reportee.reportee.reportee

Mongoose populate return undefined

I'm currently trying to develop an app using mongo and node.js.
I am facing a problem when I want to build a query who use the populate option.
Here are my Schemas :
// Schema used by mongoose
var userSchema = new mongoose.Schema(
{
_id: mongoose.Schema.Types.ObjectId,
login: String,
password: String,
movies: [ { type: mongoose.Schema.Types.ObjectId, ref: movieModel} ],
admin: Boolean
},{ collection: "user" });
var movieSchema = new mongoose.Schema(
{
_id: mongoose.Schema.Types.ObjectId,
title: String,
}, { collection: "movie" });
As you can see, each user have an array of movies, this array contains valid ids of movies. What I want is to have the movies of an user. This is how I build my query :
var query = userModel.findOne({ login: req.session.user["login"] })
.populate("movies");
query.exec(function(err, user)
{
if (err)
throw err;
console.log(user.movies[0].title);
});
The query is executed successfully, but when I try to display the title of the first movie at the console.log line I got an error "TypeError: Cannot read property 'title' of undefined". I checked the documentation of mongoose and don't understand why I'm getting this error.
I would like to specify that my database contains valid data.
I put mongoose in debug mode, and this is the query that is executed :
Mongoose: user.findOne({ login: 'user' }) { fields: undefined }
Mongoose: user.find({ _id: { '$in': [ ObjectId("52e2a28949ad409834473e71"), ObjectId("52e2a28949ad409834473e79") ] } }) { fields: undefined }
The two ids on the second line are valid ids of movies. I would like to display their name.
Thanks a lot for your help.
What is the value of this: ref: movieModel?
movieModel would need to be set to the string like "Movie". See here for more information. It will need to match the identifier provided when you create the Movie model.
var Movie = mongoose.model('Movie', movieSchema);
So, you might have in a schema:
var userSchema = mongoose.Schema({
name: String,
favorite_movies: { type: Schema.Types.ObjectId, ref: 'Movie' }
});
var User = mongoose.model('User', userSchema);
I've used the string Movie in both the Schema definition and when creating the Movie type. They need to be exactly the same.
MongooseJs uses the string name of the Model to determine where to fetch the documents from when using ref and populate.
In the debug output, you can see how Mongoose is actually querying the wrong collection, as I'd expect it to be using movies.find to find the relevant Movie documents.

Resources