Mongo 中collection相当于MySQL的表,那么当我有下面需求时,我应该如何设计我的collection及字段(key/value结构)? 场景及需求描述: 记录用户每次登录的业务标识及ip,以及登录时间 指定qid、ip需要查询该ip是否已经存在 针对上述需求,我的collection应
Mongo 中collection相当于MySQL的表,那么当我有下面需求时,我应该如何设计我的collection及字段(key/value结构)?
场景及需求描述:
- 记录用户每次登录的业务标识及ip,以及登录时间
- 指定qid、ip需要查询该ip是否已经存在
针对上述需求,我的collection应该如何设计?
目前有两种方案,正在纠结于哪个更好一些:
- 方案一
- 方案二
[cc lang="javascript"]
{‘userid’:$userid,’appid’:$appid,’ip’:$ip,’logintime’:timestamp}
[/cc]
[cc lang="javascript"]
{‘userid’:$userid,
‘appid’:$appid,
‘ipArr’:{
{‘ip’:$ip1,’logintime’:$timestamp1},
{‘ip’:$ip2,’logintime’:$timestamp2}
}
}
[/cc]
第一种方案,清晰,多对多的关系。足够的明朗!相对查询条件应该足够的简单:findOne({userid:$userid,appid:$appid,ip:$ip});
第二种方案,一对多的关系,查询起来指定条件比较麻烦!findOne({userid:$userid,appid:$appid,ipArr.ip:$ip});
注明:上述说的查询语句可能不准确(刚接触Mongo)
那么到底应该选择哪种?又如何来创建合适的索引?
别急,现在增加一个新的需求:每个用户,同一业务,只记录最新的5条记录!
好吧,我现在还不确定方案如何选择,以及最后一个需求具体该如何实现。明天和同事们讨论,更新本日志Mark一下!
PS:是不是使用PHP来做条数限制会更好一些?
最终决定采用第二种方案,实现上通过PHP的client端获取用户ip数组,使用PHP来操作数组元素的增加及删除,最后再upsert至Mongo中。
使用php的查询条件可以如下(取决于你想取什么,实际实现上userid及appid即可限制条件了)
[cc lang="php"]
var_dump($monObj->remove(array(‘userid’=>’1234567′,’appid’=>’abc’,'ipArr’=>array(‘$elemMatch’=>array(‘ip’=>’127.0.0.0.1′)))));
[/cc]
原文地址:思考题:如下场景如何设计mongo collection, 感谢原作者分享。

MySQLviewshavelimitations:1)Theydon'tsupportallSQLoperations,restrictingdatamanipulationthroughviewswithjoinsorsubqueries.2)Theycanimpactperformance,especiallywithcomplexqueriesorlargedatasets.3)Viewsdon'tstoredata,potentiallyleadingtooutdatedinforma

ProperusermanagementinMySQLiscrucialforenhancingsecurityandensuringefficientdatabaseoperation.1)UseCREATEUSERtoaddusers,specifyingconnectionsourcewith@'localhost'or@'%'.2)GrantspecificprivilegeswithGRANT,usingleastprivilegeprincipletominimizerisks.3)

MySQLdoesn'timposeahardlimitontriggers,butpracticalfactorsdeterminetheireffectiveuse:1)Serverconfigurationimpactstriggermanagement;2)Complextriggersincreasesystemload;3)Largertablesslowtriggerperformance;4)Highconcurrencycancausetriggercontention;5)M

Yes,it'ssafetostoreBLOBdatainMySQL,butconsiderthesefactors:1)StorageSpace:BLOBscanconsumesignificantspace,potentiallyincreasingcostsandslowingperformance.2)Performance:LargerrowsizesduetoBLOBsmayslowdownqueries.3)BackupandRecovery:Theseprocessescanbe

Adding MySQL users through the PHP web interface can use MySQLi extensions. The steps are as follows: 1. Connect to the MySQL database and use the MySQLi extension. 2. Create a user, use the CREATEUSER statement, and use the PASSWORD() function to encrypt the password. 3. Prevent SQL injection and use the mysqli_real_escape_string() function to process user input. 4. Assign permissions to new users and use the GRANT statement.

MySQL'sBLOBissuitableforstoringbinarydatawithinarelationaldatabase,whileNoSQLoptionslikeMongoDB,Redis,andCassandraofferflexible,scalablesolutionsforunstructureddata.BLOBissimplerbutcanslowdownperformancewithlargedata;NoSQLprovidesbetterscalabilityand

ToaddauserinMySQL,use:CREATEUSER'username'@'host'IDENTIFIEDBY'password';Here'showtodoitsecurely:1)Choosethehostcarefullytocontrolaccess.2)SetresourcelimitswithoptionslikeMAX_QUERIES_PER_HOUR.3)Usestrong,uniquepasswords.4)EnforceSSL/TLSconnectionswith

ToavoidcommonmistakeswithstringdatatypesinMySQL,understandstringtypenuances,choosetherighttype,andmanageencodingandcollationsettingseffectively.1)UseCHARforfixed-lengthstrings,VARCHARforvariable-length,andTEXT/BLOBforlargerdata.2)Setcorrectcharacters


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

Zend Studio 13.0.1
Powerful PHP integrated development environment

SublimeText3 Chinese version
Chinese version, very easy to use

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software
