Mongoose - bidirectional reference

I'm newbie in Mongoose and MongoDB, but even after hours of searching on the internet I can't found the answer.

Imagine a chat where should chat together only people in pairs. For that purpose I created schema similar to this:

var schema = new mongoose.Schema({
    chatWith: { type: mongoose.Schema.ObjectId, ref: 'User' }
});

var User = mongoose.model('User', schema)

Question:

In case when I set user2 in chatWith path on user1, is possible that it can be automatically set on the other side?

Something like that:

var user1 = new User();
var user2 = new User();
user1.chatWith = user2;
user2.chatWith === user1; // true

If it is not possible, what is the most effective / best way to achieve this?

A better way to handle this would be to keep a list of those users that people are currently "chatting to". This can be done with using an array type in the schema:

var schema = new mongoose.Schema({
    chattingTo: [{ type: mongoose.Schema.ObjectId, ref: 'User' }]
});

var User = mongoose.model('User', schema)

Then all you really need are the user _id values to set up a conversation. Here though you use the "multi" .update() method and the $addToSet operator:

User.update(
    { "_id": { "$in": [user1Id,user2Id] } },
    { "$addToSet": { "chattingTo": { "$each": [user1Id,user2Id] } } }
    { "multi": true },
    function(err,numAffected) {

    }
);

That essentially updates "both" of the supplied users at more or less the same time, at least in the single operation. The slight downside is that "user1" is listed as chatting to "user1" and the same goes for "user2". But that is not a difficult thing to filter when reading the document.

To end a conversation you can $pull from the array, which will remove the selection from both users:

User.update(
    { "_id": { "$in": [user1Id,user2Id] } },
    { "$pull": { "chattingTo": { "$in": [user1Id,user2Id] } } },
    { "multi": true },
    function(err,numAffected) {

    }
)

In the same way, all of the supplied users will be removed from "chattingTo" in each user document.

Note that the $each modifier is only available for $addToSet as of MongoDB 2.6 or greater, but since you are just getting started then this is the version you should be using. If not, then get hold of it.