MongoDB學習筆記(一)--基礎


Insert                                                                                       

MongoDB在執行插入時,首先會將插入的數據轉換成BSON格式。然后MongoDB數據庫會對BSON進行解剖,並檢查是否存在_id建。

>doc = {
    "_id" : 1,
    "author" : "yyd",
    "title" : "MongoDB Test",
    "text" : "this is a test",
    "tags" : [
    "love",
    "test"
    ],
    "comments" : [
    {
        "author" : "yyd_guest",
        "comment" : "yes"
    },
    {
        "author" : "yyd_admin",
        "comment" : "no"
    }
    ]
}
> db.yyd.insert(doc);

 

Query                                                                                       

全部查找

1

返回除了 tags 字段外的所有字段

1

返回 tags = test 除了 comments 的所有列

1

返回 id=1 的 title 字段

1

  • <, <=, >, >=

image

大於 $gt、小於 $lt、大於等於 $gte、小於等於 $lte

  • $all

$all 操作類似$in 操作,但是不同的是,$all 操作要求數組里面的值全部被包含在返回的記錄里面。

image

  • $exists

$exists 操作檢查一個字段是否存在。

image

$exists:true代表返回存在這個鍵的值。

$exists:false代表返回不存在這個鍵的值。

  • $mod
> db.user.find("this._id%2==1");
> db.user.find({_id:{$mod:[2,1]}});

兩句話一樣的效果。

  • $ne

$ne 意思是 not equal,不等於。

image

  • $in

$in 操作類似於傳統關系數據庫中的 IN。

image

  • $nin

$nin 跟$in 操作相反。

  • $or

image

  • $nor

$nor 跟$or 相反。

  • $size

$size 操作將會查詢數組長度等於輸入參數的數組。

image

  • skip

跳過前 2 條記錄。

image

  • limit

每頁返回 3 條記錄

image

  • sort()

sort()方法對返回記錄集按照指定字段進行排序返回,1 表示升序,-1 表示降序。

image

  • count()

count()方法返回查詢記錄的總數目。

image

 

Remove                                                                                     

image

 

Update                                                                                     

  • update()
db.collection.update( criteria, objNew, upsert, multi )

參數說明:

Criteria:用於設置查詢條件的對象

Objnew:用於設置更新內容的對象

Upsert:如果記錄已經存在,更新它,否則新增一個記錄

Multi:如果有多個符合條件的記錄,全部更新

注意:默認情況下,只會更新第一個符合條件的記錄

  • save()

如果存在更新它,如果不存在,新增記錄。

image

  • $inc

增加1,對int等有效。

對一個_id=3 的 user 的年齡進行加 1,兩種方法。

image

image

  • $set
{ $set : { field : value } }

把 field 的值設置成 value,當 field 不存在時,增加一個字段,類似 SQL 的 set 操作,value 支持所有類型。

  • $unset
{ $unset : { field : 1} }

      刪除給定的字段 field。

  • $push
{ $push : { field : value } }

如果 filed 是一個已經存在的數組,那么把 value 追加給 field;

如果 field 原來不存在,那么新增 field 字段,把 value 的值賦給 field;

如果 field 存在,但是不是一個數組,將會出錯。

  • $pushAll
{ $pushAll : { field : value_array } }

功能同$push,只是這里的 value 是數組,相當於對數組里的每一個值進行$push操作。

  • $addToSet
{ $addToSet : { field : value } }

如果 filed 是一個已經存在的數組,並且 value 不在其中,那么把 value 加入到數組;

如果 filed 不存在,那么把 value 當成一個數組形式賦給 field;$pop

如果 field 是一個已經存在的非數組類型,那么將會報錯。

  • $pop
{ $pop : { field : 1 } }

刪除數組中最后一個元素

{ $pop : { field : -1 } }

刪除數組中第一個元素

  • $pull
{ $pull : { field : _value } }

如果 field 是一個數組,那么刪除符合_value 檢索條件的記錄;

如果 field 是一個已經存在的非數組,那么會報錯。

  • $pullAll
{ $pullAll : { field : value_array } }
  • $rename
{ $rename : { old_field_name : new_field_name }

重命名指定的字段名稱。

 

 

轉載請注明出處:http://www.cnblogs.com/yydcdut/p/3557414.html


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM