现在的位置: 首页 > 综合 > 正文

Mongo官方文档翻译 (二)

2013年06月15日 ⁄ 综合 ⁄ 共 8388字 ⁄ 字号 评论关闭

官方文档地址:http://docs.mongodb.org/manual/applications/create/

创建

在数据库中的四个基本操作(CRUD),创建操作是指那些把记录或文档集合添加到
MongoDB中的聚集中的操作。
概述:
你可以利用下面的任一基本操作在MongoDB中创建文档集合:
insert
create with save
create with upsert
下列是MongoDB中所有的insert操作:
1.如果你试图插入一条没有_id字段的文档,客户端库或mongod实例会自动添加_id字段并设置
将其填充给一个唯一的ObjectId字段!
2.对于写相关的操作,如果你指定一个_id字段,那么该_id字段在聚集中必须是唯一的;否则,
 mongod会返回一个duplicate key exception。
3.BSON文档的最大大小事16MB。
文档类型的最大值有助于确定一个单独的文档不能占用过多的内存以及在传输过程中占用过多的带宽。
为了保存大于最大值的文档,MongoDB提供了GridFSB API。

4.文档在字段名称上有如下的约束:
1.字段_id是预留的用作主键的字段,字段的值必须是在聚集中唯一的,不变的,并且可以使除了数组
的其他任何类型。
2.字段名不能以$开始。
3.字段名称不能包含.。

注意:以下的驱动版本中,所有的写相关操作都会发出一个getLastError命令,来确认写操作的结果:
C#, version 0.7
Java, version 2.10.0
Node.js, version 1.2
Perl, version 0.601.1
PHP, version 1.4
Python, version 2.4
Ruby, version 1.8

{getLastError:1}

1.插入操作

insert()是将一个或多个文档插入到MongoDB聚集中的基本方法,该方法有如下的句式:

db.collection.insert( <document> )
 
SQL中类似的操作:insert()类似于INSERT操作!

下面的示例演示了insert()的用法:
1.如果聚集并不存在(你可以在mongo shell中通过show collections 操作来列出存在的聚集),
insert()方法会在第一次插入数据的时候创建聚集。如下所示:如果聚集bios不存在,insert操作
会创建这个聚集:

db.bios.insert(
  {
     _id: 1,
     name: { first: 'John', last: 'Backus' },
     birth: new Date('Dec 03, 1924'),
     death: new Date('Mar 17, 2007'),
     contribs: [ 'Fortran', 'ALGOL', 'Backus-Naur Form', 'FP' ],
     awards: [
               {
                 award: 'W.W. McDowell Award',
                 year: 1967,
                 by: 'IEEE Computer Society'
               },
               {
                 award: 'National Medal of Science',
                 year: 1975,
                 by: 'National Science Foundation'
               },
               {
                 award: 'Turing Award',
                 year: 1977,
                 by: 'ACM'
               },
               {
                 award: 'Draper Prize',
                 year: 1993,
                 by: 'National Academy of Engineering'
               }
     ]
  }
)

你可以通过查询bios来确认insert的操作结果:
db.bios.find()
操作会返回如下数据:


{
 "_id" : 1,
 "name" : { "first" : "John", "last" : "Backus" },
 "birth" : ISODate("1924-12-03T05:00:00Z"),
 "death" : ISODate("2007-03-17T04:00:00Z"),
 "contribs" : [ "Fortran", "ALGOL", "Backus-Naur Form", "FP" ],
 "awards" : [
              {
                "award" : "W.W. McDowell Award",
                "year" : 1967,
                "by" : "IEEE Computer Society"
              },
              {
                "award" : "National Medal of Science",
                "year" : 1975,
                "by" : "National Science Foundation"
              },
              {
                "award" : "Turing Award",
                "year" : 1977,
                "by" : "ACM"
              },
              { "award" : "Draper Prize",
                "year" : 1993,
                "by" : "National Academy of Engineering"
              }
 ]
}

2.如果新的文档不包含_id字段,insert会添加这个字段并且为其生成一个唯一的ObjectId:

db.bios.insert(
  {
     name: { first: 'John', last: 'McCarthy' },
     birth: new Date('Sep 04, 1927'),
     death: new Date('Dec 24, 2011'),
     contribs: [ 'Lisp', 'Artificial Intelligence', 'ALGOL' ],
     awards: [
               {
                 award: 'Turing Award',
                 year: 1971,
                 by: 'ACM'
               },
               {
                 award: 'Kyoto Prize',
                 year: 1988,
                 by: 'Inamori Foundation'
               },
               {
                 award: 'National Medal of Science',
                 year: 1990,
                 by: 'National Science Foundation'
               }
     ]
  }
)

你可以通过以下操作核实insert的行为:
db.bios.find({name:{first:'John',last:'McCarthy'}})

返回的结果中自动产生了一个唯一的_id字段:

{
 "_id" : ObjectId("50a1880488d113a4ae94a94a"),
 "name" : { "first" : "John", "last" : "McCarthy" },
 "birth" : ISODate("1927-09-04T04:00:00Z"),
 "death" : ISODate("2011-12-24T05:00:00Z"),
 "contribs" : [ "Lisp", "Artificial Intelligence", "ALGOL" ],
 "awards" : [
              {
                "award" : "Turing Award",
                "year" : 1971,
                "by" : "ACM"
              },
              {
                "award" : "Kyoto Prize",
                "year" :1988,
                "by" : "Inamori Foundation"
              },
              {
                "award" : "National Medal of Science",
                "year" : 1990,
                "by" : "National Science Foundation"
              }
 ]
}
3.如果你传递一个数组到insert(),insert()会对聚集产生一个批量的插入!
接下来的操作向bios聚集中插入了3个文档,这个操作同样也阐述了MongoDB中
动态模式的特性,尽管_id:3的文档中有一个title字段是其他2个文档中没有的,
MongoDB也不多强制要求其他文档包含次属性:


db.bios.insert(
  [
     {
       _id: 3,
       name: { first: 'Grace', last: 'Hopper' },
       title: 'Rear Admiral',
       birth: new Date('Dec 09, 1906'),
       death: new Date('Jan 01, 1992'),
       contribs: [ 'UNIVAC', 'compiler', 'FLOW-MATIC', 'COBOL' ],
       awards: [
                 {
                   award: 'Computer Sciences Man of the Year',
                   year: 1969,
                   by: 'Data Processing Management Association'
                 },
                 {
                   award: 'Distinguished Fellow',
                   year: 1973,
                   by: ' British Computer Society'
                 },
                 {
                   award: 'W. W. McDowell Award',
                   year: 1976,
                   by: 'IEEE Computer Society'
                 },
                 {
                   award: 'National Medal of Technology',
                   year: 1991,
                   by: 'United States'
                 }
       ]
     },
     {
       _id: 4,
       name: { first: 'Kristen', last: 'Nygaard' },
       birth: new Date('Aug 27, 1926'),
       death: new Date('Aug 10, 2002'),
       contribs: [ 'OOP', 'Simula' ],
       awards: [
                 {
                   award: 'Rosing Prize',
                   year: 1999,
                   by: 'Norwegian Data Association'
                 },
                 {
                   award: 'Turing Award',
                   year: 2001,
                   by: 'ACM'
                 },
                 {
                   award: 'IEEE John von Neumann Medal',
                   year: 2001,
                   by: 'IEEE'
                 }
       ]
     },
     {
       _id: 5,
       name: { first: 'Ole-Johan', last: 'Dahl' },
       birth: new Date('Oct 12, 1931'),
       death: new Date('Jun 29, 2002'),
       contribs: [ 'OOP', 'Simula' ],
       awards: [
                 {
                   award: 'Rosing Prize',
                   year: 1999,
                   by: 'Norwegian Data Association'
                 },
                 {
                   award: 'Turing Award',
                   year: 2001,
                   by: 'ACM'
                 },
                 {
                   award: 'IEEE John von Neumann Medal',
                   year: 2001,
                   by: 'IEEE'
                 }
       ]
     }
  ]
)

2.通过save创建

 save()方法是一个特殊的方法,它使用<document>的_id字段来决定插入或者更新一个文档。
  1.如果<document>参数不包含_id字段或者包含的_id字段的值并不在聚集中,save()方法会表现
  插入操作。
  2.否则,save()方法会表现为更新操作。
 
 save()方法的句法如下:
  db.collection.save( <document> )
 
 通过下面的示例来思考save()方法在插入操作方面的用法:
  1.如果<document>不包含_id字段,save()方法会表现为插入.
  具体实现参照“插入操作“中对于不包含_id字段文档的插入操作的详细说明。
  下面的操作展示了<document>不包含_id字段的时候save()方法表现出来的插入行为:
 
db.bios.save(
  {
     name: { first: 'Guido', last: 'van Rossum'},
     birth: new Date('Jan 31, 1956'),
     contribs: [ 'Python' ],
     awards: [
               {
                 award: 'Award for the Advancement of Free Software',
                 year: 2001,
                 by: 'Free Software Foundation'
               },
               {
                 award: 'NLUUG Award',
                 year: 2003,
                 by: 'NLUUG'
               }
     ]
  }
)
2.如果<document>包含_id字段但是字段的值在聚集中没有找到,save()方法也会表现出插入操作。
具体实现参照“插入操作“中插入操作的详细说明。

下面的操作展示了将一个文档插入到bios聚集中,但是该文档所包含的_id字段的数值10在bios聚集中
无法找到的情况:

db.bios.save(
  {
    _id: 10,
    name: { first: 'Yukihiro', aka: 'Matz', last: 'Matsumoto'},
    birth: new Date('Apr 14, 1965'),
    contribs: [ 'Ruby' ],
    awards: [
              {
                award: 'Award for the Advancement of Free Software',
                year: '2011',
                by: 'Free Software Foundation'
              }
    ]
  }
)

3.通过upsert创建
一个upsert消除了一条在一个独立数据库中的记录在插入或更新之前调用检查其是否存在的需要。(原文:
An upsert eliminates the need to perform a separate database call to check for the existence of a record before performing either an update or an insert operation.)
传统的update操作会更新已存在的文档模型,但是在MongoDB中,update()操作可以接受<upsert>操作作为一个参数
,upsert是一个调用<query>参数来决定写操作的混合操作:
1.如果query匹配到一个或多个已然存在的文档,upsert会表现为更新操作。
2.如果query在聚集中没有匹配到文档,则erpsert会插入一个独立的文档。

通过下面的句法思考upsert的操作:

db.collection.update( <query>,
                      <update>,
                      { upsert: true } )
  
  接下来的例子阐述了upsert表现为create的操作:
  
1.如果没有文档匹配到<query>参数,upsert表现为插入操作,如果<update>参数仅包含字段和数值对,则新文档
包含<update>参数中指定的所有字段和值,如果_id字段被省略了,那么添加操作会自动产生一个
唯一的ObjectId来填充_id字段(唯一的哦)!

下面的upsert操作箱bios聚集中插入了一个新的文档:

db.bios.update(
  { name: { first: 'Dennis', last: 'Ritchie'} },
  {
    name: { first: 'Dennis', last: 'Ritchie'},
    birth: new Date('Sep 09, 1941'),
    died: new Date('Oct 12, 2011'),
    contribs: [ 'UNIX', 'C' ],
    awards: [
              {
                award: 'Turing Award',
                year: 1983,
                by: 'ACM'
              },
              {
                award: 'National Medal of Technology',
                year: 1998,
                by: 'United States'
              },
              {
                award: 'Japan Prize',
                year: 2011,
                by: 'The Japan Prize Foundation'
              }
    ]
  },
  { upsert: true }
)

2.如果<query>参数没有匹配到文档,upsert操作会插入一个新的文档,如果<update>参数只是
包含对已有文档对象的修改,那么新诞生的文档会包含<query>参数中已有的字段和<update>
参数中更新的字段!

下面的操作在bios聚集中插入了一个新文档:

db.bios.update(
  {
    _id: 7,
    name: { first: 'Ken', last: 'Thompson' }
  },
  {
    $set: {
             birth: new Date('Feb 04, 1943'),
             contribs: [ 'UNIX', 'C', 'B', 'UTF-8' ],
             awards: [
                       {
                         award: 'Turing Award',
                         year: 1983,
                         by: 'ACM'
                       },
                       {
                         award: 'IEEE Richard W. Hamming Medal',
                         year: 1990,
                         by: 'IEEE'
                       },
                       {
                         award: 'National Medal of Technology',
                         year: 1998,
                         by: 'United States'
                       },
                       {
                         award: 'Tsutomu Kanai Award',
                         year: 1999,
                         by: 'IEEE'
                       },
                       {
                         award: 'Japan Prize',
                         year: 2011,
                         by: 'The Japan Prize Foundation'
                       }
            ]
          }
  },
  { upsert: true }
)

抱歉!评论已关闭.