mercredi 25 février 2015

Saving reference to a mongoose document, after findOneAndUpdate

I feel like I'm encountering something completely simple, yet can not figure it out, will be glad, if you can help me.


I'm using mongoose + socket.io as CRUD between client and server. As I'm using sockets, there is a private scope individual for each client's socket, in which, for future use without making db find calls, I would like to store a reference of mongoose document, that I once found for this user.


One client is creanting a Room:



var currentroom;

client.on('roomcreate', function (data) {
currentroom = new Room({
Roomid: id,
UsersMeta: [UserMeta],
///other stuff///
})
currentroom.save(function (err, room) {
if (err) console.log(err);
else console.log('success');
});
});


Then, whenewer I want, on another creator's call I can just simply



currentroom.Roomnaid = data;
curretroom.save()


And it's working fine, the problem is - I do not understand how I can get the same reference on not creating, but Room search, for the moment i'm using this for search:



Room.findOneAndUpdate({ Roomid: roomid }, { $push: { UsersMeta: UserMeta}}, { new: false }, function (err, room) {
if (err) console.log(err);
console.log('room output:');
console.log(room);
client.emit('others', room);
})


The thing is, that in one call I want to:

1: find a doc in db,

3: send it to user (in pre-updated state),

4: update found document,

2: save a reference (of the current updated doc)


With findOneAndUpdate I can do all, but not saving a current reference. So, how I need to approach it then?


Aucun commentaire:

Enregistrer un commentaire