-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
33 lines (28 loc) · 1 KB
/
index.js
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
var MongoClient = require('mongodb').MongoClient,
co = require('co');
var collection;
co(function*() {
var db = yield MongoClient.connect('mongodb://localhost:27017/test');
var collection = db.collection("simple_document_insert_collection_with_generators");
return yield Promise.resolve(collection);
}).then(function(obj) {
collection = obj;
console.log(collection.s.dbName, ' collection success');
}, function(err) {
console.log('collection error');
});
co(function*() {
var db = yield MongoClient.connect('mongodb://localhost:27017/test');
var collection = db.collection("simple_document_insert_collection_with_generators");
// Insert document
var r = yield collection.insert({
hello: 'world ' + new Date().toString()
});
console.log(r.insertedCount, ' - inserted');
// Fetch all document
return yield collection.find().toArray();
}).then(function(docs) {
console.log(docs.length, ' - find all');
}, function(err) {
console.error(err.stack);
});