简体   繁体   English

您如何计算Node内MongoDB集合中的文档数量?

[英]How do you count the amount of documents in a MongoDB collection within Node?

Quite an odd situation, I've tried numerous solutions and I can't seem to crack it. 奇怪的是,我尝试了许多解决方案,但似乎无法破解。

A lot of the resources I've found only include mongo console commands, I'm not sure how you'd write this in Node.js. 我发现的很多资源仅包含mongo控制台命令,我不确定您如何在Node.js中编写它。

The reason I'm trying to work this out is I'm trying to make each documents 'id' iterate from the last, so the attempt is to find the amount of documents in a collection, add one and then use that number as the 'id' for the new document. 我尝试解决此问题的原因是,我试图使每个文档的“ id”都从上一个开始进行迭代,因此尝试查找集合中文档的数量,添加一个,然后使用该数字作为新文件的'id'。

To get the number of documents in a collection, use db.collection.find({}).count() . 要获取集合中的文档数,请使用db.collection.find({}).count()

However, what you are trying to do will not work. 但是,您尝试执行的操作将不起作用。 When you have a lot of parallel accesses to the database, then it is possible that multiple threads do this at the same time, receive the same count and will thus insert a document with the same id. 当您对数据库有很多并行访问时,则可能多个线程同时执行此操作,接收到相同的计数,因此将插入具有相同ID的文档。 According to the CAP theorem , a distributed database like MongoDB can not provide this kind of consistency. 根据CAP定理 ,像MongoDB这样的分布式数据库无法提供这种一致性。

What you should do instead is rely on MongoDB ObjectId 's as unique identifiers for documents. 相反,您应该做的是依靠MongoDB ObjectId作为文档的唯一标识符。 MongoDB generates these automatically for each document when you don't provide an own value for _id . 如果您没有为_id提供自己的值,则MongoDB将为每个文档自动生成这些值。 ObjectId's are globally unique (unique enough for any practical purpose), so you won't get any collisions. ObjectId在全局上是唯一的(对于任何实际目的而言都是唯一的),因此您不会遇到任何冲突。 They also begin with a timestamp, so when you order by _id you get a roughly chronological order (as previously stated, a strict chronological order is impossible to provide by a distributed system). 它们也以时间戳记开头,因此当您通过_id排序时,您将获得大致的时间顺序(如前所述,分布式系统不可能提供严格的时间顺序)。

As a rule of thumb, whenever you would use AUTO_INCREMENT in SQL, you would likely use ObjectId's in MongodDB. 根据经验,只要在SQL中使用AUTO_INCREMENT ,就可能在MongodDB中使用ObjectId。

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

相关问题 如何计算不。 Node.Js在MongoDB集合中收集文档? - How to count no. of documents in MongoDB collection using Node.Js ? 如何将字符串推送到 mongodb 集合中的所有用户文档中? - How do you push a string into all users documents in a mongodb collection? 您如何使用 admin SDK 计算 firestore 集合中的文档? - How do you count documents in a firestore collection using the admin SDK? MongoDB 使用 Node.js 获取集合中的文档数(计数) - MongoDB get the number of documents (count) in a collection using Node.js 修复了 mongodb 集合中文档数量的限制 - Fixed limit on the amount of documents in a mongodb collection 如何计算Node-MongoDB-native stream()返回的文档? - How to count documents returned by Node-MongoDB-native stream()? 如何将 MongoDB-atlas 集合中的特定文档与存储在同一集群中不同集合中的文档相关联? - How do I associate specific documents in a collection in MongoDB-atlas to documents that are stored in a different collection in the same cluster? 如何使用 node.js 对 object 中的值求和,object 是 mongodb 中的嵌套子文档? - How do you sum the values within an object which is a nested subdocument in mongodb using node.js? 如何计算 mongodb/express 中的文档数 - How to count documents in mongodb/express 如何计算在数组中包含值的文档? - How do I count the documents that include a value within an array?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM