What i'm trying to achieve is matching people with each other. To achieve this behavior, i'm embedding Match document to user's matches array.
Heres my User
Model
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const Match = new Schema({
with: {type: Schema.Types.ObjectId, ref: 'User'},
near: {type:String},
createdAt: {type:Date, default: Date.now}
});
const UserSchema = new Schema({
name: { type: String, default: '' },
surname: { type: String, default: '' },
email: { type: String, default: '', },
salt: { type: String, default: '' },
hashed_password: { type: String, default: '' },
interested: { type: [String] },
about: { type: String },
createdAt: {type:Date, default:Date.now},
devices: {type: [String]},
matches: [Match]
});
But for a reliable application logic, The users must not be matched twice. My plan is inserting matched people inside their matches array together,
for example;
User A
_id: 123
matches: [{with: 456,near:California,createdAt:someDateTime}]
User B
_id: 456
matches: [{with: 123,near:California,createdAt:someDateTime}]
Before a match occurs, i need to control whether any of the users matched before or not and it must be atomic
.
Thanks.
Maybe this works:
const Match = new Schema({
matchedIds: [ {type:string} ]
near: {type:String},
createdAt: {type:Date, default: Date.now}
});
So matchedIds
just contains the two userIDs. You can find the match then with:
db.match.find( { matchedIds: { $all: ["UserID 1", "UserID 2"] } } )
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.