请确定已阅读【Node.js】mongoose教程–Schema与Model

GitHub源码链接:sodino#MongoDemo


数据准备

为了对Phone进行存储以及后续的查询,需要提前准备一些手机型号的数据以供练习使用。
为此准备了如下5个手机型号的数据:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
var Phone = mongoose.model('Phone', phoneSchema);
var arrPhone = [];
var raw;
raw = require('./raw.iPhoneSE.json');
var iPhoneSE = new Phone(raw);
iPhoneSE.printBrief();
arrPhone.push(iPhoneSE);

raw = require('./raw.huawei.Mate8.json');
var huaweiMate8 = new Phone(raw);
huaweiMate8.printBrief();
arrPhone.push(huaweiMate8);

raw = require('./raw.mi.max.json');
var miMax = new Phone(raw);
miMax.printBrief();
arrPhone.push(miMax);

raw = require('./raw.samsung.S6Edge.json');
var s6Edge = new Phone(raw);
s6Edge.printBrief();
arrPhone.push(s6Edge);

raw = require('./raw.nokia1000.json');
var nokia1000 = new Phone(raw);
nokia1000.printBrief();
arrPhone.push(nokia1000);

由此,控制台打印如下:

1
D:\desk\JavaScript\MongoDemo>node app.js
---printBrief()---------------------------------------
iPhone SE ¥4999
Huawei Mate8 ¥4988
Mi Max ¥1650
Samsung S6 Edge ¥3850
Nokia 1000 ¥200

其中,各个.json的文件内容如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
// raw.iPhoneSE.json
{
"device" : "iPhone SE",
"isSmart" : "true",
"releaseTime" : "2016-03-21 10:00:00",
"price" : 4999,
"apps" : [{"name" : "Safari"}, {"name" : "Map"}, {"name" : "Tinder"}],
"manufacturer": {
"name" : "Apple",
"country" : "The United States"
}
}


// raw.huawei.Mate8.json
{
"device" : "Huawei Mate8",
"isSmart" : "true",
"releaseTime" : "2015-12-09 10:00:00",
"price" : 4988,
"apps" : [{"name" : "Alipay"}, {"name" : "CMB"}, {"name" : "NetEase Music"}, {"name" : "Tinder"}, {"name" : "WeChat"}, {"name" : "Gaode Map"}],
"manufacturer": {
"name" : "Huawei",
"country" : "China"
}
}



// raw.mi.max.json
{
"device" : "Mi Max",
"isSmart" : "true",
"releaseTime" : "2015-04-17 10:00:00",
"price" : 1650,
"apps" : [{"name" : "MiChat"}, {"name" : "Didi"}, {"name" : "CMB"}],
"manufacturer": {
"name" : "Mi",
"country" : "China"
}
}

// raw.samsung.S6Edge.json
{
"device" : "Samsung S6 Edge",
"isSmart" : "true",
"releaseTime" : "2015-12-09 10:00:00",
"price" : 3850,
"apps" : [{"name" : "Taobao"}, {"name" : "Didi"}, {"name" : "Radio"}, {"name" : "360 Camera"}],
"manufacturer": {
"name" : "Samsung",
"country" : "South Korea"
}
}

// raw.nokia1000.json
{
"device" : "Nokia 1000",
"isSmart" : "false",
"releaseTime" : "2011-08-26 10:00:00",
"price" : 200,
"apps" : [{"name" : "System Phone"}, {"name" : "Radio"}],
"manufacturer": {
"name" : "Nokia",
"country" : "Finland"
}
}

单条数据存储

可以通过Model.save()很方便对单条数据进行存储。
在这里save()是个instance method

1
2
3
4
5
6
7
iPhoneSE.save((err, phone)=>{
if (err) {
console.log(err);
} else {
console.log('Phone[' + phone.device + '] saved.');
}
});

多条数据批量存储

也可以通过Model.insertMany()一次性批量存储多个。
在这里save()是个static method

1
2
3
4
5
6
7
8
9
10
Phone.insertMany(arrPhone, (err, arrPhone)=>{
if (err) {
console.log('insertMany() failed.');
} else {
console.log('---insertMany()---------------------------------------');
console.log('All phone devices saved.insertMany() saved.');
}
// 批量存储后,打印当前存储个数
Phone.printCount();
})

下一篇:mongoose教程–查询


About Sodino