Accessing ref object without calling populate

100 views
Skip to first unread message

Tug

unread,
Aug 13, 2013, 1:06:09 PM8/13/13
to mongoo...@googlegroups.com
Hello,
Is there a way to set and later access a reference object without calling populate ?
I have a large object, let's call it 'user' of type User which is kept in memory.
Now I have a schema which has a reference to a User object. It looks like this :

var Room = new mongoose.Schema({
        ...
        owner            : { type: mongoose.Schema.Types.ObjectId, ref: 'User' },
});

when creating a new room referencing that user I can write :
var room = new Room({ owner: user });
or
var room = new Room({ owner: user._id });

and save it in database :
room.save(function(err) { ... });

This works.
Now I want to keep 'room' in memory and access the 'user' object from it.
I don't want to reload the whole user object from database by calling populate('owner').
I just want to write something like :
room.set('owner', user);
and not have the owner property be set to user._id

How can I do that ?

I have tried :
room._doc.owner = user;
but if I call room.save() after that the owner reference will be replaced by the whole user document.

Thanks.

Aaron Heckmann

unread,
Aug 14, 2013, 7:14:08 PM8/14/13
to mongoo...@googlegroups.com
Do not touch _doc. It's prone to break things.

You essentially need to cache the room and owner together in memory right? Maybe define a virtual getter/setter that handles storing/returning the user in some temporary place in memory. 


--
Documentation - http://mongoosejs.com/
Plugins - http://plugins.mongoosejs.com/
Bug Reports - http://github.com/learnboost/mongoose
Production Examples - http://mongoosejs.tumblr.com/
StackOverflow - http://stackoverflow.com/questions/tagged/mongoose
Google Groups - https://groups.google.com/forum/?fromgroups#!forum/mongoose-orm
Twitter - https://twitter.com/mongoosejs
IRC - #mongoosejs
---
You received this message because you are subscribed to the Google Groups "Mongoose Node.JS ODM" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mongoose-orm...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 



--
Aaron


Tug

unread,
Aug 19, 2013, 2:11:55 PM8/19/13
to mongoo...@googlegroups.com
Thanks for your help Aaron.
I think that would have been great if there was a way to hack the room object for it to behave like I just called populate. Looking at the code it does not seem easily doable though.
I agree that a temporary dictionary plus some virtual getter/setter could do the job just fine but I am quite hesitant in breaking my code construct.
For now, I choosed to just set room._doc.streamer and make sure the room object is only read after that. What's the worst that could happen:D

Tug

unread,
Mar 25, 2014, 11:04:21 AM3/25/14
to mongoo...@googlegroups.com
Could a document.markNotModified method make sense ?
Reply all
Reply to author
Forward
0 new messages