I have a schema called Message and it has a property replies
which are also Message objects. I am trying to define it in mongoose, but replies
keeps returning undefined.
var MessageSchema = new Schema({
sender: {
type: Schema.ObjectId,
ref: 'User'
},
replies: [{type:Schema.ObjectId, ref:'Message'}],
roomId: String,
sendTime: Date,
content: String,
parentId: Schema.ObjectId
});
I've also tried replies: [MessageSchema]
and replies:[Schema.ObjectId]
but all of them keep returning undefined.
Nothing wrong with the code you are listing so it's what you are not showing us that causes the problems.
When you reference you are going to both save a copy of the object in the collection and also add that related ObjectId value to the array of objects that are in "reply" in your case.
There are several ways to do it, but a good safe MongoDB way is using $push
to add additional items.
As a complete example:
var async = require('async'),
mongoose = require('mongoose'),
Schema = mongoose.Schema;
var userSchema = new Schema({
"name": String
});
var messageSchema = new Schema({
"sender": { "type": Schema.Types.ObjectId, "ref": "User" },
"replies": [{ "type": Schema.Types.ObjectId, "ref": "Message" }],
"roomId": String,
"sendTime": { "type": Date, "default": Date.now },
"content": String,
"parentId": Schema.Types.ObjectId
});
var Message = mongoose.model( "Message", messageSchema );
var User = mongoose.model( "User", userSchema );
mongoose.connect('mongodb://localhost/test');
async.waterfall(
[
// Clean up samples
function(callback) {
async.each(
[User,Message],
function(model,callback) {
model.remove({},callback);
},
callback
)
},
// Create user
function(callback) {
User.create({ "name": "Bill" },callback);
},
// Create a message
function(user,callback) {
Message.create({
"sender": user._id,
"roomId": "1",
"content": "message"
},function(err,message) {
callback(err,user,message);
});
},
// Create a reply
function(user,message,callback) {
Message.create({
"sender": user._id,
"roomId": "1",
"content": "reply",
"parentId": message._id
},callback);
},
// Save that reply on the parent
function(message,callback) {
Message.findByIdAndUpdate(
message.parentId,
{ "$push": { "replies": message._id } },
function(err,message) {
console.info( message );
callback(err);
}
);
},
// List that
function(callback) {
Message.find({},function(err,messages) {
if (err) callback(err);
console.log(
"All:\n%s",
JSON.stringify( messages, undefined, 4 )
);
callback();
});
}
],
function(err) {
if (err) throw err;
mongoose.disconnect();
}
);
And the output:
{ _id: 54f3dff198a8c85306a2ef67,
sender: 54f3dff198a8c85306a2ef66,
roomId: '1',
content: 'message',
__v: 0,
sendTime: Mon Mar 02 2015 14:58:41 GMT+1100 (AEDT),
replies: [ 54f3dff198a8c85306a2ef68 ] }
All:
[
{
"_id": "54f3dff198a8c85306a2ef67",
"sender": "54f3dff198a8c85306a2ef66",
"roomId": "1",
"content": "message",
"__v": 0,
"sendTime": "2015-03-02T03:58:41.387Z",
"replies": [
"54f3dff198a8c85306a2ef68"
]
},
{
"_id": "54f3dff198a8c85306a2ef68",
"sender": "54f3dff198a8c85306a2ef66",
"roomId": "1",
"content": "reply",
"parentId": "54f3dff198a8c85306a2ef67",
"__v": 0,
"sendTime": "2015-03-02T03:58:41.393Z",
"replies": []
}
]
That way if you called .populate()
on an message that has "replies" present, it will go back to the collection and retrieve the related data and make it appear that data was part of that item as well.
Please not that such magic does not happen "recursively" without your own intervention. It's just a basic helper, so if you want more then you still have to do the lifting yourself.