how use Schema methods in mongoose sub-documents after loading - node.js

I have the following scheme in the mongoose
var Schema = mongoose.Schema;
var CellSchema = new Schema({
foo: Number,
});
CellSchema.methods.fooMethod= function(){
return 'hello';
};
var GameSchema = new Schema({
field: [CellSchema]
});
if create new document like:
var cell = new CellModel({foo: 2})
var game = new GameModel();
game.field.push(cell);
game.field[0].fooMethod();
it's correctly work. But if you run this code:
GameModel.findOne({}, function(err, game) {
console.log(game);
game.field[0].fooMethod()
})
i get TypeError: game.field[0].fooMethod is not a function
and console log is
{
field:
[ { foo: 2,
_id: 5675d5474a78f1b40d96226d }
]
}
how correct load sub-document with all schema methods?

You have to define the methods on the embedded schema before defining the parent schema.
Also you have to reference CellSchema instead of 'Cell'
var CellSchema = new Schema({
foo: Number,
});
CellSchema.methods.fooMethod = function() {
return 'hello';
};
var GameSchema = new Schema({
field: [CellSchema]
});

Related

Importing a custom mongoose schema in another schema

My software has a mongoose Schema, let's call it carSchema, and I am exporting a model of the carSchema called Car. Example:
/* Car.js */
var carSchema= new Schema({
_id: Schema.Types.ObjectId,
type: String,
wheels: Number
});
carSchema.statics.drive = async function(){
...
}
module.exports = mongoose.model('Car', carSchema);
Now say my software has another schema called lotSchema in another file, that uses carSchema in it:
/* Lot.js */
var lotSchema = new Schema({
_id: Schema.Types.ObjectId,
carArr: [carSchema]
});
lotSchema.statics.getAllId = async function(){
return carSchema[0]['_id']
}
How can I properly import or export carSchema for use in lotSchema? Would a simple const carSchema = require('Car.js') suffice?
If you are not using not using es6/7 then you can do this
/* Car.js */
const carSchema= new Schema({
_id: Schema.Types.ObjectId,
type: String,
wheels: Number
});
carSchema.statics.drive = async function(){
...
}
module.exports.carSchema = carSchema;
module.exports.carModel = mongoose.model('Car', carSchema);
/* Lot.js */
const { carSchema , carModel } = require('./Car.js');
var lotSchema = new Schema({
_id: Schema.Types.ObjectId,
carArr: [carSchema]
});
lotSchema.statics.getAllId = async function(){
return carSchema[0]['_id']
}
You could do this in Car.js
module.exports = {
useCarSchema: function(myCollection) {
return mongoose.model('Car', carSchema, myCollection);
}
}
And this in Lot.js
var carSchema = require('./Car.js');
var Car = carSchema.useCarSchema(myCollection);
You can use Populate:
Population is the process of automatically replacing the specified
paths in the document with document(s) from other collection(s). We
may populate a single document, multiple documents, plain object,
multiple plain objects, or all objects returned from a query.
Example:
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const carSchema= new Schema({
_id: Schema.Types.ObjectId,
type: String,
wheels: Number
});
const lotSchema = new Schema({
_id: Schema.Types.ObjectId,
cars: [{ type: Schema.Types.ObjectId, ref: 'Car' }]
});
mongoose.model('Car', carSchema);
mongoose.model('Lot', lotSchema);
Now, to populate works, you first need to create a car object car = new Car({}) and a lot object lot = new Lot({}), and then:
lot.cars.push(car);
lot.save(callback);

Mongoose null checking before saving on nested schema array

I have an Schema model like this:
var propertySchema = new Schema({
name: {type: String, required: true},
surname: String
});
var objSchema = new Schema({
properties: [prepertySchema]
});
var accountSchema = new Schema({
objects: [objSchema]
});
mongoose.model('account', accountSchema);
Then i have the operations:
account.objects.push(null);
account.save(function(error, account) {
//Error checking and response
})
In that case, i'm getting a ValidationError because of the null value. This is expected. But, in the next operations:
var obj = {properties: null}
account.objects.push(obj);
account.save(function(error, account) {
//Error checking and response
})
Here the value is stored on database, and then i have an unexpected null value where it had been an array. Doing that with objects like this,
var obj = {
properties: [{name:'randname'}, null]
}
Also saves null values in the database that are prohibited to the data model.
I've read about validators, and middleware for checking things. Is there anyway to do this directly on the schema, or i have to parse the received object before i save it in the database? What is the best approach for this?
Well you could just use the model definitions for this. Even though you are embedding you can still do this but of course you do not want to actually save the objects to their own collection. Just feed them into the item as embedded:
var async = require("async"),
mongoose = require("mongoose"),
Schema = mongoose.Schema;
mongoose.connect('mongodb://localhost/prop');
var propertySchema = new Schema({
name: { type: String, required: true },
surname: String
});
var objSchema = new Schema({
properties: [propertySchema],
});
var accountSchema = new Schema({
objects: [objSchema]
});
var Account = mongoose.model( 'account', accountSchema );
var ObjMod = mongoose.model( 'ObjMod', objSchema, null, false );
var PropMod = mongoose.model( 'PropMod', propertySchema, null, false );
var account = new Account();
var prop = new PropMod({ "name": null });
var obj = new ObjMod({ properties: prop });
account.objects.push( obj );
account.save(function(err,account) {
if (err) throw err;
console.log( JSON.stringify( account, undefined, 4 ) );
});
So what happens there is the validation will work for each stage, in this case it will fail on the name of the property schema item not being a string or even if not included.

Mongoose schema inheritance and model populate

I have been trying this with the built in inheritance features of mongoose (rather than the extend plugin) but haven't been having much luck so far. This is a simplified example of code I am trying to use which exhibits the same problem. This is based on an expanded version of the mongoose documentation for schema inheritance using discriminators - http://mongoosejs.com/docs/api.html#model_Model.discriminator
var util = require('util');
var mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/problem');
var Schema = mongoose.Schema;
var ObjectId = Schema.ObjectId;
function BaseSchema() {
Schema.apply(this, arguments);
this.add({
name: String,
createdAt: Date
});
}
util.inherits(BaseSchema, Schema);
var BossStatusSchema = new Schema({
status: String
});
var BossStatus = mongoose.model('BossStatus', BossStatusSchema);
var PersonSchema = new BaseSchema();
var Person = mongoose.model('Person', PersonSchema);
var BossSchema = new BaseSchema({
department: String,
bossStatus: {
type: ObjectId,
ref: 'BossStatus'
}
});
var Boss = Person.discriminator('Boss', BossSchema);
Example code to add the documents:
var superBoss = new BossStatus({
status: 'super'
});
var normalBoss = new BossStatus({
status: 'normal'
});
var andy = new Person({
name: 'Andy'
});
var billy = new Boss({
name: 'Billy',
bossStatus: superBoss._id
});
var callback = function(err, result) {
console.dir(err);
console.dir(result);
};
superBoss.save(callback);
normalBoss.save(callback);
andy.save(callback);
billy.save(callback);
So when finding a record without populate:
Person
.findOne({
name: 'Billy'
})
.exec(callback);
The result is as expected, the bossStatus refers to an _id from the bossstatuses collection:
null
{ name: 'Billy',
bossStatus: 52a20ab0185a7f4530000001,
_id: 52a20ab0185a7f4530000004,
__v: 0,
__t: 'Boss' }
When adding the populate call:
Person
.findOne({
name: 'Billy'
})
.populate('bossStatus')
.exec(callback);
The resulting bossStatus property of the Person result is null:
null
{ name: 'Billy',
bossStatus: null,
_id: 52a20ab0185a7f4530000004,
__v: 0,
__t: 'Boss' }
EDIT:
Ok I've just put together what is probably a better example of what I'm trying to achieve, the schema structure lends itself more to a relational DB but hopefully makes the problem clearer.
var util = require('util');
var mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/problem');
var Schema = mongoose.Schema;
var ObjectId = Schema.ObjectId;
function BaseSchema() {
Schema.apply(this, arguments);
this.add({
name: {
type: String,
unique: true,
required: true
}
});
}
util.inherits(BaseSchema, Schema);
var DeviceSchema = new BaseSchema();
var LocalDeviceSchema = new BaseSchema({
driver: {
type: ObjectId,
ref: 'Driver'
}
});
var RemoteDeviceSchema = new BaseSchema({
networkAddress: {
type: ObjectId,
ref: 'NetworkAddress'
}
});
var DriverSchema = new Schema({
name: {
type: String,
unique: true,
required: true
}
});
var NetworkHostSchema = new Schema({
host: {
type: String,
unique: true,
required: true
}
});
var NetworkAddressSchema = new Schema({
networkHost: {
type: ObjectId,
ref: 'NetworkHost'
},
port: {
type: Number,
min: 1,
max: 65535
}
});
var Driver = mongoose.model('Driver', DriverSchema);
var NetworkHost = mongoose.model('NetworkHost', NetworkHostSchema);
var NetworkAddress = mongoose.model('NetworkAddress', NetworkAddressSchema);
var Device = mongoose.model('Device', DeviceSchema);
var LocalDevice = Device.discriminator('LocalDevice', LocalDeviceSchema);
var RemoteDevice = Device.discriminator('RemoteDevice', RemoteDeviceSchema);
var networkHost = new NetworkHost({
host: '192.168.2.1'
});
var networkAddress = new NetworkAddress({
networkHost: networkHost._id,
port: 3000
});
var remoteDevice = new RemoteDevice({
name: 'myRemoteDevice',
networkAddress: networkAddress._id
});
var driver = new Driver({
name: 'ftdi'
});
var localDevice = new LocalDevice({
name: 'myLocalDevice',
driver: driver._id
});
var callback = function(err, result) {
if(err) {
console.log(err);
}
console.dir(result);
};
/*
// Uncomment to save documents
networkHost.save(function() {
networkAddress.save(function() {
remoteDevice.save(callback);
});
});
driver.save(function() {
localDevice.save(callback);
});
*/
var deviceCallback = function(err, device) {
if(err) {
console.log(err);
}
switch(device.__t) {
case 'LocalDevice':
console.log('Would create a local device instance passing populated result');
break;
case 'RemoteDevice':
console.log('Would create a remote device instance passing populated result');
break;
}
};
Device
.findOne({name: 'myLocalDevice'})
.populate('driver')
.exec(deviceCallback);
The LocalDevice and RemoteDevice schemas could (and probably would) include other differences..
The switch would for example use a DeviceFactory or something to create the instances. My thinking was it should be possible to search the devices table for a device by 'name' and populate the collection references (if this is the correct terminology?) without having to specify the collection to search in - this was my understanding of the point of schema inheritance - or have I completely misunderstood?
Thanks for replies so far!
You are looking for a Boss, not a Person:
Boss
.findOne({
name: 'Billy'
})
.populate('bossStatus')
.exec(callback);
Looks like a bug. With debugging active, this is what's being shown for the population query:
Mongoose: people.findOne({ name: 'Billy' }) { fields: undefined }
Mongoose: people.find({ _id: { '$in': [ ObjectId("52a221ee639cc03d71000001") ] } }) { fields: undefined }
(the ObjectId shown is the one stored in bossStatus)
So Mongoose is querying the wrong collection (people instead of bossstatuses).
As #regretoverflow pointed out, if you're looking for a boss, use the Boss model and not the Person model.
If you do want to populate bossStatus through the Person model, you can explicitly state a model that needs to be searched for population:
.populate({
path : 'bossStatus',
model : 'BossStatus'
})
// or shorter but less clear:
// .populate('bossStatus', {}, 'BossStatus')
EDIT: (with your Device examples)
driver is part of LocalDeviceSchema, but you're querying the Device model, which has no notion of what driver is and populating driver within the context of a Device instance doesn't make sense to Mongoose.
Another possibility for populating each instance is to do it after you retrieved the document. You already have the deviceCallback function, and this will probably work:
var deviceCallback = function(err, device) {
if(err) {
console.log(err);
}
switch(device.__t) { // or `device.constructor.modelName`
case 'LocalDevice':
device.populate('driver', ...);
break;
case 'RemoteDevice':
device.populate('networkAddress', ...);
break;
}
};
The reason is that the document is already cast into the correct model there, something that apparently doesn't happen when you chain populate with the find.

Saving an array property on a Mongoose schema

I have a mongoose object schema that looks similar to the following:
var postSchema = new Schema({
imagePost: {
images: [{
url: String,
text: String
}]
});
I'm trying to create a new post using the following:
var new_post = new Post();
new_post.images = [];
for (var i in req.body.post_content.images) {
var image = req.body.post_content.images[i];
var imageObj = { url: image['url'], text: image['text'] };
new_post.images.push(imageObj);
}
new_post.save();
However, once I save the post, it's created with an empty array for the images property. What am I doing wrong?
You're missing the imagePost object of your schema in your new object. Try this instead:
var new_post = new Post();
new_post.imagePost = { images: [] };
for (var i in req.body.post_content.images) {
var image = req.body.post_content.images[i];
var imageObj = { url: image['url'], text: image['text'] };
new_post.imagePost.images.push(imageObj);
}
new_post.save();
I've just done something similar, in my case appending to an existing collection, please see this question/answer. It may help you:
Mongoose / MongoDB - Simple example of appending to a document object array, with a pre-defined schema
Your problem is that in Mongoose you can't have nested objects, only nested Schemas. So you need to do something like this (for your desired structure):
var imageSchema = new Schema({
url: {type:String},
text: {type:String}
});
var imagesSchema = new Schema({
images : [imageSchema]
});
var postSchema = new Schema({
imagePost: [imagesSchema]
});

How to set ObjectId as a data type in mongoose

Using node.js, mongodb on mongoHQ and mongoose. I'm setting a schema for Categories. I would like to use the document ObjectId as my categoryId.
var mongoose = require('mongoose');
var Schema = mongoose.Schema,
ObjectId = Schema.ObjectId;
var Schema_Category = new Schema({
categoryId : ObjectId,
title : String,
sortIndex : String
});
I then run
var Category = mongoose.model('Schema_Category');
var category = new Category();
category.title = "Bicycles";
category.sortIndex = "3";
category.save(function(err) {
if (err) { throw err; }
console.log('saved');
mongoose.disconnect();
});
Notice that I don't provide a value for categoryId. I assumed mongoose will use the schema to generate it but the document has the usual "_id" and not "categoryId". What am I doing wrong?
Unlike traditional RBDMs, mongoDB doesn't allow you to define any random field as the primary key, the _id field MUST exist for all standard documents.
For this reason, it doesn't make sense to create a separate uuid field.
In mongoose, the ObjectId type is used not to create a new uuid, rather it is mostly used to reference other documents.
Here is an example:
var mongoose = require('mongoose');
var Schema = mongoose.Schema,
ObjectId = Schema.ObjectId;
var Schema_Product = new Schema({
categoryId : ObjectId, // a product references a category _id with type ObjectId
title : String,
price : Number
});
As you can see, it wouldn't make much sense to populate categoryId with a ObjectId.
However, if you do want a nicely named uuid field, mongoose provides virtual properties that allow you to proxy (reference) a field.
Check it out:
var mongoose = require('mongoose');
var Schema = mongoose.Schema,
ObjectId = Schema.ObjectId;
var Schema_Category = new Schema({
title : String,
sortIndex : String
});
Schema_Category.virtual('categoryId').get(function() {
return this._id;
});
So now, whenever you call category.categoryId, mongoose just returns the _id instead.
You can also create a "set" method so that you can set virtual properties, check out this link
for more info
I was looking for a different answer for the question title, so maybe other people will be too.
To set type as an ObjectId (so you may reference author as the author of book, for example), you may do like:
const Book = mongoose.model('Book', {
author: {
type: mongoose.Schema.Types.ObjectId, // here you set the author ID
// from the Author colection,
// so you can reference it
required: true
},
title: {
type: String,
required: true
}
});
My solution on using ObjectId
// usermodel.js
const mongoose = require('mongoose')
const Schema = mongoose.Schema
const ObjectId = Schema.Types.ObjectId
let UserSchema = new Schema({
username: {
type: String
},
events: [{
type: ObjectId,
ref: 'Event' // Reference to some EventSchema
}]
})
UserSchema.set('autoIndex', true)
module.exports = mongoose.model('User', UserSchema)
Using mongoose's populate method
// controller.js
const mongoose = require('mongoose')
const User = require('./usermodel.js')
let query = User.findOne({ name: "Person" })
query.exec((err, user) => {
if (err) {
console.log(err)
}
user.events = events
// user.events is now an array of events
})
The solution provided by #dex worked for me. But I want to add something else that also worked for me: Use
let UserSchema = new Schema({
username: {
type: String
},
events: [{
type: ObjectId,
ref: 'Event' // Reference to some EventSchema
}]
})
if what you want to create is an Array reference. But if what you want is an Object reference, which is what I think you might be looking for anyway, remove the brackets from the value prop, like this:
let UserSchema = new Schema({
username: {
type: String
},
events: {
type: ObjectId,
ref: 'Event' // Reference to some EventSchema
}
})
Look at the 2 snippets well. In the second case, the value prop of key events does not have brackets over the object def.
You can directly define the ObjectId
var Schema = new mongoose.Schema({
categoryId : mongoose.Schema.Types.ObjectId,
title : String,
sortIndex : String
})
Note: You need to import the mongoose module
Another possible way is to transform your _id to something you like.
Here's an example with a Page-Document that I implemented for a project:
interface PageAttrs {
label: string
// ...
}
const pageSchema = new mongoose.Schema<PageDoc>(
{
label: {
type: String,
required: true
}
// ...
},
{
toJSON: {
transform(doc, ret) {
// modify ret directly
ret.id = ret._id
delete ret._id
}
}
}
)
pageSchema.statics.build = (attrs: PageAttrs) => {
return new Page({
label: attrs.label,
// ...
})
}
const Page = mongoose.model<PageDoc, PageModel>('Page', pageSchema)
Now you can directly access the property 'id', e.g. in a unit test like so:
it('implements optimistic concurrency', async () => {
const page = Page.build({
label: 'Root Page'
// ...
})
await page.save()
const firstInstance = await Page.findById(page.id)
const secondInstance = await Page.findById(page.id)
firstInstance!.set({ label: 'Main Page' })
secondInstance!.set({ label: 'Home Page' })
await firstInstance!.save()
try {
await secondInstance!.save()
} catch (err) {
console.error('Error:', err)
return
}
throw new Error('Should not reach this point')
})

Resources