Mongoose(二):快速开始

关于快速开始,官方文档原文中有很详细的说明。

以下包含原文与调皮的译文,所以你可以不必点开原文。

Getting Started
开始使用Mongoose——

First be sure you have MongoDB and Node.js installed.
首先,确保你已经安装了MongoDB与NodeJS。

Next install Mongoose from the command line using npm:
接下来用NPM命令行工具安装Mongoose

$ npm install mongoose

Now say we like fuzzy kittens and want to record every kitten we ever meet in MongoDB. The first thing we need to do is include mongoose in our project and open a connection to the test database on our locally running instance of MongoDB.
嘛,比如说我们喜欢毛茸茸的喵星人,想在MongoDB中记录我们遇到的每一只喵星人。我们要做的第一件事就是将Mongoose导入我们的项目并建立到本地的一个名为test的数据库的连接。
(译注:当MongoDB被安装后,里面有一个初始的数据库即test。当然,如果不存在,MongoDB会自动创建一个,而不是抛出异常。)

// getting-started.js
var mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/test');

We have a pending connection to the test database running on localhost. We now need to get notified if we connect successfully or if a connection error occurs:
我们将发起一个到本地test数据库的连接。现在我们需要知道我们是否连接成功了:
(译注:localhost即本地,与127.0.0.1等价;mongodb://是指MongoDB定义并实现的一套基于TCP的通讯协议,用于NodeJS与MongoDB之间传输数据;)

var db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', function() {
  // we're connected!
});

Once our connection opens, our callback will be called. For brevity, let’s assume that all following code is within this callback.
一旦我们发起连接,这里定义的回调函数就会被调用。为了简洁起见,假设接下来的所有代码都被写在这个回调函数之内。
(译注:指上面代码中// we're connected 的位置)

With Mongoose, everything is derived from a Schema. Let’s get a reference to it and define our kittens.
在Mongoose中,所有事物都源于模式。参考下面的代码来定义我们的喵星人。

var kittySchema = mongoose.Schema({
    name: String
});

So far so good. We’ve got a schema with one property, name, which will be a String. The next step is compiling our schema into a Model.
这样就够了。我们已经创造了一个模式,它只有一个属性:名字(name),是个字符串。下一步要将我们的模式编译为一个Mongoose模型。(使其具有完整的属性、方法,并且能接入数据库)

var Kitten = mongoose.model('Kitten', kittySchema);

A model is a class with which we construct documents. In this case, each document will be a kitten with properties and behaviors as declared in our schema. Let’s create a kitten document representing the little guy we just met on the sidewalk outside:
模型是文档的构造器。在本例中,每一个文档都是一个喵星人,它具有我们之前在模式中定义的属性与行为。现在来创建一个喵星人文档:
(译注:在MongoDB中文档的概念与实体(Entity)比较类似。)

var silence = new Kitten({ name: 'Silence' });
console.log(silence.name); // 'Silence'

Kittens can meow, so let’s take a look at how to add “speak” functionality to our documents:
喵星人的一大萌点在于其叫声“喵~”,所以让我们看看怎么给它加上一个发声的功能。

// NOTE: methods must be added to the schema before compiling it with mongoose.model()
// 注意:在模式中添加方法以后要用model方法重新编译为模型
kittySchema.methods.speak = function () {
  var greeting = this.name
    ? "Meow name is " + this.name
    : "I don't have a name";
  console.log(greeting);
}

var Kitten = mongoose.model('Kitten', kittySchema);

Functions added to the methods property of a schema get compiled into the Model prototype and exposed on each document instance:
这些被加到Schema(模式)的methods(方法)属性中的函数会被编译到模型原型方法里,并暴露给所有文档实例。
(译注:这里有许多专业名词,一句话概括一下就是,这些喵星人可以调用方法来发声啦)

var fluffy = new Kitten({ name: 'fluffy' });
fluffy.speak(); // "Meow name is fluffy"

We have talking kittens! But we still haven’t saved anything to MongoDB. Each document can be saved to the database by calling its save method. The first argument to the callback will be an error if any occured.
我们有了会叫的喵啦!但是我们尚未在MongoDB中储存任何东西。所有的文档都能通过save方法被保存到数据库中。方法的第一个参数是一个用于处理异常的回调函数。
(译注:不一定是处理异常,更确切的说法是,当保存操作完毕之后的回调——因为它不仅回传err,还回传文档实例,如下代码所示,甚至还可以调用方法)

fluffy.save(function (err, fluffy) {
  if (err) return console.error(err);
  fluffy.speak();
});

Say time goes by and we want to display all the kittens we’ve seen. We can access all of the kitten documents through our Kitten model.
时间过得很快(夜幕就要降临),现在我们想要显示我们看到过的所有喵星人。我们可以通过喵星人模型访问所有的喵星人文档。
(译者打开调皮的BGM:时间过得很快,夜幕就要降临,我想我必须要离开。当我正要走时我看到了一家专卖店,那就是我要的滑板鞋!我的滑板鞋时尚时尚最时尚……)

Kitten.find(function (err, kittens) {
  if (err) return console.error(err);
  console.log(kittens);
})

We just logged all of the kittens in our db to the console. If we want to filter our kittens by name, Mongoose supports MongoDBs rich querying syntax.
我们刚刚在控制台记录了所有存储在数据库中的喵星人。如果我们想要通过喵星人的名字过滤一部分喵星人,Mongoose支持了MongoDB中丰富的查询语句。

Kitten.find({ name: /^Fluff/ }, callback);

This performs a search for all documents with a name property that begins with “Fluff” and returns the result as an array of kittens to the callback.
这里演示了找出所有以名字以”Fluff”开头的喵星人,并将结果通过回调函数传递。
(译注:这里的/^Fluff 是正则表达式,也可以用MongoDB的其他查询文档代替之)

Congratulations
恭喜恭喜!

That’s the end of our quick start. We created a schema, added a custom document method, saved and queried kittens in MongoDB using Mongoose. Head over to the guide, or API docs for more.
我们的快速开始 结束了。回顾一下,我们用Mongoose创建了一个模式,添加了自定义的文档方法,保存到MongoDB并从中查询喵星人的资料。如果想获取更多Mongoose的信息,请阅读指南或者API文档

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值