Arrays in schemas, virtuals and other stuff

195 views
Skip to first unread message

Kim

unread,
Apr 27, 2011, 3:07:21 AM4/27/11
to Mongoose Node.JS ORM
Hey all,

I have to be honest: moving from sql to nosql is a hard piece of
work ;)

At the moment, I am working on a simple project. I've got users and
their status.

A user can have many statuses and I want to store the last status
directly in the user.

My schemas look like this:

var UserStatus = new Schema({
user : ObjectId,
status : String,
date : { type : Date, default : Date.now },
lastdate : { type : Date, default : Date.now }
});

UserStatus.virtual('dateseconds').get(function() {
return this.date.getTime();
});
UserStatus.virtual('lastdateseconds').get(function() {
return this.lastdate.getTime();
});
UserStatus.virtual('json').get(function() {
return {
status : this.status,
date : this.dateseconds,
lastdate : this.lastdateseconds,
_id : this._id
}
});

var User = new Schema({
user : { type : ObjectId, Index : true },
lastStatus : [],
name : String,
date : { type : Date, default : Date.now }
});

User.virtual('status').set(function(v) {
while(this.lastStatus.length > 0) {
this.lastStatus.pop();
}
this.lastStatus.push(v.json);
}).get(function() {
return this.lastStatus[0];
});

The lastStatus property is an array because "lastStatus : Object" and
"lastStatus : {}" did not work. They exited with exceptions.
So I choose to use an array and store the status' json data in it. I
want only one record in my lastStatus property, so I tried to clear
the array. I've tried to use pop, remove, splice but there are no
records beeing deleted.

As I am new to mongoose, I want to hear your oppinion. Are there
better ways to implement this? (I am sure there are ;)) And why is it
not possible to delete records from the lastStatus array?

Waiting for your replies, cheers

Patrick McCoy

unread,
Apr 27, 2011, 6:04:49 PM4/27/11
to Mongoose Node.JS ORM
Consider using an embedded document to store the status of the user.
http://mongoosejs.com/docs/embedded-documents.html

-Patrick

Kim

unread,
Apr 28, 2011, 2:34:05 AM4/28/11
to Mongoose Node.JS ORM
So I would keep my UserStatus collection and add a new embedded
document in the user. Sounds like a solution.

I have tried to use "lastStatus : UserStatus" and "lastStatus :
ObjectId", too. First one did not work (why?) and the second one
stores only the id. I thought it would be more like a reference. Is it
possible to fetch the object behind the ObjectId directly when
accessing the property? It sounded to me like the lazy loading option
in hybernate and that is an awesome feature.

Why is it not possible to store simple objects in mongo? "lastStatus :
Object" results in an error.

Thanks for your help


On 28 Apr., 00:04, Patrick McCoy <patr...@pjmccoy.com> wrote:
> Consider using an embedded document to store the status of the user.http://mongoosejs.com/docs/embedded-documents.html

Sander Pick

unread,
Apr 28, 2011, 2:38:25 PM4/28/11
to mongoo...@googlegroups.com, Mongoose Node.JS ORM
> Why is it not possible to store simple objects in mongo? "lastStatus :
> Object" results in an error.

Just do:

lastStatus: {}

Kim

unread,
Apr 29, 2011, 2:06:52 AM4/29/11
to Mongoose Node.JS ORM
Is there any example usage that worked in a real life example? I tried
lastStatus : {} and it stored nothing. If I use String or ObjectId the
same code stores data.

On 28 Apr., 20:38, Sander Pick <sanderp...@gmail.com> wrote:
> > Why is it not possible to store simple objects in mongo? "lastStatus :
> > Object" results in an error.
>
> Just do:
>
> lastStatus: {}
>

Sander Pick

unread,
Apr 29, 2011, 2:28:02 AM4/29/11
to mongoo...@googlegroups.com, Mongoose Node.JS ORM
Can you share your code?

Kim

unread,
Apr 29, 2011, 2:50:56 AM4/29/11
to Mongoose Node.JS ORM
Tried it again and it worked, must have been my fault.

But is there no lazy loading or direct fetching of ObjectId into a
document? That would be an awesome feature and should be easy to
implement.

On 29 Apr., 08:28, Sander Pick <sanderp...@gmail.com> wrote:
> Can you share your code?
>

Sander Pick

unread,
Apr 29, 2011, 3:38:57 AM4/29/11
to mongoo...@googlegroups.com, Mongoose Node.JS ORM
You just have to implement that yourself... You could use a getter or virtual attribute.
Reply all
Reply to author
Forward
0 new messages