search

Home  >  Q&A  >  body text

Is the course reservation system suitable for mongodb scenarios?

There is a project for user course reservation. Because the backend has node to do it, mongodb is a good choice. However, because the course reservation system has a transaction of course reservation information after the user successfully makes a reservation or cancels the reservation. Operation, my understanding of mongo is that it is not suitable for use in transaction scenarios. I don’t know much about database design. I would like to ask whether mongodb is suitable for projects such as course reservations?

巴扎黑巴扎黑2798 days ago752

reply all(1)I'll reply

  • 给我你的怀抱

    给我你的怀抱2017-05-02 09:27:40

    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

    reply
    0
  • Cancelreply