MongoDB 101

作者: xiaofudeng | 来源:发表于2017-11-16 22:09 被阅读0次

    MongoDB 101

    参考

    Documents

    MongoDB中存储的数据称为document. documentjson对象类似.
    示例:

    {
       "_id" : ObjectId("54c955492b7c8eb21818bd09"),
       "address" : {
          "street" : "2 Avenue",
          "zipcode" : "10075",
          "building" : "1480",
          "coord" : [ -73.9557413, 40.7720266 ]
       },
       "borough" : "Manhattan",
       "cuisine" : "Italian",
       "grades" : [
          {
             "date" : ISODate("2014-10-01T00:00:00Z"),
             "grade" : "A",
             "score" : 11
          },
          {
             "date" : ISODate("2014-01-16T00:00:00Z"),
             "grade" : "B",
             "score" : 17
          }
       ],
       "name" : "Vella",
       "restaurant_id" : "41704620"
    }
    

    Collections

    MongoDBdocuments存储在collections中. Collections类似于关系数据库中的table. 但是Collection不要求其中的document有相同的结构.

    存储在Collection中的document必须有一个_id字段, 作为其的primary key.

    Import Example Dataset

    primer-dataset.json中下载这个数据集合, 另存为primer-dataset.json.

    使用mongoimport命令导入该数据集:

    mongoimport --db test --collection restaurants --drop --file ~/downloads/primer-dataset.json
    

    运行结果:

    2017-11-16T20:17:47.487+0800 I NETWORK  [thread1] connection accepted from 127.0.0.1:34806 #2 (1 connection now open)
    2017-11-16T20:17:47.489+0800    connected to: localhost
    2017-11-16T20:17:47.489+0800    dropping: test.restaurants
    2017-11-16T20:17:47.489+0800 I COMMAND  [conn2] CMD: drop test.restaurants
    2017-11-16T20:17:47.531+0800 I NETWORK  [thread1] connection accepted from 127.0.0.1:34808 #3 (2 connections now open)
    2017-11-16T20:17:48.569+0800    imported 25359 documents
    2017-11-16T20:17:48.569+0800 I -        [conn2] end connection 127.0.0.1:34806 (2 connections now open)
    2017-11-16T20:17:48.569+0800 I -        [conn3] end connection 127.0.0.1:34808 (2 connections now open)
    

    Insert a Document

    进入mongoDB shell之后, 切换到test数据库.

    use test
    

    插入数据:

    db.restaurants.insert(
       {
          "address" : {
             "street" : "2 Avenue",
             "zipcode" : "10075",
             "building" : "1480",
             "coord" : [ -73.9557413, 40.7720266 ]
          },
          "borough" : "Manhattan",
          "cuisine" : "Italian",
          "grades" : [
             {
                "date" : ISODate("2014-10-01T00:00:00Z"),
                "grade" : "A",
                "score" : 11
             },
             {
                "date" : ISODate("2014-01-16T00:00:00Z"),
                "grade" : "B",
                "score" : 17
             }
          ],
          "name" : "Vella",
          "restaurant_id" : "41704620"
       }
    )
    

    返回值:

    WriteResult({ "nInserted" : 1 })
    

    如果传递给insert()document中不包含_id字段, 那么mongo shell会自动设置这个字段.

    Query for Documents

    Query for all

    不带任何参数的find()将会返回全部documents.

    db.restaurants.find()
    

    Specify Equality Conditions

    { <field1>: <value1>, <field2>: <value2>, ... }
    

    如果<field>top-level field, 而且不是一个嵌套的document的字段, 或者数组的字段, 那么字段名可以用引号括起来, 也可以省略引号.

    如果<field>在一个嵌套的document里面或者在一个数组中, 可以使用dot notation来访问该字段, 此时引号是必须的.

    Query by a Top Level Field

    以下查询语句返回borough字段为Manhattandocuments.

    db.restaurants.find( { "borough": "Manhattan" } )
    

    Query by a Field in an Embedded Document

    这个时候引号是必须的:

    db.restaurants.find( { "address.zipcode": "10075" } )
    

    Query by a Field in an Array

    grades数组包含了嵌套的documents作为它的元素. 如果要指定一个查询条件在这些documents的字段上, 那么需要使用dot notation.

    下面这条查询语句用于查找grades数组中包含了grade字段为B的所有documents.

    db.restaurants.find( { "grades.grade": "B" } )
    

    Specify Conditions with Operators

    { <field1>: { <operator1>: <value1> } }
    

    $gt 大于

    db.restaurants.find( { "grades.score": { $gt: 30 } } )
    

    $lt 小于

    db.restaurants.find( { "grades.score": { $lt: 10 } } )
    

    Logical AND

    如果需要匹配多个conditions, 那么直接用逗号分隔开每个condition document即可.

    db.restaurants.find( { "cuisine": "Italian", "address.zipcode": "10075" } )
    

    Logical OR

    使用$or连接多个conditions:

    db.restaurants.find(
       { $or: [ { "cuisine": "Italian" }, { "address.zipcode": "10075" } ] }
    )
    

    Sort Query Results

    直接将sort()添加到query后面. 然后在sort()中传入用于指定排序的document. 其中包含了用于排序的fields, 和对应的排序类型(升序 1, 降序 -1).

    以下命令对restaurants的查询结果首先按照borough字段升序排序, 然后在每一个borough中按照address.zipcode字段升序排列.

    db.restaurants.find().sort( { "borough": 1, "address.zipcode": 1 } )
    

    Update Data

    update()参数:

    • 一个filter document用于匹配需要被修改的documents
    • 一个update document用于指定需要进行的修改
    • 一个options parameter, 这是可选的参数

    filter document的结构和语法同query conditions.
    默认情况下, update()仅修改一个document. 使用multi option来更新所有匹配的documents.

    _id字段是无法进行修改的.

    Update Specific Fields

    有一些如$set的操作符会在某个field不存在的时候创建该field.

    Update Top-Level Fields

    db.restaurants.update(
        { "name" : "Juni" },
        {
          $set: { "cuisine": "American (New)" },
          $currentDate: { "lastModified": true }
        }
    )
    

    currentDate

    { $currentDate: { <field1>: <typeSpecification1>, ... } }
    

    <typeSpecification> can be either:

    • a boolean true to set the field value to the current date as a Date, or
    • a document { $type: "timestamp" } or { $type: "date" } which explicitly specifies the type. The operator is case-sensitive and accepts only the lowercase "timestamp" or the lowercase "date".

    修改后的对象:

    {
        "_id" : ObjectId("5a0d81eb80cddb51a870feb7"),
        "address" : {
            "building" : "12",
            "coord" : [
                -73.9852329,
                40.745971
            ],
            "street" : "East 31 Street",
            "zipcode" : "10016"
        },
        "borough" : "Manhattan",
        "cuisine" : "American (New)",
        "grades" : [
            {
                "date" : ISODate("2014-09-19T00:00:00Z"),
                "grade" : "A",
                "score" : 12
            },
            {
                "date" : ISODate("2013-08-05T00:00:00Z"),
                "grade" : "A",
                "score" : 5
            },
            {
                "date" : ISODate("2012-06-07T00:00:00Z"),
                "grade" : "A",
                "score" : 0
            }
        ],
        "name" : "Juni",
        "restaurant_id" : "41156888",
        "lastModified" : ISODate("2017-11-16T13:04:49.640Z")
    }
    
    

    Update an Embedded Filed

    db.restaurants.update(
      { "restaurant_id" : "41156888" },
      { $set: { "address.street": "East 31st Street" } }
    )
    

    Update Multiple Documents

    将匹配address.zipcode == 10016, cuisine == Otherdocumentcuisine设置为Category To Be Determined, lastModified设置为当前时间.

    db.restaurants.update(
      { "address.zipcode": "10016", cuisine: "Other" },
      {
        $set: { cuisine: "Category To Be Determined" },
        $currentDate: { "lastModified": true }
      },
      { multi: true}
    )
    

    Replace a Document

    _id不能被替换. 传递一个全新的document作为第二个参数. 因为Collection中的document是没有固定的Schema的. 所以新的document可以有不用于之前documentfields.

    如果新的document中有_id字段, 那么必须和原来的一样; 或者直接不要带上_id.

    update之后, 这个document仅包含第二个参数document的字段了.

    db.restaurants.update(
       { "restaurant_id" : "41704620" },
       {
         "name" : "Vella 2",
         "address" : {
                  "coord" : [ -73.9557413, 40.7720266 ],
                  "building" : "1480",
                  "street" : "2 Avenue",
                  "zipcode" : "10075"
         }
       }
    )
    

    如果update操作没有匹配任何一条数据, 那么默认update什么也不会做. 可以指定upsert option为true, 使得在这种情况下, update直接创建一个新的document.

    MongoDB中, 写操作是原子性的, 但是仅仅是对于单个document. 如果一个update操作会修改多个documents, 那么这些操作将会和其他对这个collection的写操作交错进行.

    Remove Data

    Remove All Documents That Match a Condition

    db.restaurants.remove( { "borough": "Manhattan" } )
    

    justOne Option

    使用justOne: true选项使得仅移除一个匹配的documents.

    db.restaurants.remove( { "borough": "Queens" }, { justOne: true } )
    

    Remove All Documents

    db.restaurants.remove( { } )
    

    Drop a Collection

    remove仅仅移除collection中的document. 如果需要移除collection本身, 以及它的indexes等, 直接用下面语句即可:

    db.restaurants.drop()
    

    Data Aggregation

    The aggregate() method accepts as its argument an array of stages, where each stage, processed sequentially, describes a data processing step.

    db.collection.aggregate( [ <stage1>, <stage2>, ... ] )
    

    Group Documents by a Field and Calculate Count

    使用$group stage来通过key聚合数据. 通过_id来指定$group stage用到的field. $group通过field path来访问fields, 格式就是在field名字前加上美元$符号.

    The following example groups the documents in the restaurants collection by the borough field and uses the $sum accumulator to count the documents for each group.

    db.restaurants.aggregate(
       [
         { $group: { "_id": "$borough", "count": { $sum: 1 } } }
       ]
    );
    

    结果:

    { "_id" : "Missing", "count" : 51 }
    { "_id" : "Staten Island", "count" : 969 }
    { "_id" : "Brooklyn", "count" : 6086 }
    { "_id" : "Bronx", "count" : 2338 }
    { "_id" : "Queens", "count" : 5656 }
    { "_id" : "Manhattan", "count" : 10260 }
    

    The _id field contains the distinct borough value, i.e., the group by key value.

    Filter and Group Documents

    db.restaurants.aggregate(
       [
         { $match: { "borough": "Queens", "cuisine": "Brazilian" } },
         { $group: { "_id": "$address.zipcode" , "count": { $sum: 1 } } }
       ]
    );
    

    先用$match挑出后续$group stage的操作对象. $match的语法同query的语法.

    Indexes

    如果没有Indexes, MongoDB必须扫描整个collection来匹配query statement.

    createIndex()用于在collection上建立索引. MongoDB自动对_id字段创建索引 (在这个collection建立时).

    创建Indexes的语法: 传递一个index key specification documentcreateIndex()即可.

    { <field1>: <type1>, ...}
    

    <type>:

    • 1 升序index
    • -1 降序index

    createIndex()仅在index不存在的时候创建index.

    Create a Single-Field Index

    db.restaurants.createIndex( { "cuisine": 1 } )
    

    结果:

    2017-11-16T21:57:28.148+0800 I INDEX    [conn4] build index done.  scanned 25360 total records. 0 secs
    {
        "createdCollectionAutomatically" : false,
        "numIndexesBefore" : 1,
        "numIndexesAfter" : 2,
        "ok" : 1
    }
    

    Create a compound index

    db.restaurants.createIndex( { "cuisine": 1, "address.zipcode": -1 } )
    

    fields的顺序决定了index如何存储它的keys. 上面的命令建立indexcuisine field 和 address.zipcode field.

    The index orders its entries first by ascending "cuisine" values, and then, within each "cuisine", by descending "address.zipcode" values.

    输出:

    2017-11-16T22:01:53.380+0800 I INDEX    [conn4] build index done.  scanned 25360 total records. 0 secs
    {
        "createdCollectionAutomatically" : false,
        "numIndexesBefore" : 2,
        "numIndexesAfter" : 3,
        "ok" : 1
    }
    

    相关文章

      网友评论

        本文标题:MongoDB 101

        本文链接:https://www.haomeiwen.com/subject/jlnrvxtx.html