小张别慌 發表於 2022-6-24 13:56:00

MongoDB-SQL语法

<p>MongoDB-SQL语法</p>
<p>可视化软件:Navicat</p>
<p>1. MongoDB-查询</p>
<p>db.getCollection('表名').find({});<br> <br>db.getCollection('表名').find({"_id":1});</p>
<p><br>2. MongoDB-NE(NOT EQUAL)查询</p>
<p>db.getCollection('表名').find({"_id":{$ne:1}})</p>
<p><br>3. MongoDB-IN查询</p>
<p>db.getCollection('表名').find({"_id":{$in:}});</p>
<p><br>4. MongoDB-NOT_IN查询</p>
<p>db.getCollection('表名').find({"_id":{$nin:}})</p>
<p><br>5. MongoDB-EXISTS查询</p>
<p>db.getCollection('表名').find({"字段名":{$exists:true}});</p>
<p>当boolean为true,$exists匹配包含字段的文档,包括字段值为null的文档。</p>
<p>当boolean为false,$exists返回不包含对应字段的文档。</p>
<p>&nbsp;</p>
<p>6. MongoDB-大于小于查询</p>
<p>db.getCollection('表名').find({"_id":{$gt:1}})<br>(&gt;)---大于---$gt<br>(&lt;)---小于---$lt<br>(&gt;=)---大于等于---$gte<br>(&lt;=)---小于等于---$lte</p>
<p><br>7. MongoDB-大于小于日期查询</p>
<p>Date方式<br>db.getCollection('表名').find({"时间字段":{$gte:new Date(2000,1,1)},"时间字段":{$lte:new Date(2030,1,1)}});<br>ISODate方式<br>db.getCollection('dictValue').find({"createTime":{$gte:ISODate("2000-01-01T00:00:00Z")},"createTime":{$lte:ISODate("2030-01-01T00:00:00Z")}});</p>
<p><br>8. MongoDB-模糊匹配</p>
<p>db.表名.find({"字段名": {$regex:'要模糊匹配的字符',$options:'i'}});<br>$options: '&lt;options&gt;':选项    含义<br>i===&gt;大小写不敏感<br>m===&gt;查询匹配中使用了锚,例如^(代表开头)和$(代表结尾),以及匹配n后的字符串<br>x===&gt;忽视所有空白字符<br>s===&gt;允许点字符(.)匹配所有的字符,包括换行符</p>
<p>&nbsp;</p>
<p>9.分页与排休查询</p>
<p>//条件查排序并分页:1.是升序, -1是降序;2-页码,10-每页条数<br>db.getCollection('order').find().sort({"payTime":-1}).limit(2,10);</p>
<p>&nbsp;</p>
<p>10. MongoDB-修改-所有匹配的数据</p>
<p>db.getCollection('表名').update({"字段名" : "原字段值"},{$set:{"字段名" : "新字段值"}},{multi:true});</p>
<p><br>11. MongoDB-新增数据</p>
<p>db.getCollection('表名').save({"_id":NumberLong(1)});</p>
<p><br>12. MongoDB-删除数据</p>
<p>db.getCollection('表名').remove({});<br>db.getCollection('表名').remove({"_id":1});</p>
<p><br>13. MongoDB-两个字段比较</p>
<p>常规查询:<br>db.getCollection('表名').find({$expr:{$gt:["$approve_create", "$approve_delete"]}})<br>聚合查询:<br>db.getCollection('表名').aggregate({$match:{$expr:{$gt:["$approve_create", "$approve_delete"]}}})<br>$gt -------- greater than&gt;<br>$gte --------- gt equal&gt;=<br>$lt -------- less than&lt;<br>$lte --------- lt equal&lt;=<br>$ne ----------- not equal!=<br>$eq--------equal=</p>
<p>&nbsp;</p>
<p>14.MongoDB-联表查询,查询总数并分页</p>
<p>联表查询总数:</p>
<p>db.getCollection("表名").aggregate([<br>        { "$match" : { "userType" : 4.0 }&nbsp;}, <br>        { "$group" : {&nbsp; &nbsp;//分组查询,分组查询后的结果集中只有下面的字段<br>                    "_id" : "$userId",&nbsp; //根据userId来分组,分组后userId就是结果集的_id<br>                    "count" : { "$sum" :"$isDownLoad" }, //分组计算总数<br>                                                                    "mobile" : { "$last" : "$mobile" }, <br>                    "time" : {"$last" : "$inviteTime" }, <br>                    "userId" : { "$last" : "$userId" }, <br>                    "userType" : { "$last" : "$userType" }<br>              }&nbsp;}, <br>        {"$match" : {"count" : { "$gt" : 0.0 }}&nbsp;&nbsp;},&nbsp;&nbsp;//分组后再过滤筛选,只要总数大于0的<br>        { "$lookup" : { <br>                    "from" : "联表查询的表名",&nbsp; //和上面过滤筛选出来的结果集进行联表操作<br>                    "localField" : "userId", //当前联表的关联字段<br>                    "foreignField" : "_id", //上面查询结果的临时表的关联字段<br>                    "as" : "user" //给当前联表取别名为user<br>              }&nbsp;}, <br>        { "$unwind" : { <br>                    "path" : "$user", <br>                    "includeArrayIndex" : "1index", <br>                    "preserveNullAndEmptyArrays" : true<br>              }&nbsp;}, <br>        { "$match" : {"user.provinceId": {"$eq":"520000"}} },&nbsp; //联表后在过滤筛选数据<br>                                  { "$count" : "total" }&nbsp; //查询总数<br>    ])</p>
<p>联表分页查询:</p>
<p>db.getCollection("表名").aggregate([<br>        {"$match" : { "userType" : 4.0}}, <br>        { "$group" : { <br>                    "_id" : "$userId", <br>                    "count" : { "$sum" : "$isDownLoad"}, <br>                    "mobile" : { "$last" : "$mobile" }, <br>                    "time" : {"$last" : "$inviteTime" }, <br>                    "userId" : { "$last" : "$userId" }, <br>                    "userType" : { "$last" : "$userType" }<br>              }}, <br>        { "$match" : { "count" : { "$gt" : 0.0 }}}, <br>                    {"$lookup" : { <br>                    "from" : "联表查询的表名", <br>                    "localField" : "userId", <br>                    "foreignField" : "_id", <br>                    "as" : "lp"<br>               }&nbsp;}, <br>                                 { "$unwind" : { <br>                    "path" : "$lp", <br>                    "includeArrayIndex" : "1index", <br>                    "preserveNullAndEmptyArrays" : true<br>              }&nbsp;}, <br>        { "$match" : { <br>                    "lp.provinceId" : "520000" , <br>                    "lp.cityId" :"520100"<br>              }&nbsp;}, <br>                                  {&nbsp;"$project": {"mobile" : "$mobile","time":"$time","userId":"$userId","userType":"$userType","count":1,"address":'$lp.address'}&nbsp;},&nbsp; //最后结果集只需要的字段数据<br>        {"$sort" : { "time" : -1.0 }},&nbsp; //排序<br>        { "$skip" : 20.0 },&nbsp; &nbsp;//跳过前面20条数据,从第21条数据开始收集<br>  { "$limit" : 10.0 }&nbsp; &nbsp;//只收集10条数据<br>    ], { "allowDiskUse" : false }&nbsp;);</p>
<p>&nbsp;</p>
<p> 15.MongoDB-加索引</p>
<p>db.表名.ensureIndex({"字段1":1,"字段2":1,"字段3":1})<br><br></p>
<p>&nbsp;</p><br><br>
来源:https://www.cnblogs.com/luolei0120/p/16408596.html
頁: [1]
查看完整版本: MongoDB-SQL语法