课程预约系统适合mongodb的场景吗?
巴扎黑
巴扎黑 2017-05-02 09:25:40
0
1
701

有一个用户课程预约的项目,因为后端有node来做,所以mongodb是个很好的选择,可是因为课程预约系统存在 用户预约成功后,或取消预约后,会有课程预约信息的事务操作,我对mongo的了解是她不适合用在事务场景里,我对数据库的设计不是很了解,想请问课程预约这样的项目适合用mongodb吗?

巴扎黑
巴扎黑

reply all(1)
给我你的怀抱

MongoDB is not suitable for strong transaction scenarios, but many scenarios often do not require strong transactions, and can be replaced by eventual consistency. Many strong transactions can also be converted into MongoDB document atomicity through data model setting techniques to avoid strong transactions. You need to describe more clearly what kind of situation you are talking about so that you can judge whether it can be avoided.
For the course reservation scenario that I understand, it is not a strong scenario that requires MongoDB, but it is not a scenario that requires RDBMS. So to say whether MongoDB is suitable for use, it is recommended to analyze the specific situation in detail.
=== Updated on 2017.4.9 ===
According to the update in the comments, the following additional explanations are made:
The requirement of limiting the number of people can be solved using MongoDB’s document atomicity. As far as I understand the registration scenario, it is nothing more than recording who has signed up for which course, how many people have signed up in total, and controlling not to exceed the registration limit. Then the data structure of the course can be designed like this:

{
    _id: ObjectId("58e9f6cd58d2c10959c8619a"),
    title: "MongoDB入门教程",
    intro: "演示用",
    createdAt: ISODate("2017-04-09T17:00:00Z"),
    createdBy: {
        userId: 12345,
        name: "MongoDB中文社区"
        // 其他必要的信息
    },
    allowedAttendeeQty: 50, // 允许参与的人数
    reservations: {
        qty: 0, // 已预订人数
        attendees: [123, 456, 789] // 参与人员ID
    }
}

This course can be updated when someone signs up:

var result = db.class.findAndUpdate(
    query: {
        _id: ObjectId("58e9f6cd58d2c10959c8619a"), 
        qty: {$lte: 50}    // 保证已报名人数小于50时才报名
    },
    update: {
        $inc: {
            "reservations.qty": 1 // 报名人数+1
        },
        $push: {
            "reservations.attendees": 102  // 新报名人员ID
        }
    }
});

In other words, if you search for this course according to the course ID, if the number of applicants is less than the specified number, new applicants will be added, otherwise no changes will be made. So how to judge whether the registration is successful? findAndModifyBy default, the document before modification will be returned (the document after modification can also be returned). So:

if (result.reservations.qty < 50) {
    // 报名成功
} else {
    // 报名失败
}

Please refer to db.collection.findAndModify() for the usage of findAndModify的用法请参考db.collection.findAndModify()。MongoDB中对一个文档的修改具备原子性,所以不难发现update也可以很好地完成上述任务。关于updatefindAndUpdate. Modifications to a document in MongoDB are atomic, so it is not difficult to find that update can also complete the above tasks well. For comparisons between update and findAndUpdate, please refer to: Compares with update Method

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!