Mongoose/node.js如何查找,填充,处理,“填充"和更新

问题描述:

我想对db进行优美而优雅的调用,但一直缺乏猫鼬经验.

I want to make a nice and elegant call to db, but constantly suffer from lack of mongoose experience.

希望我不太烦人,我只是在我的docs-google-Stack-diging技能失败时才尝试解决堆栈溢出问题.

Hope i'm not too annoying, i'm trying to adress Stack Overflow only when my docs-google-Stack-digging skills fails.

我想做什么:

-在数据库中查找文档
-在此文档中填充数组(Schema中的此数组是UserMeta Schema中的Objectid s的数组)
-通过插座将其发送到一些更好的地方
-update找到具有另一个_id的文档,该文档与UserMeta
中的文档相对应 -将此更新保存到db&作为将来对var currentroom

-find a doc in DB
-populate array inside this doc (this array in Schema is an array of Objectids from UserMeta Schema)
-send it throug sockets to some better places
-update found doc with another _id reffering to doc in UserMeta
-save this updates to db & as future reference to var currentroom

问题在最后两个步骤中发生,因为我无法取消填充"文档,因为我已经得到响应并进一步保存它.

Problem occures in last 2 steps, as i can't 'unpopulate' doc, that i already got as response and update-save it further.

目前,我是在没有任何人口的情况下这样做的:

For the moment that is how i'm doing this without any population:

Room.findOne({ Roomid: roomid }, function (err, oldRoom) {
    client.emit('others', oldRoom);
    oldRoom.UsersMeta.push(currentUser._id);
    oldRoom.save(function (err, newRoom) {            
        currentroom = newRoom;
    });
})

我可以将需要的文档从torent UserMeta到toJSON蛮力地复制到此Room文档,并且只需手动维护它们.但是,如果有一种方法可以通过便捷的mongoose工具自动执行此操作,那么我想采用这种方法.而且当然是出于好奇.

I can just brute-force-ish copy needed docs through toJSON from parrent UserMeta to this Room doc and just manually maintain both of them. But if there is a way to do this automagically via handy mongoose tools, I would like to take this way. And in the sake of curiosity, of course.

这是我上一个问题的延续保存对猫鼬文档的引用,在findOneAndUpdate 之后- 只是一句话,您真的不需要去那里

It's a continuation of my previous question Saving reference to a mongoose document, after findOneAndUpdate - just a remark, you dont really need to go there

更新:事情是我需要在findOne中运行populate(),因此作为响应,我已经在oldRoom中填充了_id s

Upd: Thing is that I need to run populate() In query with findOne, therefore in response I got oldRoom already with populated _ids

Room.findOne({ Roomid: roomid }).populate('UsersMeta').exec(function (err,       oldRoom) {
    if (err) {
        console.log(err);
    }
    else if (oldRoom) {
        console.log(oldRoom.UsersMeta)
        client.emit('others', oldRoom.UsersMeta);
        oldRoom.UsersMeta.push(currentUser._id);
        oldRoom.save(function (err, newRoom) {
            currentroom = newRoom;
            console.log(newRoom);
        });
    }
    else { console.log('nothing found') };
})

upd2:所以我弄清楚了,如果我在已经填充的oldroom中推入新的_id并将其保存,在db中,它将自动以应有的方式出现为_id的集合.但是我现在感到困惑的是,如果我继续使用该currentroom参考,因为它已经被删除了,我该如何安全地从填充数组中删除某些内容而又不从db中完全删除填充的条目.

upd2: so i figured out, that if i push new _id in already populated oldroom and save it, in db it will automagically appear as set of just _id's as it should be. Yet I now confused if i will continue to work with this currentroom reference, as it was already pupulated, how can i safely remove something from populated array without removing populated entry from db completely.

upd3:啊,我只是一团糟.出于某些奇怪的原因,我认为对每个套接字客户端保存在变量中的文档的引用将始终指向db中的最新文档,并且我将能够通过该文档使用该文档,从而无需使用find db工具不止一次获得此参考...我需要重新考虑我的数据库逻辑.
SO
然后有一个问题.如果用户连接到Rooms这是RoomSchema的文档,并且用户是套接字用户,即他具有个人作用域,则可以在其中存储他的个人会话详细信息.我可以以某种方式存储到此特定Room文档的直接链接,以阐明例如如果用户更改了房间的名称,则需要通过整个数据库来搜索该房间.如果我需要searh-似乎更好的做法是保存用户所在房间的ID,然后在db中通过该ID查找房间并更改其名称,对吗?

upd3: Ah i just made a mess in my head. For some weird reason i thought that reference to doc saved in variable for each socket client will be always pointing to up-to-date doc in db, and that i will be able to work with this doc through it elluminating need to using find db tools more than once to get this reference... I need to rethink my db logic.
SO
There is a question then. If user connected to Rooms which is a doc from RoomSchema, and a user is a socket user i.e he has a personal scope in which i can store his personal session details. Can i somehow store direct link to this particular Room doc to elluminate need of searching for this room through whole db if user, for example, changes room's name. If i NEED to searh - it seems that it a better practice to save an id of room in which user is, and then just look up in db for room by this id and change it's name, am I right?

以下是获取ID为UserMeta的查询

Here is the query to get UserMeta with ids only

Room.findOne({ Roomid: roomid },function (err, oldRoom) {

});