简体   繁体   English

mongodb如何保存注册用户数?

[英]How to save the number of registered users in mongodb?

I have a problem and I am looking for a solution.我有一个问题,我正在寻找解决方案。 I want to save the number of users registered in mongodb.我想保存在mongodb中注册的用户数。 For example, in django, the admin page has the number of registered users, and all other data is saved there.例如,在 django 中,管理页面有注册用户的数量,所有其他数据都保存在那里。 I want it to be saved in mongodb database instead of showing it on admin page, because my other data is also saved in mongodb.我希望将其保存在 mongodb 数据库中,而不是在管理页面上显示,因为我的其他数据也保存在 mongodb 中。 How do I do this?我该怎么做呢? Should I make separate a class in models.py or something else.我应该在models.py或其他东西中单独创建一个类。

You are asking a wrong question, because you should not do that.你问错了问题,因为你不应该那样做。

The number of users is User.objects.count() maybe with a filter to count only active users.用户数是User.objects.count()可能带有过滤器以仅计算活动用户。

Never save data that can be calculated/derived from other data, as this will just lead to inconsistencies.永远不要保存可以从其他数据计算/导出的数据,因为这只会导致不一致。 Why do you want to save it?你为什么要保存它? You'd have to make sure the number is updated every time a new user is added/deleted and it's so easy to forget places in your code where this might happen.您必须确保每次添加/删除新用户时都会更新该号码,并且很容易忘记代码中可能发生这种情况的地方。

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

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