取自 mongo_dart 的 blog.dart 中的示例,我想在将记录添加到数据库时添加一些强类型。任何帮助表示赞赏。
Db db = new Db("mongodb://127.0.0.1/mongo_dart-blog");
DbCollection collection;
DbCollection articlesCollection;
Map<String,Map> authors = new Map<String,Map>();
db.open().chain((o){
db.drop();
collection = db.collection('authors');
collection.insertAll( //would like strongly typed List here instead
[{'name':'William Shakespeare', 'email':'william@shakespeare.com', 'age':587},
{'name':'Jorge Luis Borges', 'email':'jorge@borges.com', 'age':123}]
);
return collection.find().each((v){authors[v["name"]] = v;});
}).chain((v){
print(">> Authors ordered by age ascending");
db.ensureIndex('authors', key: 'age');
return collection.find(where.sortBy('age')).each(
(auth)=>print("[${auth['name']}]:[${auth['email']}]:[${auth['age']}]"));
}).then((dummy){
db.close();
});