If I have a a Schema in Mongoose that's defined like:
var subSchema = new Schema({
some: String
});
var topSchema = new Schema({
subs: [subSchema]
});
var topModel = mongoose.model("Top", topSchema);
Is it possible to define an instance method for the sub document? I've tried the following(added before the model declaration), but it doesn't work:
subSchema.methods.someFn = function() {
return 'blah';
};
Answering my own question.
What I originally wanted to do was to create a function that can be used on the collection of subdocs, as in:
topdoc.subs.someFn();
However, what I actually did with code in the original question was create a function for a subdoc itself, as in:
topdoc.subs[i].someFn();
This works.
As far as I can tell, creating a function for the collection of subdocs is not supported by Mongoose.
I got around this by defining a method in topSchema that would do what I want.
Related
I wanted to find the subdocument with id and return the subdocument, not the parent document, my schema are as follows :
var coursesSchema = new mongoose.Schema({
coursename:{type:String}
});
var parentSchema = new mongoose.Schema({
courses:[coursesSchema]
});
var Parent= mongoose.model('parent', parentSchema );
module.exports = Parent;
let data = Parent.courses.id(_id);
as given in the docs I tried to find the subdocument using .id, it is throwing error saying Cannot read property 'id' of undefined,
and after searching a lot found this question I tried the same, apparently it also returned the same error.
let data = Parent['courses'].id(_id);
please help me with this, I am not able to get my head around with it๐
In order to get the subdocument, you have to do the following:
Look for the document
Reference the subdocument property
In your case, you have defined a Model. Based on this model, you would need to:
Parent.findOne({ _id: ObjectId("abcd12345678901234567890") }, function(err, doc) {
let subDocument = doc.courses;
...
}
The reason you get the "undefined" error is because the courses subdocument doesn't have a field called id. It only has a field called coursename.
We can solve this using Aggregation operations as #KunalMukherjee pointed out in the comments Here is a quick example to do so
Most of the examples I have seen online do something like...
var UserSchema = new mongoose.Schema({
name: String,
age: String
});
However, recently I found a book do the above... but without the new keyword.
var UserSchema = mongoose.Schema({
name: String,
age: String
});
I am now confused. Do we use the new keyword for creating the schema or not.. and what happens in both cases?
Both are valid and returns a new instance of the Mongoose.Schema class. What this means is that both does exactly the same. This line checks whether you already have an instance of the Schema class, if not, it returns one for you.
To summarize, if you call
var schema = new mongoose.Schema({})
you initialize an instance yourself, while if you call
var schema = mongoose.Schema({})
mongoose initializes one for you, with this:
function Schema(obj, options) {
if (!(this instanceof Schema)) {
return new Schema(obj, options);
}
...
Yes, I agree with the above answer. But here I would like to add some more important points to it. Looking at the Mongoose documentation, I feel like it should always be called with the new keyword since Schema is a constructor.
Both will work and are valid, but my suggestion is to only use the new mongoose.Schema() since it's the correct way. Following standard conventions will make your code easier to read; not only for others but for you as well when you go back to it in 6 months.
i have a model schema as :
var A = new Schema ({
a: String,
b : [ { ba: Integer, bb: String } ]
}, { collection: 'a' } );
then
var M = mongoose.model("a", A);
var saveid = null;
var m = new M({a:"Hello"});
m.save(function(err,model){
saveid = model.id;
}); // say m get the id as "1"
then
m['b'].push({ba:235,bb:"World"});
m.save(function(err,model){
console.log(model.id); //this will print 1, that is the id of the main Document only.
//here i want to find the id of the subdocument i have just created by push
});
So my question is how to find the id of the subdocument just pushed in one field of the model.
I've been looking for this answer as well, and I'm not sure that I like accessing the last document of the array. I do have an alternative solution, however. The method m['b'].push will return an integer, 1 or 0 - I'm assuming that is based off the success of the push (in terms of validation). However, in order to get access to the subdocument, and particularly the _id of the subdocument - you should use the create method first, then push.
The code is as follows:
var subdoc = m['b'].create({ ba: 234, bb: "World" });
m['b'].push(subdoc);
console.log(subdoc._id);
m.save(function(err, model) { console.log(arguments); });
What is happening is that when you pass in the object to either the push or the create method, the Schema cast occurs immediately (including things like validation and type casting) - this means that this is the time that the ObjectId is created; not when the model is saved back to Mongo. In fact, mongo does not automatically assign _id values to subdocuments this is a mongoose feature. Mongoose create is documented here: create docs
You should also note therefore, that even though you have a subdocument _id - it is not yet in Mongo until you save it, so be weary of any DOCRef action that you might take.
The question is "a bit" old, but what I do in this kind of situation is generate the subdocument's id before inserting it.
var subDocument = {
_id: mongoose.Types.ObjectId(),
ba:235,
bb:"World"
};
m['b'].push(subDocument);
m.save(function(err,model){
// I already know the id!
console.log(subDocument._id);
});
This way, even if there are other database operations between the save and the callback, it won't affect the id already created.
Mongoose will automatically create an _id for each new sub document, but - as far as I know - doesn't return this when you save it.
So you need to get it manually. The save method will return the saved document, including the subdocs. As you're using push you know it will be the last item in the array, so you can access it from there.
Something like this should do the trick.
m['b'].push({ba:235,bb:"World"});
m.save(function(err,model){
// model.b is the array of sub documents
console.log(model.b[model.b.length-1].id);
});
If you have a separate schema for your subdocument, then you can create the new subdocument from a model before you push it on to your parent document and it will have an ID:
var bSchema = new mongoose.Schema({
ba: Integer,
bb: String
};
var a = new mongoose.Schema({
a: String,
b : [ bSchema ]
});
var bModel = mongoose.model('b', bSchema);
var subdoc = new bModel({
ba: 5,
bb: "hello"
});
console.log(subdoc._id); // Voila!
Later you can add it to your parent document:
m['b'].push(subdoc)
m.save(...
I'm calling MongoDB from my Node app using Mongoose like this:
var query = itemModel.find();
query.exec(function (err, items) {
console.log(err);
socket.emit("items", items);
});
I have 3 models defined like this:
var Schema = mongoose.Schema
, ObjectId = Schema.ObjectId;
var playerModel = require('./models/player.js').make(Schema, mongoose);
var characterModel = require('./models/character.js').make(Schema, mongoose, ObjectId);
var itemModel = require('./models/item.js').make(Schema, mongoose);
my models look like this:
function make(Schema, mongoose) {
itemSchema = new Schema({
name: String
, bonus: [{
type: String
, value: Number
}]
, price: Number
, slot: String
});
return mongoose.model('Character', characterSchema);
}
exports.make = make;
For some reason I'm getting all documents, regardless of them being items, characters or players. Since I'm calling find() on itemModel I was expecting only Items, what am I doing wrong?
The model that you have shown appears to be the item model, but you are creating the model with the 'Character' name. This means that you told Mongoose about the scheme for an item and that it is stored in the 'character' collection. Assuming you've done the same for each other model (Character/Player), you've been Mongoose that everything is in the same collection.
Then you query that collection and you seem surprised that everything is stored in the same collection. It strikes me as if you have little experience with Mongoose/MongoDB, so I will suggest you download and learn to love MongoVUE. This application is a good GUI to see what is going on under the hood of the MongoDB database. While developing, you also might want to enable debugging so you can see what queries mongoose is launching to the server (mongoose.set('debug', true)).
here is my code for models.js where I keep models
var mongoose = require('mongoose')
, Schema = mongoose.Schema;
var GroupSchema = new Schema({
title : String
, elements : [ElementSchema]
, author : String
});
var ElementSchema = new Schema({
date_added : Date
, text : String
, author : String
});
mongoose.model('Group', GroupSchema);
exports.Group = function(db) {return db.model('Group');};
mongoose.model('Element', ElementSchema);
exports.Element = function(db) { return db.model('Element');
};
To me it looks pretty clear, but when I do
function post_element(req, res, next) {
Group.findOne({_id: req.body.group}, function(err, group) {
new_element = new Element({author: req.body.author,
date_added: new Date()});
new_element.save();
group.elements.push(new_element);
group.save();
res.send(new_element);
return next();
})
}
I don't understand why when I go in Mongo I have two collections one called Groups with nested groups (so it looks fine) and the other collection is called Elements.
Why? Shouldn't it be called just Group ?
I don't understand, a good chap that please explain it to me?
Thanks,
g
When you execute this line:
new_element.save();
you're saving the newly created element to the Elements collection. Don't call save on the element and I think you'll get the behavior you're looking for.
Its because of the following line:
mongoose.model('Element', ElementSchema);
This registers a model in mongoose and when you register a model, it will create its own collection inside mongo. All you have to do is get rid of this line and you will see it disappear.
On another note, its much cleaner and easier to setup your files to only export one model per file using the following to export the model:
module.exports = mongoose.model('Group', GroupSchema);
Hope this helps!