<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                [TOC] ## 數據庫操作 ### 創建數據庫 `use test` ### 展示所有數據庫 ``` > show dbs admin 0.000GB config 0.000GB local 0.000GB ``` ### 刪除數據庫 ``` use test db.dropDatabase() ``` ## 集合(表) ### 創建集合 demo ``` use test db.createCollection("runoob") ``` 創建帶參數集合,整個集合空間大小 6142800 KB, 文檔最大個數為 10000 個(超過會覆蓋舊數據) ``` db.createCollection("runoob", { capped : true, autoIndexId : true, size : 6142800, max : 10000 } ) ``` 插入時,自動創建集合 ``` db.runoob.insert({"name" : "菜鳥教程"}) ``` ### 列出集合 ``` show tables ``` ### 刪除集合 ``` use test db.集合名.drop() ``` ## 插入 ### 插入數據時,自動創建 在數據庫中添加記錄,如果集合不存在會自動創建 ``` //切換/創建庫 use demo2 //在表test 中插入數據,如果表不存在就添加 db.test.insert({"name":"123",tags: ['mongodb', 'database', 'NoSQL']}) ``` 如果不指定 \_id 字段 save() 方法類似于 insert() 方法。如果指定 \_id 字段,則會更新該 \_id 的數據 ### update 更新 ``` db.collection.update( <query>, <update>, { upsert: <boolean>, multi: <boolean>, writeConcern: <document> } ) 參數說明: query : update的查詢條件,類似sql update查詢內where后面的。 update : update的對象和一些更新的操作符(如$,$inc...)等,也可以理解為sql update查詢內set后面的 upsert : 可選,這個參數的意思是,如果不存在update的記錄,是否插入objNew,true為插入,默認是false,不插入。 multi : 可選,mongodb 默認是false,只更新找到的第一條記錄,如果這個參數為true,就把按條件查出來多條記錄全部更新。 writeConcern :可選,拋出異常的級別。 ``` ### update 更新一條 `db.col.update({'title':'MongoDB 教程'},{$set:{'title':'MongoDB'}})` 把 title等于 `MongoDB 教程` 更新為 `MongoDB` ### update 更新多條 `db.col.update({'title':'MongoDB 教程'},{$set:{'title':'MongoDB'}},{multi:true})` 更新數組中的數據 `db.friend.update({ "friends.group.userid":"222"}, {$set:{"friends.group.$.user_status":"p"}}, {multi:true})` ### save 數據更替 如果 save 中 `_id` 值一樣,就進行整體替換 ## 刪除 ``` db.collection.remove( <query>, { justOne: <boolean>, writeConcern: <document> } ) 參數說明: query :(可選)刪除的文檔的條件。 justOne : (可選)如果設為 true 或 1,則只刪除一個文檔,如果不設置該參數,或使用默認值 false,則刪除所有匹配條件的文檔。 writeConcern :(可選)拋出異常的級別。 ``` ### 刪多條 實例:移除title 為`MongoDB 教程`的數據 ``` >db.test.remove({'title':'MongoDB 教程'}) WriteResult({ "nRemoved" : 2 }) # 刪除了兩條數據 ``` ### 刪一條 ``` >db.test.remove({'title':'MongoDB 教程'},1) ``` ### 清空 ``` >db.test.remove() ``` ## 查詢 格式 ``` db.collection.find(query, projection) query :可選,使用查詢操作符指定查詢條件 projection :可選,使用投影操作符指定返回的鍵。查詢時返回文檔中所有鍵值, 只需省略該參數即可(默認省略)。 ``` ### projection #### inclusion 指定返回的鍵,不返回其他鍵 ``` db.test.find({}, {title: 1, url: 1}) //只返回 title,與url ``` #### exclusion模式 指定不返回的鍵,返回其他鍵 ``` db.test.find({}, {title: 0, url: 0}) //返回排除 title 與url ``` > inclusion 與 exclustion 不能混用 > _id 鍵默認返回,需要主動指定 _id:0 才會隱藏 `db.test.find({}, {title: 1, _id: 0}) // 只返回 title` ### where 語句 ``` db.col.find({"by":"菜鳥教程"}).pretty() => where by = '菜鳥教程' db.col.find({"likes":{$lt:50}}).pretty() => where likes < 50 可選參數 $lt 小于 $lte 小于等于 $gt 大于 $gte 大于等于 $ne 不等于 ``` ### and 條件 每個where 以逗號隔開 ``` >db.col.find({key1:value1, key2:value2}).pretty() ``` ### OR 條件 需要用到 $or 關鍵字 ``` db.col.find( { $or: [ {key1: value1}, {key2:value2} ] } ) ``` ### AND 和 OR 聯合使用 ``` db.col.find({ "likes": { $gt: 50 }, $or: [ { "by": "菜鳥教程" }, { "title": "MongoDB 教程" } ] }) ``` ### $type 查詢value 的指定類型 > [完整類型表 ](https://www.runoob.com/mongodb/mongodb-operators-type.html) | **類型** | **數字** | **備注** | | --- | --- | --- | | Double | 1 | ? | | String | 2 | ? | | Object | 3 | ? | | Array | 4 | ? | ... 測試數據 ``` >db.col.insert({ title: 'PHP 教程', }) >db.col.insert({ title:123456, }) ``` 只查詢title值為字符串的 ``` db.col.find({"title" : {$type : 2}}) 或 db.col.find({"title" : {$type : 'string'}}) ``` ### limit `db.col.find({},{"title":1,_id:0}).limit(2)` ### Skip `db.col.find({},{"title":1,_id:0}).limit(1).skip(1)` > skip()方法默認參數為 0 ### sort `db.col.find({},{"title":1,_id:0}).sort({"likes":-1})` > -1 倒敘, 1 順序,skip(), limilt(), > sort()三個放在一起執行的時候,執行的順序是先 sort(), 然后是 skip(),最后是顯示的 limit() ## createIndex 索引 語法 ``` db.collection.createIndex(keys, options) ``` Key 值為你要創建的索引字段,1 為指定按升序創建索引,如果你想按降序來創建索引指定為 -1 <details> <summary>options 參數</summary> | Parameter | Type | Description | | --- | --- | --- | | background | Boolean | 為true 不阻塞創建索引 默認值為**false**。 | | unique | Boolean | 唯一索引,默認值為**false**. | | name | string | 索引名,不指定自動生成 | | sparse | Boolean | 對文檔中不存在的字段數據不啟用索引;這個參數需要特別注意,如果設置為true的話,在索引字段中不會查詢出不包含對應字段的文檔.。默認值為**false**. | | expireAfterSeconds | integer | 指定一個以秒為單位的數值,完成 TTL設定,設定集合的生存時間。 | | v | index version | 索引的版本號 | | weights | document | 索引權重值,數值在 1 到 99,999 之間,表示該索引相對于其他索引字段的得分權重。 | | default\_language | string | 對于文本索引,該參數決定了停用詞及詞干和詞器的規則的列表。 默認為英語 | | language\_override | string | 對于文本索引,該參數指定了包含在文檔中的字段名,語言覆蓋默認的language,默認值為 language. | </details> <br/> 實例: ``` db.col.createIndex({"title":1,"description":-1}, {background: true}) ``` ``` 1、查看集合索引 db.col.getIndexes() 2、查看集合索引大小 db.col.totalIndexSize() 3、刪除集合所有索引 db.col.dropIndexes() 4、刪除集合指定索引 db.col.dropIndex("索引名稱") ```
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看