在findOneAndUpdate之后保存对猫鼬文档的引用 [英] Saving reference to a mongoose document, after findOneAndUpdate

查看:135
本文介绍了在findOneAndUpdate之后保存对猫鼬文档的引用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果您能帮助我,我觉得自己遇到的事情非常简单,但仍无法解决,会很高兴.

我正在使用mongoose + socket.io作为客户端和服务器之间的CRUD.当我使用套接字时,每个客户端的套接字都有一个私有作用域个体,为了将来使用而无需进行db find调用,我想存储一个猫鼬文档的引用,该引用是我曾经为该用户找到的. /p>

一个客户正在创建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');
    });
});

然后,在需要的时候,在另一个创作者的电话上,我可以简单地

currentroom.Roomnaid = data;
curretroom.save()

它工作正常,问题是-我不了解如何在不使用创建但使用Room搜索的情况下获得相同的引用,目前我正在使用它进行搜索:

 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);
 })

问题是,我想打个电话:
1:在db中查找文档,
3:将其发送给用户(处于更新前的状态),
4:更新找到的文档,
2:保存参考(有关当前更新文档的信息)

使用findOneAndUpdate,我可以执行所有操作,但不能保存当前参考. 那么,那我该如何处理呢?

解决方案

喜欢这个;

Room.findOne({ Roomid: roomid }, function (err, oldRoom) {
    //make changes to oldRoom
    //then save it like this
    oldRoom.save(function(err,newRoom) {
      //newRoom is updated document
      //now you have reference to both old and new docs
    });
 })

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?

解决方案

Like this;

Room.findOne({ Roomid: roomid }, function (err, oldRoom) {
    //make changes to oldRoom
    //then save it like this
    oldRoom.save(function(err,newRoom) {
      //newRoom is updated document
      //now you have reference to both old and new docs
    });
 })

这篇关于在findOneAndUpdate之后保存对猫鼬文档的引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆