您的位置:首页 > 编程语言 > Go语言

Mongo官方文档翻译 (三)

2013-01-22 16:55 423 查看
官方文档地址:http://docs.mongodb.org/manual/applications/read/

读操作

在数据库的四种基础操作中,读操作是指那些从MongoDB聚集中检索记录或文档的操作。

你可以通过以下任意一种方法从MongoDB中检索文档:

1.find

2.findOne

Find

find方法是从聚集中检索众多文档的基础方法,find()方法返回包含若干文档的cursor。大多数的

驱动程序都向应用程序开发者提供了一种本地的可遍历借口来处理cursor和遍历文档。find()方法

包含如下句法:

db.collection.find( <query>, <projection> )

SQL中类似的操作:find()方法类似于SQL中的SELECT语句。

1.<query>参数类似于WHERE字句,而且

2.<projection>参数类似于将要从结果集合中检索的目标字段。

通过下面的示例来阐述find()方法的使用:

这个示例相关于一个bios的聚集,该聚集中有如下文档原型:

{

"_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. McDowellAward",

"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"

}

]

}

注意:在mongo shell中,你可以通过在find()后面添加.pertty()来格式化输出。

1.如果没有<query>参数,find()方法会返回聚集中的所有文档,

下面的操作返回bios聚集中的所有文档,(更精确的说法,返回一个cursor)

db.bios.find()

2.如果有<query>参数,find()方法会从聚集的所有文档中选择满足query标准的。

下面的扫做返回bios聚集中所有_id字段为5或者ObjectId("507c35dd8fada716c89d0013")

db.bios.find(

{

_id: { $in: [ 5, ObjectId("507c35dd8fada716c89d0013") ] }

}

)



下面的操作返回bios聚集中array字段contribs中包含unix的文档:



db.bios.find(

{

contribs: 'UNIX'

}

)

下面操作返回bios聚集中array字段awards包含一个子文档,该子文档包含award字段里有T如ing Arard

而且year字段大于1980:

db.bios.find(

{

awards: {

$elemMatch: {

award: 'Turing Award',

year: { $gt: 1980 }

}

}

}

)

下面的操作返回bios聚集中一个子文档“name"中包含字段first值为”Yukihiro“而且

last字段值为”Matsumoto“,quer通过.操作符来访问子文档的字段:

db.bios.find(

{

'name.first': 'Yukihiro',

'name.last': 'Matsumoto'

}

)

实际上,上面的操作对于下面连各种情况都会匹配:

{

first: 'Yukihiro',

aka: 'Matz',

last: 'Matsumoto'

}

{

last: 'Matsumoto',

first: 'Yukihiro'

}

下面的操作会返回bios聚集中子文档name字段确切的{ first: 'Yukihiro', last: 'Matsumoto' },并且顺序

也一致:

db.bios.find(

{

name: {

first: 'Yukihiro',

last: 'Matsumoto'

}

}

)

上面的操作会严格的匹配子文档中的数值包括顺序,二下面的语句就不会了:

{

first: 'Yukihiro',

aka: 'Matz',

last: 'Matsumoto'

}

{

last: 'Matsumoto',

first: 'Yukihiro'

}

下面的操作会返回bios聚集中name子文档下first字段值为字母G开头或者birth字段的值

小于new Date('01/01/1945'):

db.bios.find(

{ $or: [

{ 'name.first' : /^G/ },

{ birth: { $lt: new Date('01/01/1945') } }

]

}

)

下面的操作会返回bios聚集中name子文档下first字段值为字母K开头并且contribs字段中

包含UNIX值得文档:

db.bios.find(

{

'name.first': /^K/,

contribs: 'UNIX'

}

)

在上面的查询中,参数(两个参数)通过一个隐式的AND在两个不同的字段之间进行连接,如果

AND之间连接的字段是相同的,则需要用$and操作符!

如果存在<projection>参数,那么find()方法只会返回<projection>参数中指定的要包含

或者排除的字段!

注意:_id字段默认的被包含在<projection>参数中,在projection中所有明确指出包含的

字段中,_id字段是你可以明确排除的,否则,你将不能使包含字段与排除字段的说明!

(原文:The _id field is implicitly included in the <projection> argument.

In projections that explicitly include fields,

_id is the only field that you can explicitly exclude.

Otherwise, you cannot mix include field and

exclude field specifications.)

下面的操作将会返回bios聚集的所有文档的name字段,contribs字段,以及_id字段:

db.bios.find(

{ },

{ name: 1, contribs: 1 }

)

下面的操作会返回bios聚集的所有文档中的name字段和congribs字段:

db.bios.find(

{ },

{ name: 1, contribs: 1, _id: 0 }

)

下面的操作会返回bios聚集的所有文档中contribs字段中包含元素:”OOP“并且从结果文档中返回所有字段但是排除:

_id,name中first字段,birth字段!

db.bios.find(

{ contribs: 'OOP' },

{ _id: 0, 'name.first': 0, birth: 0 }

)

下面的操作会返回bios聚集的所有文档中name子文档的last字段,以及contribs字段中的前两项内容!

db.bios.find(

{ },

{

_id: 0,

'name.last': 1,

contribs: { $slice: 2 }

}

)

游标

find()方法返回一个结果的cursor,在mongo shell中,如果返回的cursor没有被赋值给

一个变量,那么这个cursor会被自动的遍历20次来打印cursor中的前20个文档,示例如下:

db.bios.find( { _id: 1 } );

当你把find()赋值到一个变量的时候:

1.你可以输入变量的名字来显示匹配的文档中的前20条。如下所示:

var myCursor = db.bios.find( { _id: 1 } );

myCursor

2.你可以使用cursor的next()方法来访问文档,如下所示:

var myCursor = db.bios.find( { _id: 1 } );

var myDocument = myCursor.hasNext() ? myCursor.next() : null;

if (myDocument) {

var myName = myDocument.name;

print (tojson(myName));

}

为了输出,你也可以使用pringjson()方法来代替print(tojson()):

if (myDocument) {

var myName = myDocument.name;

printjson(myName);

}

你也可以使用cursor的forEach()方法来遍历游标并访问文档,如下所示:

var myCursor = db.bios.find( { _id: 1 } );

myCursor.forEach(printjson);

注意:你可以使用DBQuary.shellBatchSize来改变cursor中的默认值20.

修改游标行为:

除了<query>和<projection>参数之外,mongo shell和drivers提供了其他的方法

所以你可以在find()方法返回的cursor上调用这些方法来改变cursor的行为,这些方法是:

1.sort,通过method指定的一个或多个字段来对结果集中的文档排序!

下面的操作会对bios聚集中返回的cursor进行按照name的升序排列:

db.bios.find().sort( { name : 1 } )

sort()类似于SQL中的ORDER BY子句!

2.limit()方法用来限制结果集中文档的数量。

下面的操作会返回bios聚集中最多5条文档:

db.bios.find().limit(5)

limit()方法类似于SQL中的LIMIT子句!

3.skip()方法控制着结果集的起点!

下面的操作返回bios聚集的所有文档中返回的cursor,跳过前5条:

db.bios.find().skip(5)

你可以像下面一样连接这几个cursor方法:

db.bios.find().sort( {name:1} ).limit(5)

db.bios.find().limit( 5 ).sort( {name:1} )

Find One

findOne()方法从聚集中返回一个单独的文档并且不会返回cursor。

findOne()方法有如下句法:

db.collection.findOne( <query>,<projection> )

除了返回值,findOne()方法非常类似于find()方法。事实上,在内部中,

findOne()方法就是带limit(1)的find()!

通过下面的示例来理解findOne()方法的使用:

1.如果没有<query>参数,findOne()方法仅仅从聚集中攒则一个文档。

下面的操作从bios聚集中返回一个单独的文档。

db.bios.findOne()

2.如果有<query>参数,findOne()方法从满足<query>条件的集合中选择一个文档!

下面的操作会在bios聚集的文档中选择第一条(first matching)满足子文档name中的first字段以G字母开头或者birth字段小于new Date('01/01/1945')的记录:

db.bios.findOne(

{

$or: [

{ 'name.first' : /^G/ },

{ birth: { $lt: new Date('01/01/1945') } }

]

}

)

3.你可以向findOne()方法传递一个<projection>参数来控制结果集中的字段:

下面的操作返回bios聚集的所有文档中的一条,且只包含name字段,contribs字段,当然,还有_id字段:

db.bios.findOne(

{ },

{ name: 1, contribs: 1 }

)

下面的操作返回bios聚集的所有文档中满足contribs字段包含”OOP“的记录,但是返回的字段中排除_id,name.first,birth:

db.bios.findOne(

{ contribs: 'OOP' },

{ _id: 0, 'name.first': 0, birth: 0 }

)

尽管于find()方法很相似,由于findOne()方法返回的是文档而并不是cursor,所以你并不能把cursor上的方法诸如:sort(),skip(),limit()等,但是你可以直接访问文档,如下所示:

var myDocument = db.bios.findOne();

if (myDocument) {

var myName = myDocument.name;

print (tojson(myName));

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: