標籤:tle 擷取 dex opera missing name 分組 字元 pretty
MongoDB
ps 好多的MongoDB都沒有設密碼,預設的MongoDB都是不設定密碼的,暈
$type運算子
查詢類型用的
擷取col集合中的title值為字串類型的文檔
db.col.find(title: {$type: 2});
limit 和 skip方法limit查詢指定的方法
> db.ming.find().pretty();{ "_id" : ObjectId("5b5f6eb7d595bb04cbe14b96"), "ming" : "sdfrf" }{ "_id" : ObjectId("5b5f6f0ad595bb04cbe14b97"), "name" : "refgerfg" }{ "_id" : ObjectId("5b5f6f40d595bb04cbe14b98"), "x" : 222, "y" : 44444 }{ "_id" : ObjectId("5b5f6f50d595bb04cbe14b99"), "x" : 4444, "y" : 22222 }> db.ming.find().limit(2);{ "_id" : ObjectId("5b5f6eb7d595bb04cbe14b96"), "ming" : "sdfrf" }{ "_id" : ObjectId("5b5f6f0ad595bb04cbe14b97"), "name" : "refgerfg" }
skip是跳過第幾條資料排序
> db.ming.find({}, {ming:1, name:1}).sort({_id:-1});{ "_id" : ObjectId("5b5f6f50d595bb04cbe14b99") }{ "_id" : ObjectId("5b5f6f40d595bb04cbe14b98") }{ "_id" : ObjectId("5b5f6f0ad595bb04cbe14b97"), "name" : "refgerfg" }{ "_id" : ObjectId("5b5f6eb7d595bb04cbe14b96"), "ming" : "sdfrf" }> db.ming.find({}.{ming:1, name:1}).sort({id:1});2018-07-31T19:49:11.007+0800 E QUERY [js] SyntaxError: missing name after . operator @(shell):1:16> db.ming.find({}, {ming:1, name:1, x:1, y:1}).sort({ _id:1});{ "_id" : ObjectId("5b5f6eb7d595bb04cbe14b96"), "ming" : "sdfrf" }{ "_id" : ObjectId("5b5f6f0ad595bb04cbe14b97"), "name" : "refgerfg" }{ "_id" : ObjectId("5b5f6f40d595bb04cbe14b98"), "x" : 222, "y" : 44444 }{ "_id" : ObjectId("5b5f6f50d595bb04cbe14b99"), "x" : 4444, "y" : 22222 }>
索引
> db.ming.createIndex({ming:1});{ "createdCollectionAutomatically" : false, "numIndexesBefore" : 1, "numIndexesAfter" : 2, "ok" : 1}>
ming欄位按照升序建立序列
彙總
類似於sql的count()
db.ming.aggregate( [ ( $groupp: { _id : "$by-user", num_tutorial: { $sum: 1 } } ) ] )
按照_id進行分組,然後對分組的文檔進行統計,即對分組的文檔總和統計。
管道
> db.ming.aggregate(... {... $project : {... _id:0,... x:1,... y:1... }... }... ){ }{ }{ "x" : 222, "y" : 44444 }{ "x" : 4444, "y" : 22222 }
一個值處理完成以後在傳給下一個進行處理
即,值經過管道以後,會變成相應的格式
隨機擷取文檔
db.ming.aggregate( [ { $sample: { size:1 } } ] )
採用彙總,使用$sample來達到目的,其中size為隨機擷取的條數
sql隨機擷取兩條記錄
select * from dataorder by rand()limit 2
mysql> select * from c order by rand() limit 2;+--------+------------------------+-------+| cno | cn | hourc |+--------+------------------------+-------+| 050192 | 專業英語 | 32 || 090112 | | 48 |+--------+------------------------+-------+2 rows in set (0.10 sec)mysql> select * from c order by rand() limit 2;+--------+----------------------+-------+| cno | cn | hourc |+--------+----------------------+-------+| 050198 | 電腦網路安全與應用 | 32 || 050150 | 電腦群組裝與維護 | 36 |+--------+----------------------+-------+2 rows in set (0.03 sec)mysql>
剩下的複製,看不下去了。
mongodb(2)