Mongoose create test data and populate without a database connection - javascript

I am writing a unit test that uses a mongoose model which has nested object. I want to populate the main model and the referenced model without calling 'populate' and fetching anything from the database. Here is an example in coffeescript
CarSchema = new mongoose.Schema
name:
type: String
required: true
engine:
type: ObjectId
ref: 'Engine'
required: true
Car = mongoose.model('Car', CarSchema)
EngineSchema = new mongoose.Schema
name:
type:String
required: true
Engine = mongoose.model('Engine', EngineSchema)
engine1 = new Engine({name: 'test'})
car1 = new Car({engine: engine1, name: 'car'})
assert.equal (car1.engine.name, 'test') #this fails
What happens is that car1.engine is set to an id and not to the engine object.
Is there a way to get this working?

calling setValue will retain the hydrated document:
car1.setValue('engine', engine1)

Related

'this' is empty during Mongoose middleware

long time Java programmer here trying to figure out Node.js
I am trying to cascade a delete to child objects using the pre middleware, but no child delete is happening, while the parent deletes without a hitch. Leaving my database full of sad orphans. When I started logging to the console I see that the reference to 'this' is empty. For most cases this seems to be a problem of using ==> to create the function, but I am not doing so:
GameSession(parent):
var mongoose = require('mongoose');
var TimeLineEvent = require('../models/timelineevent');
//Define a schema
var Schema = mongoose.Schema;
var GameSessionSchema = new Schema({
name: {type: String, required: true},
gameMasterId : {type: Schema.Types.ObjectId, ref: 'GameMaster', required: true},
});
GameSessionSchema.pre('findOneAndDelete', function(next) {
console.log('GameSessionSchema.pre findOneAndDelete this ='+this);
console.log('GameSessionSchema.pre findOneAndDelete id ='+this._id);
TimeLineEvent.deleteMany({gameSessionId: this._id}).exec();
next();
});
//Export function to create "SomeModel" model class
module.exports = mongoose.model('GameSessionModel', GameSessionSchema );
TimeLineEvent(child):
//Require Mongoose
var mongoose = require('mongoose');
//Define a schema
var Schema = mongoose.Schema;
var TimeLineEventSchema = new Schema({
name: {type: String, required: true},
gameSessionId: {type: Schema.Types.ObjectId, ref: 'GameSession', required: true},
time: {type: Number, required: true},
nextAction: {type: Number}
});
module.exports = mongoose.model('TimeLineEventModel', TimeLineEventSchema );
This is what the console shows when the pre method is called:
GameSessionSchema.pre findOneAndDelete this =[object Object]
GameSessionSchema.pre findOneAndDelete id =undefined
Can you see what I am missing? Thanks!
From the documentation: "In query middleware functions, this refers to the query."
If you use console.log(this) you will be able to view the full Query object. You might find this.model references what you need.

Populating an Object with Information from a Child Object with Different Model (JS/Mongoose)

Here is what I have. I created a project model that references a user model for an array of members.
var ProjectSchema = new mongoose.Schema(
{title: {
type: String,
required: true
},
members: [
{user: {
type: mongoose.Schema.Types.ObjectId,
ref: 'users'
}
}],
});
User Schema (I have code that creates a model from both of these schemas)
const UserSchema = new mongoose.Schema({
name: {
type: String,
required: true
},
email: {
type: String,
required: true,
unique: true
}
});
In a separate file, I want to export the JSON of a found project and include the information of the users in the members array, but I am not sure how to do that. This is the code I have right now.
const project = await Project.findById(req.params.proj_id).populate(
'members'
);
res.json(project);
It has no trouble finding the project but the only information I can get on the members is their id. I tried using for loops to gather the information from members separately using the id that I can get from the project, but the code gets messy and I am hoping to find a simpler way to do it.
You can use mongoose populate query to get all members associated with a project. It should populate array of objects users associated to a project. You should be doing something like this:
const project = await Project.findById(req.params.proj_id)
await project.populate('members').execPopulate()
console.log(project.members)
Mongoose docs for the reference: Populate
You can give the model to your mongoose populate.
const project = await Project.findById(req.params.proj_id)
.populate({
'members',
model: UserModel
})
.exec()
res.json(project);
keep in mind you've to create UserModel just like
const UserModel = mongoose.model('User', userSchema);

Nested elements in mongoose modelling

I have to create a database in this format:
I have tried to do it like this :
var mongoose = require("mongoose")
, Schema = mongoose.Schema;
var categorySchema = new Schema({
_id: String,
subcategory: {
type: Schema.Types.ObjectId,
ref: "Subcategory"
},
id: String,
name: String,
page_description: String,
page_title: String,
parent_category: String,
c_showInMenu: Boolean
});
module.exports = mongoose.model("Category", categorySchema);
And I did the same for every subcategory, but I named them "Subcategory" and "SubsubCategory". I'm pretty much a newbie, I've read the documentation on mongoose and I've also followed an online course. I feel like I know something, but I don't understand it properly or that my logic behind it is wrong.
Also, I've managed to find this about recursive elements in Schemas, but I don't fully understand it and I don't know how to implement it to my database: Recursive elements in Schema : Mongoose modelling
I checked your code and collection image.
Here's what you are doing wrong.
You don't need to declare _id field, it's auto-generated.
Keep the column names same as they are in the given collection image.
This should be your schema declaration. Note that, this schema is for the whole collection and not just for categories array.
var mongoose = require("mongoose"),
Schema = mongoose.Schema;
var mainSchema = new Schema({
categories: [ {
id: String,
image: String,
name: String,
page_description: String,
page_title: String,
parent_category_id: String,
c_showInMenu: Boolean
}]
});
This is just an example of the schema, please add necessary changes to it.

How do I check the value of a field in a mongo database

I am creating a Node app that would need to check the fields of a collection and return what the data type is. For example if the field is "First Name" the data type would be "String". How would I get started creating a back end application that does this?
In case you are using mongoose , then Each field or nested field is addressed by path.
var myschema = new Schema({
...
name: {
first:{type: String, required: true,},
last :{type: String, required: true,},
...
});
here name.first and name.last are paths.
Now to know the type of name.last there is an Schema API, called path().So.
var pathmeta = myschema.path(name.last);
console.log(" datatype = "+pathmeta.instance);
console.log(" whole pathmeta structure is "+JSON.stringify(pathmetas));
should print this..
datatype = String
whole pathmeta structure is
{"enumValues":[],"regExp":null,"path":"text","instance":"String","validators":[],"setters":[],"getters":[],"options":{},"_index":null}

Mongoose JS findOne always returns null

I've been fighting with trying to get Mongoose to return data from my local MongoDB instance; I can run the same command in the MongoDB shell and I get results back. I have found a post on stackoverflow that talks about the exact problem I'm having here; I've followed the answers on this post but I still can't seem to get it working. I created a simple project to try and get something simple working and here's the code.
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var userSchema = new Schema({
userId: Number,
email: String,
password: String,
firstName: String,
lastName: String,
addresses: [
{
addressTypeId: Number,
address: String,
address2: String,
city: String,
state: String,
zipCode: String
}
],
website: String,
isEmailConfirmed: { type: Boolean, default: false },
isActive: { type: Boolean, default: true },
isLocked: { type: Boolean, default: false },
roles: [{ roleName: String }],
claims: [{ claimName: String, claimValue: String }]
});
var db = mongoose.connect('mongodb://127.0.0.1:27017/personalweb');
var userModel = mongoose.model('user', userSchema);
userModel.findOne({ email: 'test#test.com' }, function (error, user) {
console.log("Error: " + error);
console.log("User: " + user);
});
And here is the response of the 2 console.log statements:
Error: null
User: null
When the connect method is called I see the connection being made to my Mongo instance but when the findOne command is issued nothing appears to happen. If I run the same command through the MongoDB shell I get the user record returned to me. Is there anything I'm doing wrong?
Thanks in advance.
Mongoose pluralizes the name of the model as it considers this good practice for a "collection" of things to be a pluralized name. This means that what you are currently looking for in code it a collection called "users" and not "user" as you might expect.
You can override this default behavior by specifying the specific name for the collection you want in the model definition:
var userModel = mongoose.model('user', userSchema, 'user');
The third argument there is the collection name to be used rather than what will be determined based on the model name.
I know 7 years pass, however I'm starting to develop in node JS with MongoDB using mongoose, so searching for solution to the same problem, result = null.
After read this post and all the answers, I release that I totally forget to include the DB name in the string of the connection, 'mongodb://localhost:27017/DB name' so that solved my case. I guessed this can be help other clueless like me! :)

Categories