Mongoo(三):数据类型
既然Mongoo是数据建模⽤的,那么必然应当极为重视数据类型。
关于数据类型, ⾥有详尽的描述,以下是原⽂与调⽪的译⽂。
注意:这绝⾮是⼀个词⼀个词的翻译,请不要⽤它来学习英⽂。=w=
SchemaTypes 数据类型
SchemaTypes handle definition of path defaults, validation, getters, tters, field lection defaults for queries and other general characteristics for Strings and Numbers. Check out their respective API documentation for more detail.
数据类型⽤于定义默认路径, 验证⽅式, 获取/设置⽅法,⽤于数据库查询的默认字段,以及其他针对字符串与数字的特性。关于详细信息请查阅相关API⽂档。
译注:默认路径即某个域相对于⽂档⽽⾔的路径,如{a: 1}这个⽂档中,若指定路径为’a’,即可访问到1这个数据。
Following are all valid Schema Types.
接下来是Mongoo中所有可⽤的数据类型。
String
字符串
Number
数字
Date
⽇期
Buffer
缓冲区
圣马丁节
Boolean
布尔值
Mixed小年祝福语
混合
Objectid
高一数学课本对象ID
Array
数组
Example
举个栗⼦!
var schema = new Schema({
name: String,
binary: Buffer,
living: Boolean,
updated: { type: Date, default: w },
age: { type: Number, min: 18, max: 65 },
mixed: Schema.Types.Mixed,
_someId: Schema.Types.ObjectId,
array: [],
ofString: [String],
pickonofNumber: [Number],
ofDates: [Date],
ofBuffer: [Buffer],
ofBoolean: [Boolean],
ofMixed: [Schema.Types.Mixed],
ofObjectId: [Schema.Types.ObjectId],
nested: {
stuff: { type: String, lowerca: true, trim: true }
}
})
// example u
var Thing = del('Thing', schema);
var m = new Thing;
m.name = 'Statue of Liberty';
m.age = 125;
m.updated = new Date;
m.binary = new Buffer(0);
m.living = fal;
m.mixed = { any: { thing: 'i want' } };
m.markModified('mixed');
歌管楼台声细细m._someId = new mongoo.Types.ObjectId;
m.array.push(1);
m.ofString.push("strings!");
m.ofNumber.unshift(1,2,3,4);
m.ofDates.addToSet(new Date);
m.ofBuffer.pop();
m.ofMixed = [1, [], 'three', { four: 5 }];
m.save(callback);
Usage notes ⽤法要点
Dates ⽇期型
Built-in Date methods are not hooked into the mongoo change tracking logic which in English means that if you u a Date in your document and modify it with a method like tMonth(), mongoo will be unaware of this change and
doc.save() will not persist this modification. If you must modify Date types using built-in methods, tell mongoo about the change with doc.markModified(‘pathToYourDate’) before saving.
Mongoo不跟踪JS内建的⽇期⽅法对数据造成的改变。这意味着如果你在⽂档中使⽤Date 类型并⽤tMonth之类的⽅法去修改
它,Mongoo不会意识到它的改变,调⽤doc.save⽅法保存时不会保留这个修改。如果你⼀定要⽤JS内建的⽅法修改Date类型的数据,在保存之前⽤doc.markModified ⽅法告诉Mongoo这个改变。
var Assignment = del('Assignment', { dueDate: Date });
Assignment.findOne(function(err, doc) {
doc.dueDate.tMonth(3);
doc.save(callback); // THIS DOES NOT SAVE YOUR CHANGE
doc.markModified('dueDate');
doc.save(callback); // works
})
Mixed 混合型
An “anything goes” SchemaType, its flexibility comes at a trade-off of it being harder to maintain. Mixed is available either through Schema.Types.Mixed or by passing an empty object literal. The foll
owing are equivalent:
混合型是⼀种“存啥都⾏”的数据类型,它的灵活性来⾃于对可维护性的妥协。Mixed类型⽤Schema.Types.Mixed 或者⼀个字⾯上的空对象{}来定义。下⾯的定义是等价的:
var Any = new Schema({ any: {} });
var Any = new Schema({ any: Schema.Types.Mixed });
Since it is a schema-less type, you can change the value to anything el you like, but Mongoo los the ability to auto detect and save tho changes. To “tell” Mongoo that the value of a Mixed type has changed, call the
.markModified(path) method of the document passing the path to the Mixed type you just changed.
因为它是⼀种 ⽆拘⽆束,⽆法⽆天 ⽆固定模式的类型,所以你可以想怎么改就怎么改,但是Mongoo 没有能⼒去⾃动检测和保存这些改动。请通过调⽤doc.markModified ⽅法来告诉Mongoo某个混合类型的值被改变了。
person.anything = { x: [3, 4, { y: "changed" }] };
person.markModified('anything');
person.save(); // anything will now get saved
ObjectIds 对象ID型
To specify a type of ObjectId, u Schema.Types.ObjectId in your declaration.
⽤Schema.Types.ObjectId 来声明⼀个对象ID类型。
译注:对象ID同MongoDB内置的_id 的类型。由24位Hash字符串。
var mongoo = require('mongoo');
var ObjectId = mongoo.Schema.Types.ObjectId;
var Car = new Schema({ driver: ObjectId });
// or just Schema.ObjectId for backwards compatibility with v2
Arrays 数组型
Provide creation of arrays of SchemaTypes or Sub-Documents.
提供创造各种数据类型或⼦⽂档的数组的⽅法。
var ToySchema = new Schema({ name: String });
var ToyBox = new Schema({
toys: [ToySchema],
buffers: [Buffer],
string: [String],
numbers: [Number]
// ... etc
});
Note: specifying an empty array is equivalent to Mixed. The following all create arrays of Mixed:文明从我做起
注意:⽤⼀个空数组来定义等价于创建⼀个混合型的数组。下⾯都是创建混合型数组的例⼦:
var Empty1 = new Schema({ any: [] });
var Empty2 = new Schema({ any: Array });
金面人var Empty3 = new Schema({ any: [Schema.Types.Mixed] });
var Empty4 = new Schema({ any: [{}] });云南大学研究生
Creating Custom Types 创建⾃定义类型
Mongoo can also be extended with custom SchemaTypes. Search the plugins site for compatible types like mongoo-long, mongoo-int32 and other types. To create your own custom schema take a look at Creating a Basic Custom Schema Type.
Mongoo也⽀持使⽤⾃定义的数据类型来拓展功能。从搜索合适的类型,⽐如mongoo-long,mongoo-int32或者其他类型。 想要⾃⼰创建类型的话请参考。
Next Up
下⼀步
Now that we’ve covered SchemaTypes, let’s take a look at Models.
现在我们看完数据类型部分,让我们来看看模型部分。