简体   繁体   English

本机MongoDB驱动程序Node.js-我们应该存储集合吗?

[英]Native MongoDB Driver Node.js - should we store collections?

I haven't been able to find any documents outlining if it's a bad / good idea to keep a reference to a collection so that it can be re-used after the db connection has been established. 我没有找到任何概述可以保留对集合的引用的好主意的文档,以便在建立数据库连接后可以重新使用它。

for instance, in our database.ts which is called during our server start-up. 例如,在我们的服务器启动期间调用的database.ts中。 It will grab the collections and store it for use in the module which can then be references throughout the project. 它将获取这些集合并将其存储在模块中使用,然后可以在整个项目中作为参考。

example of storing the collection 存储集合的示例

 /*database.ts*/ //module to keep 1 connection alive and allow us to have 1 instance of our collections. import {MongoClient, Db, Collection } from 'mongodb'; let uri: string = 'connection_string'; export let db: Db; export let usrCollection: Collection; export let bookCollection: Collection; export function initDb(cb: any) { MongoClient.connect(uri, function(err, database) { //handle err db = database; cb(); //returns now since db is assigned. }); } export function initCollections() { usrCollection = db.collection('users'); //non-async bookCollection = db.collection('book'); //non-async } /*app.ts*/ //in our express app when it start up import {db, initCollections, initDb} from './database'; initDb(function() { //this will hold up our server from starting BUT //there is no reason to make it async. initCollections(); }); 

what are some of the short coming of this pattern/build? 这种模式/构建的短处是什么? what can I improve and what should I avoid for performance hits specificially with managing the collections.would this pattern keep my source code bug free or easy to find the bugs and extensible for the future? 我应该改进哪些方面,以及应该特别注意管理集合而避免性能下降。这种模式将使我的源代码错误免费或容易找到并在将来扩展吗? or is there even a better pattern - please no ext library aside from the native mongodb driver for node.js solutions, such as mongoose. 还是有更好的模式-除了用于mongoose之类的node.js解决方案的本地mongodb驱动程序之外,请不要使用ext库。

IMO, this is not a good practise. 海事组织,这不是一个好习惯。 Users of database.ts will have to make sure that they have initialized these collections before using them. database.ts用户必须在使用它们之前确保已初始化这些集合。 I will not even export any internal collections, since user should never care how the database is structured. 我什至不会导出任何内部集合,因为用户永远不必关心数据库的结构。 You should export some operation functions like function addUser(user: User) {/.../} . 您应该导出一些操作函数,例如function addUser(user: User) {/.../} There is a neat code sample created by MS . MS创建了一个简洁的代码示例

//Edit. //编辑。 Yes, you can store collections reference internally so that you don't need to type collection names every time. 是的,您可以在内部存储集合引用,这样就不必每次都键入集合名称。 It's easy to introduce bugs by ignoring singular and plural. 通过忽略单数和复数很容易引入错误。 The best practise would be: 最佳做法是:

import mongodb = require('mongodb');

const server = new mongodb.Server('localhost', 27017, {auto_reconnect: true})
const db = new mongodb.Db('mydb', server, { w: 1 });
db.open(function() {});
const userCollection =db.collection("users");
const bookCollection = db.collection("book"); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM