Allan 译 The Little MongoDB Book (Chapter 1 - The Basics)

译文:

Chatper 1 - The Basics

通过基本操作来开始我们的MongoDB之旅。当然了,这也是了解MongoDB的核心,但是它也同
样帮助我们了回答关于MongoDB适合于哪这样的更高层次的问题。

马上开始,这里有6个简单的概念需要我们来了解。

1 MongoDB同样具有你所熟悉的‘数据库’概念(或者在Oracle这样的东东里叫做schema)。在一个
   MongoDB实例中你可以有0或多个数据库,每一个都充当高级别的容器。
2 一个数据库可具有0或多个'collections'(集合)。collection 和传统的'table'基本相同,所以你可以放心的
   把它们俩当作同样的东西。
3 Collections(集合)是由0或多个'documents'(文件) 组成。同样,可以把document安全的当作'row'(行)
   来理解。
4 一个document(文件)由一或多个'fields'(字段)组成。没错,你可以理解为'columns'(列)。
5 'Indexed'(索引)在MongoDB中的功能和RDBMS中的非常相像。
6 'Cursors'(游标)不同于以上五个概念,但是它非常的重要并且常常被忽视,我认为它值得单独讨论。
   了解游标最重要的是,当你向MongoDB读取数据时,它返回一个游标,我们可以做例如计数或者跳过
   这类操作而不必真正的下载数据。

概述一下,MongoDB是由包含'collections'的数据库组成。collection是由documents组成。每一个document
是由fields组成,Collections可以被索引,提高查找和排序性能。最后,当我们从MongoDB请求数据时,是
通过一个cursor,它是按需延迟执行的。

你可能会感到奇怪,为什么使用新术语(collection vs. table, document vs. row 和 field vs. column)。这不是
把事情搞的更复杂了吗?事实是虽然这些概念和它们的关系数据库概念相近,但是并不相同。主要的不同
是关系型数据库定义columns在table一层上,而面向文档数据库定义它的fields在document一层上。这也就是
说collection中的每一个document可以有它自已的独立的一系列fields。这样, collection和table相比就是一个
dumbed down容器,而document比row有更多的信息。

虽然了解它非常重要,但如果不是很清晰也不用担心。用不着几个插入就可以了解它真正的意思。最终,关键
就是一个collection不是那么的严格(它是弱架构)。Fields追踪每一个单独的document。利弊端我们将在下面
章节中介绍。

让我们动手做。如果你还没有让它运行,启运mongod服务器。shell运行JavaScript。你可以执行几个全局命令
如help或exit。你执行的命令是对于你当前的数据库而言的,例如db.help()或都db.stats()。通过db.COLLECTION_NAME
对象针对特定的collection执行命令,比如db.unicorns.help()或者db.unicorns.count()。

输入db.help(),你将获得可以对db对象执行的命令列表。

小小说明一下。因为这是一个JavaScript shell,如果你执一个方法并略去圆括号(),你将会看到方法体而不是
执行这个方法。我提到它是因为当你第一次看到以function(...)开头的响应你不会惊奇。例如,如果你输入db.help
(没有圆括号),你将会看到help方法的内部实现。

首先,我们使用全局use方法去选择数据库,输入use learn。数据库不存在也没有问题 。我们创建的第一个collection
还将是一个实际的learn数据库。现在你就在数据库里,你可以执行数据库命令,如db.getCollectionNames()。如果你
这样做的话,你会得到一个空数组([]),因为collections是弱架构的,我们不用时确的需要去创建它们。我们只需要
向一个新collection插入一个document。使用insert命令,提供它document去插入:

db.unicorns.insert({name: 'Aurora', gender: 'f', weight: 450})

上面一行对unicorns collection执行insert,传递它一个单一参数。MongoDB内部使用一个二进制序列化的JSON格式。
外部,意味着我们要使用很多的JSON,就像我们的参数一样。如果现在我们执行db.getCollectionNames()
 我们会看到两个collection:unicorns和system.indexes。 system.indexes 在每一次数据库创建时生成,它包含我们
数据库中索引的信息。

你可以对unicorns使用find命令返回document列表:

db.unicorns.find()

注意,除了你指定的数据,还有一个_id 字段。每一个document都有一个惟一的_id字段,你可以自已指定一个或者让
MongoDB自动给你生成一个ObjectId。大多数情况下你都会让MongoDB为你自动生成它。默认情况下,_id字段是索引
-这就说明了为什么system.indexes collection被创建。你可以查看一下system.indexes:

db.system.indexes.find()

你将会看到索引的名字,创建所依照的数据库和collection,还有索引中包含的字段。

现在,回来有关我们弱架构collection的讨论上来。向unicorns插入一个完全不同的document,比如:

db.unicorns.insert({name: 'Leto', gender: 'm', home: 'Arrakeen', worm: false})

同样,使用find去列出document。一旦我们知道的多一点,我们就会讨论MongoDB的这个有趣的行为,但是现在希望你
开始了解为什么传统的术语不适合它。

掌握选择器:
除了我们已探究的6个概念,在学习更高级的话题前,MongoDB还有一个实用的方面需要你更好的把握:查询选择器。
MongoDB的查询选择器就像SQL中的where从句。这样,当你从collection查询,计数,更新,删除时可以使用它。
一个选择器是一个JSON对象,它们中最简单的是{},它匹配所有的document(null也算在内)。如果我们想要查找所有的
女性unicorn,我们可以使用{gender:'f'}。

在更深的探究选择器之前,让我们设置一些数据。首先,通过db.unicorns.remove()删除之前我们插入的数据(因为我们没有提
供一个选择器,它会删除所有的document)。现在,执行下面的insert命令插入一些数据。

db.unicorns.insert({name: 'Horny', dob: new Date(1992,2,13,7,47), loves: ['carrot','papaya'], weight: 600, gender: 'm', vampires: 63});
db.unicorns.insert({name: 'Aurora', dob: new Date(1991, 0, 24, 13, 0), loves:['carrot', 'grape'], weight: 450, gender: 'f', vampires: 43});
db.unicorns.insert({name: 'Unicrom', dob: new Date(1973, 1, 9, 22, 10), loves:['energon', 'redbull'], weight: 984, gender: 'm', vampires: 182});
db.unicorns.insert({name: 'Roooooodles', dob: new Date(1979, 7, 18, 18, 44),loves: ['apple'], weight: 575, gender: 'm', vampires: 99});
db.unicorns.insert({name: 'Solnara', dob: new Date(1985, 6, 4, 2, 1), loves:['apple', 'carrot', 'chocolate'], weight:550, gender:'f', vampires:80});
db.unicorns.insert({name:'Ayna', dob: new Date(1998, 2, 7, 8, 30), loves: ['strawberry', 'lemon'], weight: 733, gender: 'f', vampires: 40});
db.unicorns.insert({name:'Kenny', dob: new Date(1997, 6, 1, 10, 42), loves: ['grape', 'lemon'], weight: 690, gender: 'm', vampires: 39});
db.unicorns.insert({name: 'Raleigh', dob: new Date(2005, 4, 3, 0, 57), loves:['apple', 'sugar'], weight: 421, gender: 'm', vampires: 2});
db.unicorns.insert({name: 'Leia', dob: new Date(2001, 9, 8, 14, 53), loves: ['apple', 'watermelon'], weight: 601, gender: 'f', vampires: 33});
db.unicorns.insert({name: 'Pilot', dob: new Date(1997, 2, 1, 5, 3), loves: ['apple', 'watermelon'], weight: 650, gender: 'm', vampires: 54});
db.unicorns.insert({name: 'Nimue', dob: new Date(1999, 11, 20, 16, 15), loves:['grape', 'carrot'], weight: 540, gender: 'f'});
db.unicorns.insert({name: 'Dunx', dob: new Date(1976, 6, 18, 18, 18), loves: ['grape', 'watermelon'], weight: 704, gender: 'm', vampires: 165});

现在 我们有了数据,我们可以控制选择器。{field: value}用于查找任何field等于value的document。{field1: value1, field2:value2}
我们用在and声明中。特殊的 $lt, $lte, $gt, $gte和$ne用在小于,小于等于,大于, 大于等于和不等于操作中。例如,查找所有的体重大于
700磅的男性  unicorn。我们可以这样做:

db.unicorns.find({gender: 'm', weight: {$gt: 700}})
//或者(不相同,只是出于示范目的)
db.unicorns.find({gender: 'm', weight: {$gte: 700}})

$exists运算符用于匹配出现和未出现的字段,例如:

db.unicorns.find({vampires: {$exists: false}})

返回一个单一document。如果我们想要OR而不是AND,我们可以使用$or 运算符,并把我们需要的or条件放置在一个数组中:

db.unicorns.find({gender: 'f', $or:[{loves:'apple'}, {loves: 'orange'}, {weight: {$lt: 500}}]})

上面命令会返回喜欢apple或orange或体重小于500磅的女性unicorn。

在我们最后一个例子中, 有一些整齐漂亮的东西。你可能已经注意到了,就是 loves字段是一个数组。MongoDB支持数组作为
第一类对象。这是一个难以置信的方便的特性。一旦你开始使用它,你会惊奇没有它你是怎么活的。更有趣的是使用
{loves: 'watermelon'}会返回喜欢任喜欢水果之一是watermelon的记录。

还有更多比我们现在看到的操作符有价值的操作符。最灵活的是$where,它可以让我们提供在服务器端执行的JavaScript。所有这些
都在MongoDB网站上高级查询中有具体描述。目前我们所涉及的是你现在需要的基础信息。必且你将会一直使用它们。

我们已经看到了这些选择器怎样使用find命令。还有我们简单看了一下的remove命令。count命令我们还没有涉及,但是你可以大概能
解决的出。update命令我们将在下面花更多的时间来讨论。

MongoDB为_id字段生成的ObjectId可以这样查询:

db.unicorns.find({_id: ObjectId("TheObjectId")})

In This Chpter
我们还没有了解update命令和一些find实现的华丽东西。然而,我们让MongoDB运行起来,
了解了简单的insert和remove命令。我们还介绍了find和MongoDB的选择器。我们有了一个好的
开端并为接下来的东西有一个不错的基础。信或不信,你实际上已知道了大多数MongoDB-它真的是
很好学和易用。我强烈的主张你在你本地运行这些命令。插入不同的document,或者在新的collection,
使用不同的选择器。使用find, count和remove。在你自已做完几个尝试后,刚开始还很笨拙的
事情可能最后就变得很简单明子。

 

原文:

Chapter 1 - The Basics
We begin our journey by getting to know the basic mechanics of working with MongoDB.
Obviously this is core to understanding MongoDB, but it should also help us answer higher-
level questions about where MongoDB fits.
To get started, there are six simple concepts we need to understand.
1. MongoDB has the same concept of a `database' with which you are likely already familiar
(or a schema for you Oracle folks). Within a MongoDB instance you can have zero or more
databases, each acting as high-level containers for everything else.
2. A database can have zero or more `collections'. A collection shares enough in common
with a traditional `table' that you can safely think of the two as the same thing.
3. Collections are made up of zero or more `documents'. Again, a document can safely be
thought of as a `row'.
4. A document is made up of one or more `fields', which you can probably guess are a lot like
`columns'.
5. `Indexes' in MongoDB function much like their RDBMS counterparts.
6. `Cursors' are different than the other five concepts but they are important enough, and
often overlooked, that I think they are worthy of their own discussion. The important thing
to understand about cursors is that when you ask MongoDB for data, it returns a cursor,
which we can do things to, such as counting or skipping ahead, without actually pulling
down data.
To recap, MongoDB is made up of databases which contain collections. A collection is
made up of documents. Each document is made up of fields. Collections can be indexed,
which improves lookup and sorting performance. Finally, when we get data from MongoDB we
do so through a cursor whose actual execution is delayed until necessary.
You might be wondering, why use new terminology (collection vs. table, document vs. row and
field vs. column). Is it just to make things more complicated? The truth is that while these
concepts are similar to their relational database counterparts, they are not identical. The core
difference comes from the fact that relational databases define columns at the table level
whereas a document-oriented database defines its fields at the document level. That is to
say that each document within a collection can have its own unique set of fields. As such,
a collection is a dumbed down container in comparison to a table, while a document has a
lot more information than a row.
Although this is important to understand, don't worry if things aren't yet clear. It won't take
more than a couple of inserts to see what this truly means. Ultimately, the point is that a
collection isn't strict about what goes in it (it's schema-less). Fields are tracked with each
individual document. The benefits and drawbacks of this will be explored in a future chapter.
Let's get hands-on. If you don't have it running already, go ahead and start the mongod server
as well as a mongo shell. The shell runs JavaScript. There are some global commands you
can execute, like help or exit. Commands that you execute against the current database
are executed against the db object, such as db.help() or db.stats() . Commands that you
execute against a specific collection, which is what we'll be doing a lot of, are executed against
the db.COLLECTION_NAME object, such as db.unicorns.help() or db.unicorns.count().
Go ahead and enter db.help(), you'll get a list of commands that you can execute against
the db object.
A small side note. Because this is a JavaScript shell, if you execute a method and omit the
parentheses (), you'll see the method body rather than executing the method. I only mention
it because the first time you do it and get a response that starts with function (...){ you
won't be surprised. For example, if you enter db.help (without the parentheses), you'll see
the internal implementation of the help method.
First we'll use the global use method to switch databases, go ahead and enter use learn. It
doesn't matter that the database doesn't really exist yet. The first collection that we create
will also create the actual learn database. Now that you are inside a database, you can start
issuing database commands, like db.getCollectionNames(). If you do so, you should get an
empty array ([ ]). Since collections are schema-less, we don't explicitly need to create them.
We can simply insert a document into a new collection. To do so, use the insert command,
supplying it with the document to insert:
db.unicorns.insert({name: 'Aurora', gender: 'f', weight: 450})
The above line is executing insert against the unicorns collection, passing it a single argu-
ment. Internally MongoDB uses a binary serialized JSON format. Externally, this means that
we use JSON a lot, as is the case with our parameters. If we execute db.getCollectionNames
() now, we'll actually see two collections: unicorns and system.indexes. system.indexes
is created once per database and contains the information on our databases index.
You can now use the find command against unicorns to return a list of documents:
db.unicorns.find()
Notice that, in addition to the data you specified, there's an _id field. Every document must
have a unique _id field. You can either generate one yourself or let MongoDB generate an
ObjectId for you. Most of the time you'll probably want to let MongoDB generate it for you.
By default, the _id field is indexed - which explains why the system.indexes collection was
created. You can look at system.indexes:
db.system.indexes.find()
What you're seeing is the name of the index, the database and collection it was created against
and the fields included in the index.
Now, back to our discussion about schema-less collections. Insert a totally different document
into unicorns, such as:
db.unicorns.insert({name: 'Leto', gender: 'm', home: 'Arrakeen', worm: false})
And, again use find to list the documents. Once we know a bit more, we'll discuss this
interesting behavior of MongoDB, but hopefully you are starting to understand why the more
traditional terminology wasn't a good fit.
Mastering Selectors
In addition to the six concepts we've explored, there's one practical aspect of MongoDB you
need to have a good grasp of before moving to more advanced topics: query selectors. A
MongoDB query selector is like the where clause of an SQL statement. As such, you use it
when finding, counting, updating and removing documents from collections. A selector is a
JSON object , the simplest of which is {} which matches all documents (null works too). If
we wanted to find all female unicorns, we could use {gender:'f'}.
Before delving too deeply into selectors, let's set up some data to play with. First, remove
what we've put so far in the unicorns collection via: db.unicorns.remove() (since we aren't
supplying a selector, it'll remove all documents). Now, issue the following inserts to get some
data we can play with (I suggest you copy and paste this):
db.unicorns.insert({name: 'Horny', dob: new Date(1992,2,13,7,47), loves: ['
carrot','papaya'], weight: 600, gender: 'm', vampires: 63});
db.unicorns.insert({name: 'Aurora', dob: new Date(1991, 0, 24, 13, 0), loves:
['carrot', 'grape'], weight: 450, gender: 'f', vampires: 43});
db.unicorns.insert({name: 'Unicrom', dob: new Date(1973, 1, 9, 22, 10), loves:
['energon', 'redbull'], weight: 984, gender: 'm', vampires: 182});
db.unicorns.insert({name: 'Roooooodles', dob: new Date(1979, 7, 18, 18, 44),
loves: ['apple'], weight: 575, gender: 'm', vampires: 99});
db.unicorns.insert({name: 'Solnara', dob: new Date(1985, 6, 4, 2, 1), loves:['
apple', 'carrot', 'chocolate'], weight:550, gender:'f', vampires:80});
db.unicorns.insert({name:'Ayna', dob: new Date(1998, 2, 7, 8, 30), loves: ['
strawberry', 'lemon'], weight: 733, gender: 'f', vampires: 40});
db.unicorns.insert({name:'Kenny', dob: new Date(1997, 6, 1, 10, 42), loves: ['
grape', 'lemon'], weight: 690, gender: 'm', vampires: 39});
db.unicorns.insert({name: 'Raleigh', dob: new Date(2005, 4, 3, 0, 57), loves:
['apple', 'sugar'], weight: 421, gender: 'm', vampires: 2});
db.unicorns.insert({name: 'Leia', dob: new Date(2001, 9, 8, 14, 53), loves: ['
apple', 'watermelon'], weight: 601, gender: 'f', vampires: 33});
db.unicorns.insert({name: 'Pilot', dob: new Date(1997, 2, 1, 5, 3), loves: ['
apple', 'watermelon'], weight: 650, gender: 'm', vampires: 54});
db.unicorns.insert({name: 'Nimue', dob: new Date(1999, 11, 20, 16, 15), loves:
['grape', 'carrot'], weight: 540, gender: 'f'});
db.unicorns.insert({name: 'Dunx', dob: new Date(1976, 6, 18, 18, 18), loves: [
'grape', 'watermelon'], weight: 704, gender: 'm', vampires: 165});
Now that we have data, we can master selectors. {field: value} is used to find any docu-
ments where field is equal to value. {field1: value1, field2: value2} is how we do an
and statement. The special $lt, $lte, $gt, $gte and $ne are used for less than, less than or
equal, greater than, greater than or equal and not equal operations. For example, to get all
male unicorns that weigh more than 700 pounds, we could do:
db.unicorns.find({gender: 'm', weight: {$gt: 700}})
//or (not quite the same thing, but for demonstration purposes)
db.unicorns.find({gender: {$ne: 'f'}, weight: {$gte: 701}})
The $exists operator is used for matching the presence or absence of a field, for example:
db.unicorns.find({vampires: {$exists: false}})
Should return a single document. If we want to OR rather than AND we use the $or operator
and assign it to an array of values we want or'd:
db.unicorns.find({gender: 'f', $or: [{loves: 'apple'}, {loves: 'orange'}, {
weight: {$lt: 500}}]})
The above will return all female unicorns which either love apples or oranges or weigh less
than 500 pounds.
There's something pretty neat going on in our last example. You might have already noticed,
but the loves field is an array. MongoDB supports arrays as first class objects. This is an
incredibly handy feature. Once you start using it, you wonder how you ever lived without
it. What's more interesting is how easy selecting based on an array value is: {loves: '
watermelon'} will return any document where watermelon is a value of loves.
There are more available operators than what we've seen so far. The most flexible being
$where which lets us supply JavaScript to execute on the server. These are all described in
the Advanced Queries section of the MongoDB website. What we've covered so far though is
the basics you'll need to get started. It's also what you'll end up using most of the time.
We've seen how these selectors can be used with the find command. They can also be
used with the remove command which we've briefly looked at, the count command, which we
haven't looked at but you can probably figure out, and the update command which we'll spend
more time with later on.
The ObjectId which MongoDB generated for our _id field can be selected like so:
db.unicorns.find({_id: ObjectId("TheObjectId")})
In This Chapter
We haven't looked at the update command yet, or some of the fancier things we can do with
find. However, we did get MongoDB up and running, looked briefly at the insert and remove
commands (there isn't much more than what we've seen). We also introduced find and saw
what MongoDB selectors were all about. We've had a good start and laid a solid foundation
for things to come. Believe it or not, you actually know most of what there is to know about
MongoDB - it really is meant to be quick to learn and easy to use. I strongly urge you to play
with your local copy before moving on. Insert different documents, possibly in new collections,
and get familiar with different selectors. Use find, count and remove. After a few tries on
your own, things that might have seemed awkward at first will hopefully fall into place.

 

作者努力翻译,希望转载者注明文章出处及作者。谢谢


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值