【发布时间】:2023-03-22 16:37:01
【问题描述】:
我想要实现的是将人们彼此匹配。 为了实现这种行为,我将 Match 文档嵌入到用户的匹配数组中。
这是我的User 模特
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]
});
但对于可靠的应用程序逻辑, 用户不得匹配两次。我的计划是将匹配的人一起插入到他们的匹配数组中,
例如;
User A
_id: 123
matches: [{with: 456,near:California,createdAt:someDateTime}]
User B
_id: 456
matches: [{with: 123,near:California,createdAt:someDateTime}]
在匹配发生之前,我需要控制之前是否匹配过任何用户,并且必须是atomic。
谢谢。
【问题讨论】:
标签: javascript node.js mongodb express mongoose