Questions: how to design a collection in the following scenarios

Source: Internet
Author: User
Tags findone
Collection in Mongo is equivalent to a MySQL table. When I have the following requirements, how should I design my collection and fields (keyvalue structure )? Scenario and Requirement Description: record the user's Business ID and ip address for each login, as well as the specified qid for the login time, and check whether the ip address already exists.

Collection in Mongo is equivalent to a MySQL table. When I have the following requirements, how should I design my collection and fields (key/value structure )? Scenario and Requirement Description: record the user's Business ID and ip address for each login, as well as the specified qid for the login time, and check whether the ip address already exists.

Collection in Mongo is equivalent to a MySQL table. When I have the following requirements, how should I design my collection and fields (key/value structure )?

Scenario and Requirement Description:

  1. Record the Business ID, ip address, and logon time of each user login
  2. Check whether the specified qid and ip address already exist.

In response to the above requirements, how should I design my collection?

There are currently two solutions that are struggling with which is better:

  • Solution 1
  • [Cc lang = "javascript"]
    {'Userid': $ userid, 'appid ': $ appid, 'IP': $ ip, 'logintime': timestamp}
    [/Cc]

  • Solution 2
  • [Cc lang = "javascript"]
    {'Userid': $ userid,
    'Appid ': $ appid,
    'Iparr ':{
    {'IP': $ ip1, 'logintime': $ timestamp1 },
    {'IP': $ ip2, 'logintime': $ timestamp2}
    }
    }
    [/Cc]

The first solution is clear and multi-to-many relationships. Clear enough! The relative query conditions should be simple enough: findOne ({userid: $ userid, appid: $ appid, ip: $ ip });

The second solution is a one-to-many relationship. It is difficult to specify conditions for query! FindOne ({userid: $ userid, appid: $ appid, ipArr. ip: $ ip });

Note: The query statements mentioned above may be inaccurate (just getting started with Mongo)

So which one should I choose? How can we create an appropriate index?

Don't worry, now you need to add a new requirement: each user and the same service only records the latest 5 Records!

Well, I'm not sure how to choose a solution or how to implement the last requirement. I will discuss with my colleagues tomorrow and update this log Mark!
PS: Is it better to use PHP to limit the number of entries?

Finally, we decided to adopt the second solution to obtain the user ip array through the PHP client, use PHP to add and delete array elements, and then upsert to Mongo.

The query conditions for using php can be as follows (depending on what you want, the actual implementation of userid and appid can limit the conditions)

[Cc lang = "php"]
Var_dump ($ monObj-> remove (array ('userid' => '123', 'appid '=> 'abc ', 'iparr' => array ('$ elemmatch' => array ('IP' =>' 127. 0.0.0.1 ′)))));
[/Cc]

Original article address: question: how to design the mongo collection in the following scenarios, thanks to the original author for sharing.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.